Skip to content

Commit

Permalink
Fix drop table invert when if_exists option is present
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlipka committed Mar 13, 2023
1 parent 08b2dd6 commit d8b564f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions activerecord/lib/active_record/migration/command_recorder.rb
Expand Up @@ -184,7 +184,17 @@ def invert_transaction(args, &block)
[:transaction, args, invertions_proc]
end

def invert_create_table(args, &block)
if args.last.is_a?(Hash)
args.last.delete(:if_not_exists)
end
super
end

def invert_drop_table(args, &block)
if args.last.is_a?(Hash)
args.last.delete(:if_exists)
end
if args.size == 1 && block == nil
raise ActiveRecord::IrreversibleMigration, "To avoid mistakes, drop_table is only reversible if given options or a block (can be empty)."
end
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/migration/command_recorder_test.rb
Expand Up @@ -152,6 +152,14 @@ def test_invert_create_table
assert_equal [:drop_table, [:system_settings], nil], drop_table
end

def test_invert_create_table_with_if_not_exists
@recorder.revert do
@recorder.record :create_table, [:system_settings, if_not_exists: true]
end
drop_table = @recorder.commands.first
assert_equal [:drop_table, [:system_settings, {}], nil], drop_table
end

def test_invert_create_table_with_options_and_block
block = Proc.new { }
drop_table = @recorder.inverse_of :create_table, [:people_reminders, id: false], &block
Expand All @@ -164,6 +172,12 @@ def test_invert_drop_table
assert_equal [:create_table, [:people_reminders, id: false], block], create_table
end

def test_invert_drop_table_with_if_exists
block = Proc.new { }
create_table = @recorder.inverse_of :drop_table, [:people_reminders, id: false, if_exists: true], &block
assert_equal [:create_table, [:people_reminders, id: false], block], create_table
end

def test_invert_drop_table_without_a_block_nor_option
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.inverse_of :drop_table, [:people_reminders]
Expand Down

0 comments on commit d8b564f

Please sign in to comment.