FEATURE: task for global rewrite, used post migration to multisite
This commit is contained in:
parent
c555538db4
commit
44dc578ff9
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue