Skip to content

Commit

Permalink
Add create_source_columns option for migrations (globalize#715)
Browse files Browse the repository at this point in the history
If you use `migrate_date` option on `_create_translation_table_` method, then on roll back, `_drop_translation_table_ `fails because it try to create new columns that exist yet.

So I added create `create_source_columns` property (as reverse for `remove_source_columns`) to `_drop_translation_table_` method.
  • Loading branch information
IlyasValiullov authored and parndt committed Dec 9, 2019
1 parent 0bfe947 commit 22c298d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Add `create_source_columns` option for migrations. [#715](https://github.com/globalize/globalize/pull/715) by [IlyasValiullov](https://github.com/IlyasValiullov)
* Autosave is now configurable, but defaults to false. [#736](https://github.com/globalize/globalize/pull/736) by [James Hart](https://github.com/hjhart)

## 5.3.0 (2019-05-14)
Expand Down
3 changes: 1 addition & 2 deletions lib/globalize/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def remove_source_columns
end

def drop_translation_table!(options = {})
add_missing_columns if options[:create_source_columns]
move_data_to_model_table if options[:migrate_data]
drop_translations_index
drop_translation_table
Expand Down Expand Up @@ -148,8 +149,6 @@ def move_data_to_translation_table
end

def move_data_to_model_table
add_missing_columns

# Find all of the translated attributes for all records in the model.
all_translated_attributes = model.all.collect{|m| m.attributes}
all_translated_attributes.each do |translated_record|
Expand Down
10 changes: 10 additions & 0 deletions test/globalize/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ class MigrationTest < MiniTest::Spec
assert !Migrated.translation_class.index_exists_on?(:locale)
end

it 'create source columns on drops the translations table' do
column_before = Migrated.columns.detect { |c| c.name == 'name' }

Migrated.create_translation_table!({:name => :string}, :remove_source_columns => true)
Migrated.drop_translation_table!(:create_source_columns => true)

column = Migrated.columns.detect { |c| c.name == 'name' }
assert_equal column_before.try(:type), column.try(:type)
end

it 'cannot be called on non-translated models' do
assert_raises NoMethodError do
Blog.drop_translation_table!
Expand Down

0 comments on commit 22c298d

Please sign in to comment.