aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2012-12-18 23:32:45 -0500
committerMarc-Andre Lafortune <github@marc-andre.ca>2012-12-21 13:54:52 -0500
commite43e8e268f0522f9f11f4eacae1d04afca768c9b (patch)
treec512c465509c02d4007c04123ed0f985fefb20b9 /activerecord
parentaedcd683684d08eaf30623a4b48ce31a31426372 (diff)
downloadrails-e43e8e268f0522f9f11f4eacae1d04afca768c9b.tar.gz
rails-e43e8e268f0522f9f11f4eacae1d04afca768c9b.tar.bz2
rails-e43e8e268f0522f9f11f4eacae1d04afca768c9b.zip
Make change_table reversible when possible [#8267]
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/migration/command_recorder.rb6
-rw-r--r--activerecord/test/cases/migration/command_recorder_test.rb20
2 files changed, 25 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb
index 4ea709fe69..f738c909bc 100644
--- a/activerecord/lib/active_record/migration/command_recorder.rb
+++ b/activerecord/lib/active_record/migration/command_recorder.rb
@@ -70,7 +70,7 @@ module ActiveRecord
super || delegate.respond_to?(*args)
end
- [:create_table, :create_join_table, :change_table, :rename_table, :add_column, :remove_column,
+ [:create_table, :create_join_table, :rename_table, :add_column, :remove_column,
:rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps,
:change_column, :change_column_default, :add_reference, :remove_reference, :transaction,
:drop_join_table, :drop_table, :remove_columns, :remove_index,
@@ -84,6 +84,10 @@ module ActiveRecord
alias :add_belongs_to :add_reference
alias :remove_belongs_to :remove_reference
+ def change_table(table_name, options = {})
+ yield ConnectionAdapters::Table.new(table_name, self)
+ end
+
private
module StraightReversions
diff --git a/activerecord/test/cases/migration/command_recorder_test.rb b/activerecord/test/cases/migration/command_recorder_test.rb
index 1c5530a132..b406beba06 100644
--- a/activerecord/test/cases/migration/command_recorder_test.rb
+++ b/activerecord/test/cases/migration/command_recorder_test.rb
@@ -77,6 +77,26 @@ module ActiveRecord
[:drop_table, ["figs"], block]], @recorder.commands
end
+ def test_invert_change_table
+ @recorder.revert do
+ @recorder.change_table :fruits do |t|
+ t.string :name
+ t.rename :kind, :cultivar
+ end
+ end
+ assert_equal [
+ [:rename_column, [:fruits, :cultivar, :kind]],
+ [:remove_column, [:fruits, :name, :string, {}], nil],
+ ], @recorder.commands
+
+ assert_raises(ActiveRecord::IrreversibleMigration) do
+ @recorder.revert do
+ @recorder.change_table :fruits do |t|
+ t.remove :kind
+ end
+ end
+ end
+ end
def test_invert_create_table
@recorder.revert do