From af871a0623740f53a4dca5858b78efb35f0e32e0 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Mon, 19 Nov 2012 01:12:36 -0500 Subject: Make drop_table reversible [#8267] --- .../connection_adapters/abstract/schema_statements.rb | 4 ++++ .../lib/active_record/migration/command_recorder.rb | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index bf3155e4e2..a2feb04b77 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -305,6 +305,10 @@ module ActiveRecord end # Drops a table from the database. + # + # Although this command ignores +options+ and the block if one is given, it can be helpful + # to provide these in a migration's +change+ method so it can be reverted. + # In that case, +options+ and the block will be used by create_table. def drop_table(table_name, options = {}) execute "DROP TABLE #{quote_table_name(table_name)}" end diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index 8dad1b123f..f37ec1feaa 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -73,7 +73,7 @@ module ActiveRecord [:create_table, :create_join_table, :change_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_join_table, :drop_table ].each do |method| class_eval <<-EOV, __FILE__, __LINE__ + 1 def #{method}(*args, &block) # def create_table(*args, &block) @@ -90,8 +90,15 @@ module ActiveRecord [:transaction, args, block] end - def invert_create_table(args) - [:drop_table, [args.first]] + def invert_create_table(args, &block) + [:drop_table, args, block] + end + + def invert_drop_table(args, &block) + 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 + [:create_table, args, block] end def invert_create_join_table(args, &block) -- cgit v1.2.3