mirror of
https://github.com/discourse/discourse.git
synced 2025-06-17 08:12:35 +08:00
Add option to skip tabels when using DbHelper.remap
.
This commit is contained in:
@ -8,7 +8,7 @@ class DbHelper
|
|||||||
AND (data_type LIKE 'char%' OR data_type LIKE 'text%')
|
AND (data_type LIKE 'char%' OR data_type LIKE 'text%')
|
||||||
ORDER BY table_name, column_name"
|
ORDER BY table_name, column_name"
|
||||||
|
|
||||||
def self.remap(from, to, anchor_left = false, anchor_right = false)
|
def self.remap(from, to, anchor_left: false, anchor_right: false, exclude_tables: [])
|
||||||
results = DB.query(REMAP_SQL).to_a
|
results = DB.query(REMAP_SQL).to_a
|
||||||
like = "#{anchor_left ? '' : "%"}#{from}#{anchor_right ? '' : "%"}"
|
like = "#{anchor_left ? '' : "%"}#{from}#{anchor_right ? '' : "%"}"
|
||||||
|
|
||||||
@ -19,7 +19,10 @@ class DbHelper
|
|||||||
remappable_columns[result.table_name] << result.column_name
|
remappable_columns[result.table_name] << result.column_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
exclude_tables = exclude_tables.map(&:to_s)
|
||||||
|
|
||||||
remappable_columns.each do |table_name, column_names|
|
remappable_columns.each do |table_name, column_names|
|
||||||
|
next if exclude_tables.include?(table_name)
|
||||||
set_clause = column_names.map do |column_name|
|
set_clause = column_names.map do |column_name|
|
||||||
"#{column_name} = REPLACE(#{column_name}, :from, :to)"
|
"#{column_name} = REPLACE(#{column_name}, :from, :to)"
|
||||||
end.join(", ")
|
end.join(", ")
|
||||||
|
@ -32,5 +32,13 @@ RSpec.describe DbHelper do
|
|||||||
expect(badge_attributes.except("query"))
|
expect(badge_attributes.except("query"))
|
||||||
.to eq(badge.attributes.except("query"))
|
.to eq(badge.attributes.except("query"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'allows tables to be excluded from scanning' do
|
||||||
|
post = Fabricate(:post, cooked: "test")
|
||||||
|
|
||||||
|
DbHelper.remap("test", "something else", exclude_tables: %w{posts})
|
||||||
|
|
||||||
|
expect(post.reload.cooked).to eq('test')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user