REFACTOR: moved the export.rake task into script/discourse script

This commit is contained in:
Régis Hanol 2014-02-21 16:17:00 +01:00
parent bbd14c5589
commit 0caee99e8b
3 changed files with 22 additions and 88 deletions

View File

@ -233,7 +233,7 @@ module Import
log "Migrating the database..."
Discourse::Application.load_tasks
ENV["VERSION"] = @current_version.to_s
Rake::Task["db:migrate:up"].invoke
Rake::Task["db:migrate"].invoke
end
def reconnect_database

View File

@ -1,53 +0,0 @@
desc 'export the database'
task 'export', [:output_filename] => :environment do |t, args|
require "backup_restore"
require "export/exporter"
puts "Starting export..."
backup = Export::Exporter.new(Discourse.system_user.id).run
if args.output_filename.present?
puts "Moving '#{backup}' to '#{args.output_filename}'"
FileUtils.mv(backup, args.output_filename)
backup = args.output_filename
end
puts "Export done."
puts "Output file is in: #{backup}", ""
end
desc 'import from an export file and replace the contents of the current database'
task 'import', [:input_filename] => :environment do |t, args|
require "backup_restore"
require "import/importer"
begin
puts 'Starting import...'
Import::Importer.new(Discourse.system_user.id, args.input_filename).run
puts 'Import done.'
rescue Import::FilenameMissingError
puts '', 'The filename argument was missing.', '', 'Usage:', ''
puts ' rake import[/path/to/export.json.gz]', ''
rescue Import::ImportDisabledError
puts '', 'Imports are not allowed.', 'An admin needs to set allow_restore to true in the site settings before imports can be run.', ''
puts 'Import cancelled.', ''
end
end
desc 'After a successful import, restore the backup tables'
task 'import:rollback' => :environment do |t|
puts 'Rolling back if needed..'
require "backup_restore"
BackupRestore.rollback!
puts 'Done.'
end
desc 'Allow imports'
task 'import:enable' => :environment do |t|
SiteSetting.allow_restore = true
puts 'Imports are now permitted. Disable them with rake import:disable'
end
desc 'Forbid imports'
task 'import:disable' => :environment do |t|
SiteSetting.allow_restore = false
puts 'Imports are now forbidden.'
end

View File

@ -84,45 +84,32 @@ WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%
restore(filename)
end
desc "migrate", "Make sure all the posts are pointing to the new domain"
option :from, type: :array, required: true, banner: "http://previous.domain.com"
option :database, default: "default", aliases: :db
def migrate
verbose = options[:verbose]
database = options[:database]
from = options[:from].map { |f| schemaless f }
desc "rollback", "Rollback to the previous working state"
def rollback
load_rails
require "backup_restore"
begin
puts "loading rails..." if verbose
load_rails
puts 'Rolling back if needed..'
BackupRestore.rollback!
puts 'Done.'
end
puts "connecting to #{database}..." if verbose
RailsMultisite::ConnectionManagement.establish_connection(db: database)
desc "enable_restore", "Allow restore operations"
def enable_restore
load_rails
require "site_setting"
base_url = schemaless Discourse.base_url_no_prefix
SiteSetting.allow_restore = true
puts 'Restore are now permitted. Disable them with `disable_restore`'
end
puts "updating #{Post.count} posts to #{base_url}" if verbose
Post.find_each do |post|
raw, cooked = post.raw.dup, post.cooked.dup
from.each do |f|
raw.gsub!(f, base_url)
cooked.gsub!(f, base_url)
end
if raw != post.raw || cooked != post.cooked
Post.where(id: post.id).update_all(raw: raw, cooked: cooked)
putc "#" if verbose
else
putc "."
end
end
desc "disable_restore", "Forbid restore operations"
def disable_restore
load_rails
require "site_setting"
rescue => e
puts "Cannot connect to database: #{database}"
puts e
puts e.backtrace.join("\n")
end
puts "", "done!" if verbose
SiteSetting.allow_restore = false
puts 'Restore are now forbidden. Enable them with `enable_restore`'
end
private