diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index dbf8ca319c7b4..d9c42ea06c087 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -245,61 +245,6 @@ def test_only_loads_pending_migrations assert names.include?('InnocentJointable') end - def test_migrator_db_has_no_schema_migrations_table - # Oracle adapter raises error if semicolon is present as last character - if current_adapter?(:OracleAdapter) - ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations") - else - ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations;") - end - assert_nothing_raised do - ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 1) - end - end - - def test_migrator_going_down_due_to_version_target - ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", 1) - ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 0) - - assert !Person.column_methods_hash.include?(:last_name) - assert !Reminder.table_exists? - - ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid") - - Person.reset_column_information - assert Person.column_methods_hash.include?(:last_name) - assert Reminder.create("content" => "hello world", "remind_at" => Time.now) - assert_equal "hello world", Reminder.find(:first).content - end - - def test_migrator_rollback - ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid") - assert_equal(3, ActiveRecord::Migrator.current_version) - - ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") - assert_equal(2, ActiveRecord::Migrator.current_version) - - ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") - assert_equal(1, ActiveRecord::Migrator.current_version) - - ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") - assert_equal(0, ActiveRecord::Migrator.current_version) - - ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") - assert_equal(0, ActiveRecord::Migrator.current_version) - end - - def test_migrator_forward - ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 1) - assert_equal(1, ActiveRecord::Migrator.current_version) - - ActiveRecord::Migrator.forward(MIGRATIONS_ROOT + "/valid", 2) - assert_equal(3, ActiveRecord::Migrator.current_version) - - ActiveRecord::Migrator.forward(MIGRATIONS_ROOT + "/valid") - assert_equal(3, ActiveRecord::Migrator.current_version) - end - def test_get_all_versions ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid") assert_equal([1,2,3], ActiveRecord::Migrator.get_all_versions) diff --git a/activerecord/test/cases/migrator_test.rb b/activerecord/test/cases/migrator_test.rb index 01343b84c4dae..1f2537e098f25 100644 --- a/activerecord/test/cases/migrator_test.rb +++ b/activerecord/test/cases/migrator_test.rb @@ -160,34 +160,34 @@ def test_migrator_one_up calls, migrations = sensors(3) ActiveRecord::Migrator.new(:up, migrations, 1).migrate - assert_equal [[:up, 0], [:up, 1]], calls + assert_equal [[:up, 1], [:up, 2]], calls calls.clear ActiveRecord::Migrator.new(:up, migrations, 2).migrate - assert_equal [[:up, 2]], calls + assert_equal [[:up, 3]], calls end def test_migrator_one_down calls, migrations = sensors(3) ActiveRecord::Migrator.new(:up, migrations).migrate - assert_equal [[:up, 0], [:up, 1], [:up, 2]], calls + assert_equal [[:up, 1], [:up, 2], [:up, 3]], calls calls.clear ActiveRecord::Migrator.new(:down, migrations, 1).migrate - assert_equal [[:down, 2]], calls + assert_equal [[:down, 3]], calls end def test_migrator_one_up_one_down calls, migrations = sensors(3) ActiveRecord::Migrator.new(:up, migrations, 1).migrate - assert_equal [[:up, 0], [:up, 1]], calls + assert_equal [[:up, 1], [:up, 2]], calls calls.clear ActiveRecord::Migrator.new(:down, migrations, 0).migrate - assert_equal [[:down, 1]], calls + assert_equal [[:down, 2]], calls end def test_migrator_double_up @@ -195,7 +195,7 @@ def test_migrator_double_up assert_equal(0, ActiveRecord::Migrator.current_version) ActiveRecord::Migrator.new(:up, migrations, 1).migrate - assert_equal [[:up, 0], [:up, 1]], calls + assert_equal [[:up, 1], [:up, 2]], calls calls.clear ActiveRecord::Migrator.new(:up, migrations, 1).migrate @@ -208,11 +208,11 @@ def test_migrator_double_down assert_equal(0, ActiveRecord::Migrator.current_version) ActiveRecord::Migrator.new(:up, migrations, 1).run - assert_equal [[:up, 1]], calls + assert_equal [[:up, 2]], calls calls.clear ActiveRecord::Migrator.new(:down, migrations, 1).run - assert_equal [[:down, 1]], calls + assert_equal [[:down, 2]], calls calls.clear ActiveRecord::Migrator.new(:down, migrations, 1).run @@ -250,12 +250,12 @@ def test_target_version_zero_should_run_only_once # migrate up to 1 ActiveRecord::Migrator.new(:up, migrations, 1).migrate - assert_equal [[:up, 0], [:up, 1]], calls + assert_equal [[:up, 1], [:up, 2]], calls calls.clear # migrate down to 0 ActiveRecord::Migrator.new(:down, migrations, 0).migrate - assert_equal [[:down, 1]], calls + assert_equal [[:down, 2]], calls calls.clear # migrate down to 0 again @@ -263,6 +263,61 @@ def test_target_version_zero_should_run_only_once assert_equal [], calls end + def test_migrator_going_down_due_to_version_target + calls, migrator = migrator_class(3) + + migrator.up("valid", 1) + assert_equal [[:up, 1], [:up, 2]], calls + calls.clear + + migrator.migrate("valid", 0) + assert_equal [[:down, 2]], calls + calls.clear + + migrator.migrate("valid") + assert_equal [[:up, 2], [:up, 3]], calls + end + + def test_migrator_rollback + _, migrator = migrator_class(4) + + migrator.migrate("valid") + assert_equal(3, ActiveRecord::Migrator.current_version) + + migrator.rollback("valid") + assert_equal(2, ActiveRecord::Migrator.current_version) + + migrator.rollback("valid") + assert_equal(1, ActiveRecord::Migrator.current_version) + + migrator.rollback("valid") + assert_equal(0, ActiveRecord::Migrator.current_version) + + migrator.rollback("valid") + assert_equal(0, ActiveRecord::Migrator.current_version) + end + + def test_migrator_db_has_no_schema_migrations_table + _, migrator = migrator_class(3) + + ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations") + refute ActiveRecord::Base.connection.table_exists?('schema_migrations') + migrator.migrate("valid", 1) + assert ActiveRecord::Base.connection.table_exists?('schema_migrations') + end + + def test_migrator_forward + _, migrator = migrator_class(3) + migrator.migrate("/valid", 1) + assert_equal(1, ActiveRecord::Migrator.current_version) + + migrator.forward("/valid", 2) + assert_equal(3, ActiveRecord::Migrator.current_version) + + migrator.forward("/valid") + assert_equal(3, ActiveRecord::Migrator.current_version) + end + private def m(name, version, &block) x = Sensor.new name, version @@ -276,10 +331,21 @@ def sensors(count) calls = [] migrations = count.times.map { |i| m(nil, i) { |c,migration| - calls << [c, migration.version] + calls << [c, migration.version + 1] } } [calls, migrations] end + + def migrator_class(count) + calls, migrations = sensors(count) + + migrator = Class.new(Migrator).extend(Module.new { + define_method(:migrations) { |paths| + migrations + } + }) + [calls, migrator] + end end end