2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-15 05:29:43 +08:00
|
|
|
class Remap
|
|
|
|
def initialize(from, to, regex: false, verbose: false)
|
|
|
|
@from = from
|
|
|
|
@to = to
|
|
|
|
@regex = regex
|
|
|
|
@verbose = verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
sql = "SELECT table_name, column_name
|
|
|
|
FROM information_schema.columns
|
|
|
|
WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%') and is_updatable = 'YES'"
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
log "Remapping #{table_name} #{column_name}"
|
|
|
|
|
|
|
|
result = if @regex
|
|
|
|
cnn.async_exec("UPDATE #{table_name}
|
|
|
|
SET #{column_name} = regexp_replace(#{column_name}, $1, $2, 'g')
|
|
|
|
WHERE NOT #{column_name} IS NULL
|
|
|
|
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
|
|
|
|
|
|
|
|
log "#{result.cmd_tuples} rows affected!"
|
|
|
|
end
|
2019-03-29 06:31:58 +08:00
|
|
|
|
|
|
|
Theme.expire_site_cache!
|
2019-05-01 21:44:45 +08:00
|
|
|
SiteIconManager.ensure_optimized!
|
2018-08-15 05:29:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def log(message)
|
|
|
|
puts(message) if @verbose
|
|
|
|
end
|
|
|
|
end
|