2013-11-28 02:17:14 +08:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require "thor"
|
|
|
|
|
|
|
|
class DiscourseCLI < Thor
|
|
|
|
class_option :verbose, default: false, aliases: :v
|
|
|
|
|
2014-01-31 11:53:25 +08:00
|
|
|
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)"
|
2014-01-31 13:37:05 +08:00
|
|
|
text = STDIN.gets
|
|
|
|
if text.strip != "YES"
|
2014-01-31 11:53:25 +08:00
|
|
|
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"]
|
2014-02-13 12:38:47 +08:00
|
|
|
puts "Remapping #{table_name} #{column_name}"
|
2014-01-31 11:53:25 +08:00
|
|
|
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
|
|
|
|
|
2014-02-13 12:38:47 +08:00
|
|
|
desc "backup", "Backup a discourse forum"
|
2014-02-19 22:25:31 +08:00
|
|
|
def backup(filename = nil)
|
2014-01-17 09:27:47 +08:00
|
|
|
load_rails
|
2014-02-13 12:38:47 +08:00
|
|
|
require "backup_restore"
|
|
|
|
require "export/exporter"
|
|
|
|
|
2014-02-19 22:25:31 +08:00
|
|
|
puts "Starting export..."
|
|
|
|
backup = Export::Exporter.new(Discourse.system_user.id).run
|
|
|
|
if filename.present?
|
|
|
|
puts "Moving '#{backup}' to '#{filename}'"
|
|
|
|
FileUtils.mv(backup, filename)
|
|
|
|
backup = filename
|
|
|
|
end
|
|
|
|
puts "Export done."
|
|
|
|
puts "Output file is in: #{backup}", ""
|
2014-01-17 09:27:47 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:38:47 +08:00
|
|
|
desc "export", "Backup a Discourse forum"
|
|
|
|
def export
|
|
|
|
backup
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "restore", "Restore a Discourse backup"
|
|
|
|
def restore(filename)
|
2014-01-17 09:27:47 +08:00
|
|
|
load_rails
|
2014-02-13 12:38:47 +08:00
|
|
|
require "backup_restore"
|
|
|
|
require "import/importer"
|
|
|
|
|
2014-01-17 09:27:47 +08:00
|
|
|
begin
|
2014-02-19 22:25:31 +08:00
|
|
|
puts "Starting restore: #{filename}"
|
|
|
|
Import::Importer.new(Discourse.system_user.id, filename).run
|
|
|
|
puts 'Restore done.'
|
2014-01-17 09:27:47 +08:00
|
|
|
rescue Import::FilenameMissingError
|
|
|
|
puts '', 'The filename argument was missing.', ''
|
|
|
|
usage
|
|
|
|
rescue Import::ImportDisabledError
|
2014-03-18 01:13:05 +08:00
|
|
|
puts '', 'Restores are not allowed.', 'An admin needs to set allow_restore to true in the site settings before restores can be run.', ''
|
2014-02-19 22:25:31 +08:00
|
|
|
puts 'Restore cancelled.', ''
|
2014-01-17 09:27:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:38:47 +08:00
|
|
|
desc "import", "Restore a Discourse backup"
|
|
|
|
def import(filename)
|
|
|
|
restore(filename)
|
|
|
|
end
|
|
|
|
|
2014-02-21 23:17:00 +08:00
|
|
|
desc "rollback", "Rollback to the previous working state"
|
|
|
|
def rollback
|
|
|
|
load_rails
|
|
|
|
require "backup_restore"
|
2013-11-28 02:17:14 +08:00
|
|
|
|
2014-02-21 23:17:00 +08:00
|
|
|
puts 'Rolling back if needed..'
|
|
|
|
BackupRestore.rollback!
|
|
|
|
puts 'Done.'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "enable_restore", "Allow restore operations"
|
|
|
|
def enable_restore
|
|
|
|
load_rails
|
|
|
|
require "site_setting"
|
|
|
|
|
|
|
|
SiteSetting.allow_restore = true
|
|
|
|
puts 'Restore are now permitted. Disable them with `disable_restore`'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "disable_restore", "Forbid restore operations"
|
|
|
|
def disable_restore
|
|
|
|
load_rails
|
|
|
|
require "site_setting"
|
2013-11-28 02:17:14 +08:00
|
|
|
|
2014-02-21 23:17:00 +08:00
|
|
|
SiteSetting.allow_restore = false
|
|
|
|
puts 'Restore are now forbidden. Enable them with `enable_restore`'
|
2013-11-28 02:17:14 +08:00
|
|
|
end
|
|
|
|
|
2014-02-22 00:10:53 +08:00
|
|
|
desc "enable_readonly", "Enable the readonly mode"
|
|
|
|
def enable_readonly
|
|
|
|
load_rails
|
|
|
|
|
|
|
|
Discourse.enable_readonly_mode
|
|
|
|
puts 'The site is now in readonly mode.'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "disable_readonly", "Disable the readonly mode"
|
|
|
|
def disable_readonly
|
|
|
|
load_rails
|
|
|
|
|
|
|
|
Discourse.disable_readonly_mode
|
|
|
|
puts 'The site is now fully operable.'
|
|
|
|
end
|
|
|
|
|
2014-03-07 12:26:34 +08:00
|
|
|
desc "request_refresh", "Ask all clients to refresh the browser"
|
|
|
|
def request_refresh
|
|
|
|
load_rails
|
|
|
|
|
|
|
|
Discourse.request_refresh!
|
|
|
|
puts 'Requests sent. Clients will refresh on next navigation.'
|
|
|
|
end
|
|
|
|
|
2013-11-28 02:17:14 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_rails
|
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
|
|
|
end
|
|
|
|
|
|
|
|
def schemaless(url)
|
|
|
|
url.gsub(/^https?:/, "")
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseCLI.start(ARGV)
|