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 all commits
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
11 changes: 7 additions & 4 deletions db/migrate/20180323190027_add_translate_milestones.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
class AddTranslateMilestones < ActiveRecord::Migration
def self.up
Budget::Investment::Milestone.create_translation_table!({
title: :string,
description: :text
})
Budget::Investment::Milestone.create_translation_table!(
{
title: :string,
description: :text
},
{ migrate_data: true }
)
end

def self.down
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ def change

reversible do |dir|
dir.up do
I18nContent.create_translation_table! :value => :text
I18nContent.create_translation_table!(
{ value: :text },
{ migrate_data: true }
)
end

dir.down do
Expand Down
7 changes: 5 additions & 2 deletions db/migrate/20180727140800_add_banner_translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ class AddBannerTranslations < ActiveRecord::Migration

def self.up
Banner.create_translation_table!(
title: :string,
description: :text
{
title: :string,
description: :text
},
{ migrate_data: true }
)
end

Expand Down
11 changes: 7 additions & 4 deletions db/migrate/20180730120800_add_homepage_content_translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ class AddHomepageContentTranslations < ActiveRecord::Migration

def self.up
Widget::Card.create_translation_table!(
label: :string,
title: :string,
description: :text,
link_text: :string
{
label: :string,
title: :string,
description: :text,
link_text: :string
},
{ migrate_data: true }
)
end

Expand Down
9 changes: 6 additions & 3 deletions db/migrate/20180730213824_add_poll_translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ class AddPollTranslations < ActiveRecord::Migration

def self.up
Poll.create_translation_table!(
name: :string,
summary: :text,
description: :text
{
name: :string,
summary: :text,
description: :text
},
{ migrate_data: true }
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ class AddAdminNotificationTranslations < ActiveRecord::Migration

def self.up
AdminNotification.create_translation_table!(
title: :string,
body: :text
{
title: :string,
body: :text
},
{ migrate_data: true }
)
end

Expand Down
3 changes: 2 additions & 1 deletion db/migrate/20180731173147_add_poll_question_translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class AddPollQuestionTranslations < ActiveRecord::Migration

def self.up
Poll::Question.create_translation_table!(
title: :string
{ title: :string },
{ migrate_data: true }
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ class AddPollQuestionAnswerTranslations < ActiveRecord::Migration

def self.up
Poll::Question::Answer.create_translation_table!(
title: :string,
description: :text
{
title: :string,
description: :text
},
{ migrate_data: true }
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ class AddCollaborativeLegislationTranslations < ActiveRecord::Migration

def self.up
Legislation::Process.create_translation_table!(
title: :string,
summary: :text,
description: :text,
additional_info: :text,
{
title: :string,
summary: :text,
description: :text,
additional_info: :text,
},
{ migrate_data: true }
)

Legislation::Question.create_translation_table!(
title: :text
{ title: :text },
{ migrate_data: true }
)

Legislation::DraftVersion.create_translation_table!(
title: :string,
changelog: :text,
body: :text,
body_html: :text,
toc_html: :text
{
title: :string,
changelog: :text,
body: :text,
body_html: :text,
toc_html: :text
},
{ migrate_data: true }
)
Legislation::QuestionOption.create_translation_table!(
value: :string
{ value: :string },
{ migrate_data: true }
)
end

Expand Down
14 changes: 9 additions & 5 deletions db/migrate/20180924071722_add_translate_pages.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
class AddTranslatePages < ActiveRecord::Migration
def self.up
SiteCustomization::Page.create_translation_table!({
title: :string,
subtitle: :string,
content: :text
})
SiteCustomization::Page.create_translation_table!(
{
title: :string,
subtitle: :string,
content: :text
},
{ migrate_data: true }
)

change_column :site_customization_pages, :title, :string, :null => true
end

Expand Down
89 changes: 89 additions & 0 deletions lib/tasks/globalize.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace :globalize do
def translatable_classes
[
AdminNotification,
Banner,
Budget::Investment::Milestone,
I18nContent,
Legislation::DraftVersion,
Legislation::Process,
Legislation::Question,
Legislation::QuestionOption,
Poll,
Poll::Question,
Poll::Question::Answer,
SiteCustomization::Page,
Widget::Card
]
end

def migrate_data
@errored = false

translatable_classes.each do |model_class|
logger.info "Migrating #{model_class} data"

fields = model_class.translated_attribute_names

model_class.find_each do |record|
fields.each do |field|
locale = if model_class == SiteCustomization::Page && record.locale.present?
record.locale
else
I18n.locale
end

translated_field = record.localized_attr_name_for(field, locale)

if record.send(translated_field).blank?
record.send(:"#{translated_field}=", record.untranslated_attributes[field.to_s])
end
end

begin
record.save!
rescue ActiveRecord::RecordInvalid
logger.warn "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.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: :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
Loading