FEATURE: task for global rewrite, used post migration to multisite

This commit is contained in:
Sam 2014-01-31 14:53:25 +11:00
parent c555538db4
commit 44dc578ff9
1 changed files with 33 additions and 0 deletions

View File

@ -5,6 +5,39 @@ require "thor"
class DiscourseCLI < Thor
class_option :verbose, default: false, aliases: :v
desc "remap", "Remap a string sequence accross all tables"
def remap(from, to)
load_rails
puts "Rewriting all occurences of #{from} to #{to}"
puts "THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)"
result = STDIN.gets
if result.strip != "YES"
puts "aborting."
exit
end
sql = "SELECT table_name, column_name
FROM information_schema.columns
WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%')"
cnn = ActiveRecord::Base.connection.raw_connection
results = cnn.async_exec(sql).to_a
results.each do |result|
table_name = result["table_name"]
column_name = result["column_name"]
puts "Reampping #{table_name} #{column_name}"
result = 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])
puts "#{result.cmd_tuples} rows affected!"
end
end
desc "export", "Export a Discourse backup"
def export(filename=nil)
load_rails