Revert "FEATURE: make discourse remap optionally do regex_replace (#4367)"

This reverts commit f8dda198bd, reversing
changes made to 01ced67ab3.
This commit is contained in:
Robin Ward 2016-08-08 17:05:22 -04:00
parent 5191cbdcbb
commit 277e7383f3
1 changed files with 13 additions and 24 deletions

View File

@ -6,31 +6,27 @@ class DiscourseCLI < Thor
class_option :verbose, default: false, aliases: :v class_option :verbose, default: false, aliases: :v
desc "remap", "Remap a string sequence accross all tables" desc "remap", "Remap a string sequence accross all tables"
option :global, :type => :boolean def remap(from, to, global=nil)
option :regex, :type => :boolean
def remap(from, to)
load_rails load_rails
if options[:regex] global = global == "--global"
puts "Rewriting all occurences of #{from} to #{to} using regexp_replace"
else puts "Rewriting all occurences of #{from} to #{to}"
puts "Rewriting all occurences of #{from} to #{to}"
end
puts "THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)" puts "THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)"
puts "WILL RUN ON ALL #{RailsMultisite::ConnectionManagement.all_dbs.length} DBS" if options[:global] puts "WILL RUN ON ALL #{RailsMultisite::ConnectionManagement.all_dbs.length} DBS" if global
text = STDIN.gets text = STDIN.gets
if text.strip != "YES" if text.strip != "YES"
puts "aborting." puts "aborting."
exit exit
end end
if options[:global] if global
RailsMultisite::ConnectionManagement.each_connection do |db| RailsMultisite::ConnectionManagement.each_connection do |db|
puts "","Remapping tables on #{db}...","" puts "","Remapping tables on #{db}...",""
do_remap(from, to, options[:regex]) do_remap(from, to)
end end
else else
do_remap(from, to, options[:regex]) do_remap(from, to)
end end
end end
@ -203,7 +199,7 @@ class DiscourseCLI < Thor
require File.expand_path(File.dirname(__FILE__) + "/../lib/import_export/import_export") require File.expand_path(File.dirname(__FILE__) + "/../lib/import_export/import_export")
end end
def do_remap(from, to, regex=false) def do_remap(from, to)
sql = "SELECT table_name, column_name sql = "SELECT table_name, column_name
FROM information_schema.columns FROM information_schema.columns
WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%') and is_updatable = 'YES'" WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%') and is_updatable = 'YES'"
@ -217,17 +213,10 @@ WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%
column_name = result["column_name"] column_name = result["column_name"]
puts "Remapping #{table_name} #{column_name}" puts "Remapping #{table_name} #{column_name}"
begin begin
result = if regex result = cnn.async_exec("UPDATE #{table_name}
cnn.async_exec("UPDATE #{table_name} SET #{column_name} = replace(#{column_name}, $1, $2)
SET #{column_name} = regexp_replace(#{column_name}, $1, $2, 'g') WHERE NOT #{column_name} IS NULL
WHERE NOT #{column_name} IS NULL AND #{column_name} <> replace(#{column_name}, $1, $2)", [from, to])
AND #{column_name} <> regexp_replace(#{column_name}, $1, $2, 'g')", [from, to])
else
cnn.async_exec("UPDATE #{table_name}
SET #{column_name} = replace(#{column_name}, $1, $2)
WHERE NOT #{column_name} IS NULL
AND #{column_name} <> replace(#{column_name}, $1, $2)", [from, to])
end
puts "#{result.cmd_tuples} rows affected!" puts "#{result.cmd_tuples} rows affected!"
rescue => ex rescue => ex
puts "Error: #{ex}" puts "Error: #{ex}"