Don't abort the migration if the simulation fails

We think aborting the migration will generate more headaches to system
administrators, who will have to manually check and fix every invalid
record before anything can be migrated.
This commit is contained in:
Javi Martín
2018-10-22 11:13:07 +02:00
committed by decabeza
parent 10b0d8232d
commit adf03b9e1b
2 changed files with 12 additions and 8 deletions

View File

@@ -41,7 +41,7 @@ namespace :globalize do
begin
record.save!
rescue ActiveRecord::RecordInvalid
logger.error "Failed to save #{model_class} with id #{record.id}"
logger.warn "Failed to save #{model_class} with id #{record.id}"
@errored = true
end
end
@@ -68,17 +68,20 @@ namespace :globalize do
end
if errored?
logger.error "Simulation failed! Please check the errors and solve them before proceeding."
raise "Simulation failed!"
logger.warn "Some database records will not be migrated"
else
logger.info "Migrate data simulation ended successfully"
end
end
desc "Migrates existing data to translation tables"
task migrate_data: :simulate_migrate_data do
task migrate_data: :environment do
logger.info "Starting data migration"
migrate_data
logger.info "Finished data migration"
if errored?
logger.warn "Some database records couldn't be migrated; please check the log messages"
end
end
end

View File

@@ -11,7 +11,6 @@ describe "Globalize tasks" do
end
let :run_rake_task do
Rake::Task["globalize:simulate_migrate_data"].reenable
Rake::Task["globalize:migrate_data"].reenable
Rake.application.invoke_task "globalize:migrate_data"
end
@@ -134,14 +133,16 @@ describe "Globalize tasks" do
end
end
it "simulates the task and aborts without creating any translations" do
it "ignores invalid data and migrates valid data" do
expect(valid_process).to be_valid
expect(invalid_process).not_to be_valid
expect { run_rake_task }.to raise_exception("Simulation failed!")
run_rake_task
expect(Legislation::Process::Translation.count).to eq 0
expect(valid_process.translations.count).to eq 1
expect(valid_process.reload.title).to eq "Title"
expect(invalid_process.translations.count).to eq 0
expect(invalid_process.reload.title).to eq ""
end
end