mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 23:07:28 +08:00
REFACTOR: moved the export.rake task into script/discourse script
This commit is contained in:
@ -233,7 +233,7 @@ module Import
|
|||||||
log "Migrating the database..."
|
log "Migrating the database..."
|
||||||
Discourse::Application.load_tasks
|
Discourse::Application.load_tasks
|
||||||
ENV["VERSION"] = @current_version.to_s
|
ENV["VERSION"] = @current_version.to_s
|
||||||
Rake::Task["db:migrate:up"].invoke
|
Rake::Task["db:migrate"].invoke
|
||||||
end
|
end
|
||||||
|
|
||||||
def reconnect_database
|
def reconnect_database
|
||||||
|
@ -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
|
|
@ -84,45 +84,32 @@ WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%
|
|||||||
restore(filename)
|
restore(filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "migrate", "Make sure all the posts are pointing to the new domain"
|
desc "rollback", "Rollback to the previous working state"
|
||||||
option :from, type: :array, required: true, banner: "http://previous.domain.com"
|
def rollback
|
||||||
option :database, default: "default", aliases: :db
|
load_rails
|
||||||
def migrate
|
require "backup_restore"
|
||||||
verbose = options[:verbose]
|
|
||||||
database = options[:database]
|
|
||||||
from = options[:from].map { |f| schemaless f }
|
|
||||||
|
|
||||||
begin
|
puts 'Rolling back if needed..'
|
||||||
puts "loading rails..." if verbose
|
BackupRestore.rollback!
|
||||||
load_rails
|
puts 'Done.'
|
||||||
|
end
|
||||||
|
|
||||||
puts "connecting to #{database}..." if verbose
|
desc "enable_restore", "Allow restore operations"
|
||||||
RailsMultisite::ConnectionManagement.establish_connection(db: database)
|
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
|
desc "disable_restore", "Forbid restore operations"
|
||||||
Post.find_each do |post|
|
def disable_restore
|
||||||
raw, cooked = post.raw.dup, post.cooked.dup
|
load_rails
|
||||||
from.each do |f|
|
require "site_setting"
|
||||||
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
|
|
||||||
|
|
||||||
rescue => e
|
SiteSetting.allow_restore = false
|
||||||
puts "Cannot connect to database: #{database}"
|
puts 'Restore are now forbidden. Enable them with `enable_restore`'
|
||||||
puts e
|
|
||||||
puts e.backtrace.join("\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "", "done!" if verbose
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
Reference in New Issue
Block a user