Permalink
Browse files

Run BulkAlterTableMigrationsTest only when the adapter supports them

  • Loading branch information...
lifo committed Jan 31, 2011
1 parent 30176f2 commit 9666b6a625c9fef555c2a18df6721fed2000f131
Showing with 99 additions and 97 deletions.
  1. +99 −97 activerecord/test/cases/migration_test.rb
@@ -1923,141 +1923,143 @@ def with_change_table
end
end
- class AlterTableMigrationsTest < ActiveRecord::TestCase
- def setup
- @connection = Person.connection
- @connection.create_table(:delete_me, :force => true) {|t| }
- end
-
- def teardown
- Person.connection.drop_table(:delete_me) rescue nil
- end
+ if ActiveRecord::Base.connection.supports_bulk_alter?
+ class BulkAlterTableMigrationsTest < ActiveRecord::TestCase
+ def setup
+ @connection = Person.connection
+ @connection.create_table(:delete_me, :force => true) {|t| }
+ end
- def test_adding_multiple_columns
- assert_queries(1) do
- with_bulk_change_table do |t|
- t.column :name, :string
- t.string :qualification, :experience
- t.integer :age, :default => 0
- t.date :birthdate
- t.timestamps
- end
+ def teardown
+ Person.connection.drop_table(:delete_me) rescue nil
end
- assert_equal 8, columns.size
- [:name, :qualification, :experience].each {|s| assert_equal :string, column(s).type }
- assert_equal 0, column(:age).default
- end
+ def test_adding_multiple_columns
+ assert_queries(1) do
+ with_bulk_change_table do |t|
+ t.column :name, :string
+ t.string :qualification, :experience
+ t.integer :age, :default => 0
+ t.date :birthdate
+ t.timestamps
+ end
+ end
- def test_removing_columns
- with_bulk_change_table do |t|
- t.string :qualification, :experience
+ assert_equal 8, columns.size
+ [:name, :qualification, :experience].each {|s| assert_equal :string, column(s).type }
+ assert_equal 0, column(:age).default
end
- [:qualification, :experience].each {|c| assert column(c) }
-
- assert_queries(1) do
+ def test_removing_columns
with_bulk_change_table do |t|
- t.remove :qualification, :experience
- t.string :qualification_experience
+ t.string :qualification, :experience
end
- end
- [:qualification, :experience].each {|c| assert ! column(c) }
- assert column(:qualification_experience)
- end
+ [:qualification, :experience].each {|c| assert column(c) }
- def test_adding_indexes
- with_bulk_change_table do |t|
- t.string :username
- t.string :name
- t.integer :age
+ assert_queries(1) do
+ with_bulk_change_table do |t|
+ t.remove :qualification, :experience
+ t.string :qualification_experience
+ end
+ end
+
+ [:qualification, :experience].each {|c| assert ! column(c) }
+ assert column(:qualification_experience)
end
- # Adding an index fires a query everytime to check if an index already exists or not
- assert_queries(3) do
+ def test_adding_indexes
with_bulk_change_table do |t|
- t.index :username, :unique => true, :name => :awesome_username_index
- t.index [:name, :age]
+ t.string :username
+ t.string :name
+ t.integer :age
end
- end
- assert_equal 2, indexes.size
+ # Adding an index fires a query everytime to check if an index already exists or not
+ assert_queries(3) do
+ with_bulk_change_table do |t|
+ t.index :username, :unique => true, :name => :awesome_username_index
+ t.index [:name, :age]
+ end
+ end
- name_age_index = index(:index_delete_me_on_name_and_age)
- assert_equal ['name', 'age'].sort, name_age_index.columns.sort
- assert ! name_age_index.unique
+ assert_equal 2, indexes.size
- assert index(:awesome_username_index).unique
- end
+ name_age_index = index(:index_delete_me_on_name_and_age)
+ assert_equal ['name', 'age'].sort, name_age_index.columns.sort
+ assert ! name_age_index.unique
- def test_removing_index
- with_bulk_change_table do |t|
- t.string :name
- t.index :name
+ assert index(:awesome_username_index).unique
end
- assert index(:index_delete_me_on_name)
-
- assert_queries(3) do
+ def test_removing_index
with_bulk_change_table do |t|
- t.remove_index :name
- t.index :name, :name => :new_name_index, :unique => true
+ t.string :name
+ t.index :name
end
- end
- assert ! index(:index_delete_me_on_name)
+ assert index(:index_delete_me_on_name)
- new_name_index = index(:new_name_index)
- assert new_name_index.unique
- end
+ assert_queries(3) do
+ with_bulk_change_table do |t|
+ t.remove_index :name
+ t.index :name, :name => :new_name_index, :unique => true
+ end
+ end
- def test_changing_columns
- with_bulk_change_table do |t|
- t.string :name
- t.date :birthdate
- end
+ assert ! index(:index_delete_me_on_name)
- assert ! column(:name).default
- assert_equal :date, column(:birthdate).type
+ new_name_index = index(:new_name_index)
+ assert new_name_index.unique
+ end
- assert_queries(1) do
+ def test_changing_columns
with_bulk_change_table do |t|
- t.change :name, :string, :default => 'NONAME'
- t.change :birthdate, :datetime
+ t.string :name
+ t.date :birthdate
end
- end
- assert_equal 'NONAME', column(:name).default
- assert_equal :datetime, column(:birthdate).type
- end
+ assert ! column(:name).default
+ assert_equal :date, column(:birthdate).type
- protected
+ assert_queries(1) do
+ with_bulk_change_table do |t|
+ t.change :name, :string, :default => 'NONAME'
+ t.change :birthdate, :datetime
+ end
+ end
+
+ assert_equal 'NONAME', column(:name).default
+ assert_equal :datetime, column(:birthdate).type
+ end
- def with_bulk_change_table
- # Reset columns/indexes cache as we're changing the table
- @columns = @indexes = nil
+ protected
- Person.connection.change_table(:delete_me, :bulk => true) do |t|
- yield t
+ def with_bulk_change_table
+ # Reset columns/indexes cache as we're changing the table
+ @columns = @indexes = nil
+
+ Person.connection.change_table(:delete_me, :bulk => true) do |t|
+ yield t
+ end
end
- end
- def column(name)
- columns.detect {|c| c.name == name.to_s }
- end
+ def column(name)
+ columns.detect {|c| c.name == name.to_s }
+ end
- def columns
- @columns ||= Person.connection.columns('delete_me')
- end
+ def columns
+ @columns ||= Person.connection.columns('delete_me')
+ end
- def index(name)
- indexes.detect {|i| i.name == name.to_s }
- end
+ def index(name)
+ indexes.detect {|i| i.name == name.to_s }
+ end
- def indexes
- @indexes ||= Person.connection.indexes('delete_me')
- end
+ def indexes
+ @indexes ||= Person.connection.indexes('delete_me')
+ end
+ end # AlterTableMigrationsTest
end

0 comments on commit 9666b6a

Please sign in to comment.