diff --git a/db/migrate/20170330084810_remove_translations_from_custom_fields.rb b/db/migrate/20170330084810_remove_translations_from_custom_fields.rb index b87929c2b8..51a80c4fb8 100644 --- a/db/migrate/20170330084810_remove_translations_from_custom_fields.rb +++ b/db/migrate/20170330084810_remove_translations_from_custom_fields.rb @@ -8,10 +8,33 @@ class RemoveTranslationsFromCustomFields < ActiveRecord::Migration[5.0] translates :name end + class NewCustomField < ActiveRecord::Base + self.table_name = :custom_fields + self.inheritance_column = nil + end + + def get_globalize_fallbacks + first_defined_locale = {} + OldCustomField::Translation.pluck(:custom_field_id, :name).each do |id, name| + next if first_defined_locale[id] + first_defined_locale[id] = name + end + + first_defined_locale + end + def change reversible do |dir| dir.up do + names = get_globalize_fallbacks OldCustomField.drop_translation_table! migrate_data: true + + NewCustomField.transaction do + NewCustomField.where(name: nil).each do |cf| + say "Custom field #{cf.id} is missing translation for #{I18n.locale}: Falling back to #{names[cf.id]}" + cf.update_attribute(:name, names[cf.id] || "Custom field #{cf.id}") + end + end end dir.down do