A platform for community discussion. Free, open, simple.
REFACTOR: Less duplicate code in DbHelper.remap
Gerhard Schlager8b6341669ac9
diff --git a/lib/db_helper.rb b/lib/db_helper.rbindex c637fffd70d..33fd4c672f3 100644--- a/lib/db_helper.rb+++ b/lib/db_helper.rb@@ -21,20 +21,9 @@ class DbHelperdef self.remap(from, to, anchor_left: false, anchor_right: false, excluded_tables: [], verbose: false)like = "#{anchor_left ? '' : "%"}#{from}#{anchor_right ? '' : "%"}"-- triggers = DB.query(TRIGGERS_SQL).map(&:trigger_name).to_set-- text_columns = Hash.new { |h, k| h[k] = [] }-- DB.query(REMAP_SQL).each do |r|- unless triggers.include?(Migration::BaseDropper.readonly_trigger_name(r.table_name, r.column_name))- text_columns[r.table_name] << r.column_name- end- end+ text_columns = find_text_columns(excluded_tables)text_columns.each do |table, columns|- next if excluded_tables.include?(table)-set = columns.map do |column|"#{column} = REPLACE(#{column}, :from, :to)"end.join(", ")@@ -56,19 +45,9 @@ class DbHelperenddef self.regexp_replace(pattern, replacement, flags: "gi", match: "~*", excluded_tables: [], verbose: false)- triggers = DB.query(TRIGGERS_SQL).map(&:trigger_name).to_set-- text_columns = Hash.new { |h, k| h[k] = [] }-- DB.query(REMAP_SQL).each do |r|- unless triggers.include?(Migration::BaseDropper.readonly_trigger_name(r.table_name, r.column_name))- text_columns[r.table_name] << r.column_name- end- end+ text_columns = find_text_columns(excluded_tables)text_columns.each do |table, columns|- next if excluded_tables.include?(table)-set = columns.map do |column|"#{column} = REGEXP_REPLACE(#{column}, :pattern, :replacement, :flags)"end.join(", ")@@ -120,4 +99,17 @@ class DbHelperSiteIconManager.ensure_optimized!end+ def self.find_text_columns(excluded_tables)+ triggers = DB.query(TRIGGERS_SQL).map(&:trigger_name).to_set+ text_columns = Hash.new { |h, k| h[k] = [] }++ DB.query(REMAP_SQL).each do |r|+ next if excluded_tables.include?(r.table_name) ||+ triggers.include?(Migration::BaseDropper.readonly_trigger_name(r.table_name, r.column_name))++ text_columns[r.table_name] << r.column_name+ end++ text_columns+ endend