Skip to content

Commit

Permalink
Make change_table reversible when possible [#8267]
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Dec 21, 2012
1 parent aedcd68 commit e43e8e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/migration/command_recorder.rb
Expand Up @@ -70,7 +70,7 @@ def respond_to?(*args) # :nodoc:
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,
Expand All @@ -84,6 +84,10 @@ def #{method}(*args, &block) # def create_table(*args, &block)
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
Expand Down
20 changes: 20 additions & 0 deletions activerecord/test/cases/migration/command_recorder_test.rb
Expand Up @@ -77,6 +77,26 @@ def test_revert_order
[: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
Expand Down

0 comments on commit e43e8e2

Please sign in to comment.