Skip to content
Browse files

Add drop_join_table [#8267]

  • Loading branch information...
1 parent 99770e4 commit bd155d2ae31a1e4c19274cfef6049d66136fd0cd @marcandre marcandre committed Nov 19, 2012
View
11 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -214,6 +214,17 @@ def create_join_table(table_1, table_2, options = {})
end
end
+ # Drops the join table specified by the given arguments.
+ # See create_join_table for details.
+ #
+ # Although this command ignores the block if one is given, it can be helpful
+ # to provide one in a migration's +change+ method so it can be reverted.
+ # In that case, the block will be used by create_join_table.
+ def drop_join_table(table_1, table_2, options = {})
+ join_table_name = find_join_table_name(table_1, table_2, options)
+ drop_table(join_table_name)
+ end
+
# A block for changing columns in +table+.
#
# # change_table() yields a Table instance
View
9 activerecord/lib/active_record/migration/command_recorder.rb
@@ -73,6 +73,7 @@ def respond_to?(*args) # :nodoc:
[: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,
].each do |method|
class_eval <<-EOV, __FILE__, __LINE__ + 1
def #{method}(*args, &block) # def create_table(*args, &block)
@@ -93,10 +94,12 @@ def invert_create_table(args)
[:drop_table, [args.first]]
end
- def invert_create_join_table(args)
- table_name = find_join_table_name(*args)
+ def invert_create_join_table(args, &block)
+ [:drop_join_table, args, block]
+ end
- [:drop_table, [table_name]]
+ def invert_drop_join_table(args, &block)
+ [:create_join_table, args, block]
end
def invert_rename_table(args)
View
14 activerecord/test/cases/migration/command_recorder_test.rb
@@ -92,13 +92,19 @@ def test_invert_create_table_with_options
end
def test_invert_create_join_table
- drop_table = @recorder.inverse_of :create_join_table, [:musics, :artists]
- assert_equal [:drop_table, [:artists_musics]], drop_table
+ drop_join_table = @recorder.inverse_of :create_join_table, [:musics, :artists]
+ assert_equal [:drop_join_table, [:musics, :artists], nil], drop_join_table
end
def test_invert_create_join_table_with_table_name
- drop_table = @recorder.inverse_of :create_join_table, [:musics, :artists, table_name: :catalog]
- assert_equal [:drop_table, [:catalog]], drop_table
+ drop_join_table = @recorder.inverse_of :create_join_table, [:musics, :artists, table_name: :catalog]
+ assert_equal [:drop_join_table, [:musics, :artists, table_name: :catalog], nil], drop_join_table
+ end
+
+ def test_invert_drop_join_table
+ block = Proc.new{}
+ create_join_table = @recorder.inverse_of :drop_join_table, [:musics, :artists, table_name: :catalog], &block
+ assert_equal [:create_join_table, [:musics, :artists, table_name: :catalog], block], create_join_table
end
def test_invert_rename_table
View
42 activerecord/test/cases/migration/create_join_table_test.rb
@@ -78,6 +78,48 @@ def test_create_join_table_with_index
assert_equal [%w(artist_id music_id)], connection.indexes(:artists_musics).map(&:columns)
end
+
+ def test_drop_join_table
+ connection.create_join_table :artists, :musics
+ connection.drop_join_table :artists, :musics
+
+ assert !connection.tables.include?('artists_musics')
+ end
+
+ def test_drop_join_table_with_strings
+ connection.create_join_table :artists, :musics
+ connection.drop_join_table 'artists', 'musics'
+
+ assert !connection.tables.include?('artists_musics')
+ end
+
+ def test_drop_join_table_with_the_proper_order
+ connection.create_join_table :videos, :musics
+ connection.drop_join_table :videos, :musics
+
+ assert !connection.tables.include?('musics_videos')
+ end
+
+ def test_drop_join_table_with_the_table_name
+ connection.create_join_table :artists, :musics, table_name: :catalog
+ connection.drop_join_table :artists, :musics, table_name: :catalog
+
+ assert !connection.tables.include?('catalog')
+ end
+
+ def test_drop_join_table_with_the_table_name_as_string
+ connection.create_join_table :artists, :musics, table_name: 'catalog'
+ connection.drop_join_table :artists, :musics, table_name: 'catalog'
+
+ assert !connection.tables.include?('catalog')
+ end
+
+ def test_create_join_table_with_column_options
+ connection.create_join_table :artists, :musics, column_options: {null: true}
+ connection.drop_join_table :artists, :musics, column_options: {null: true}
+
+ assert !connection.tables.include?('artists_musics')
+ end
end
end
end

0 comments on commit bd155d2

Please sign in to comment.
Something went wrong with that request. Please try again.