Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] Migrate globalize data #2986

Merged
merged 7 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add task to simulate data migration
This way we can check everything is OK before actually migrating the
data to the translations tables.
  • Loading branch information
javierm committed Oct 23, 2018
commit 7fff57a25f0794ebc696a9f76ef0f7b428d1d3a8
49 changes: 43 additions & 6 deletions lib/tasks/globalize.rake
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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
30 changes: 30 additions & 0 deletions spec/lib/tasks/globalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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
Expand Down Expand Up @@ -115,5 +116,34 @@
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