Swtich to regexp for DbHelper.remap.

This commit is contained in:
Guo Xiang Tan
2018-11-08 14:07:54 +08:00
parent 42572ff138
commit c3f89e3cd7
2 changed files with 28 additions and 6 deletions

View File

@ -8,10 +8,8 @@ class DbHelper
AND (data_type LIKE 'char%' OR data_type LIKE 'text%')
ORDER BY table_name, column_name"
def self.remap(from, to, anchor_left: false, anchor_right: false, exclude_tables: [])
def self.remap(from, to, exclude_tables: [])
results = DB.query(REMAP_SQL).to_a
like = "#{anchor_left ? '' : "%"}#{from}#{anchor_right ? '' : "%"}"
remappable_columns = {}
results.each do |result|
@ -24,14 +22,14 @@ class DbHelper
remappable_columns.each do |table_name, column_names|
next if exclude_tables.include?(table_name)
set_clause = column_names.map do |column_name|
"#{column_name} = REPLACE(#{column_name}, :from, :to)"
"#{column_name} = REGEXP_REPLACE(#{column_name}, :from, :to)"
end.join(", ")
where_clause = column_names.map do |column_name|
"#{column_name} LIKE :like"
"#{column_name} ~* :from"
end.join(" OR ")
DB.exec(<<~SQL, from: from, to: to, like: like)
DB.exec(<<~SQL, from: from, to: to, from: from)
UPDATE #{table_name}
SET #{set_clause}
WHERE #{where_clause}