diff options
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 196 |
1 files changed, 99 insertions, 97 deletions
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index c2a80b02b6..6f0f73e3bd 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -1923,141 +1923,143 @@ if ActiveRecord::Base.connection.supports_migrations? 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 |