Add task to simulate data migration
This way we can check everything is OK before actually migrating the data to the translations tables.
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
namespace :globalize do
|
||||
desc "Migrates existing data to translation tables"
|
||||
task migrate_data: :environment do
|
||||
logger = Logger.new(STDOUT)
|
||||
logger.formatter = proc { |severity, _datetime, _progname, msg| "#{severity} #{msg}\n" }
|
||||
|
||||
def translatable_classes
|
||||
[
|
||||
AdminNotification,
|
||||
Banner,
|
||||
@@ -18,7 +14,13 @@ namespace :globalize do
|
||||
Poll::Question::Answer,
|
||||
SiteCustomization::Page,
|
||||
Widget::Card
|
||||
].each do |model_class|
|
||||
]
|
||||
end
|
||||
|
||||
def migrate_data
|
||||
@errored = false
|
||||
|
||||
translatable_classes.each do |model_class|
|
||||
logger.info "Migrating #{model_class} data"
|
||||
|
||||
fields = model_class.translated_attribute_names
|
||||
@@ -40,8 +42,43 @@ namespace :globalize do
|
||||
record.save!
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
logger.error "Failed to save #{model_class} with id #{record.id}"
|
||||
@errored = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def logger
|
||||
@logger ||= Logger.new(STDOUT).tap do |logger|
|
||||
logger.formatter = proc { |severity, _datetime, _progname, msg| "#{severity} #{msg}\n" }
|
||||
end
|
||||
end
|
||||
|
||||
def errored?
|
||||
@errored
|
||||
end
|
||||
|
||||
desc "Simulates migrating existing data to translation tables"
|
||||
task simulate_migrate_data: :environment do
|
||||
logger.info "Starting migrate data simulation"
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
migrate_data
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
|
||||
if errored?
|
||||
logger.error "Simulation failed! Please check the errors and solve them before proceeding."
|
||||
raise "Simulation failed!"
|
||||
else
|
||||
logger.info "Migrate data simulation ended successfully"
|
||||
end
|
||||
end
|
||||
|
||||
desc "Migrates existing data to translation tables"
|
||||
task migrate_data: :simulate_migrate_data do
|
||||
logger.info "Starting data migration"
|
||||
migrate_data
|
||||
logger.info "Finished data migration"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,6 +11,7 @@ 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
|
||||
@@ -115,5 +116,34 @@ describe "Globalize tasks" do
|
||||
expect(page.send(:"title_#{I18n.locale}")).to eq("In English")
|
||||
end
|
||||
end
|
||||
|
||||
context "Invalid data" do
|
||||
let!(:valid_process) do
|
||||
create(:legislation_process).tap do |process|
|
||||
process.translations.delete_all
|
||||
process.update_column(:title, "Title")
|
||||
process.reload
|
||||
end
|
||||
end
|
||||
|
||||
let!(:invalid_process) do
|
||||
create(:legislation_process).tap do |process|
|
||||
process.translations.delete_all
|
||||
process.update_column(:title, "")
|
||||
process.reload
|
||||
end
|
||||
end
|
||||
|
||||
it "simulates the task and aborts without creating any translations" do
|
||||
expect(valid_process).to be_valid
|
||||
expect(invalid_process).not_to be_valid
|
||||
|
||||
expect { run_rake_task }.to raise_exception("Simulation failed!")
|
||||
|
||||
expect(Legislation::Process::Translation.count).to eq 0
|
||||
expect(valid_process.reload.title).to eq "Title"
|
||||
expect(invalid_process.reload.title).to eq ""
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user