2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-01-12 18:29:16 -05:00
|
|
|
require "cases/helper"
|
2012-01-16 14:21:20 -05:00
|
|
|
require "cases/migration/helper"
|
2012-01-12 18:29:16 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
class MigratorTest < ActiveRecord::TestCase
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2014-08-28 17:31:31 -04:00
|
|
|
|
|
|
|
# Use this class to sense if migrations have gone
|
|
|
|
# up or down.
|
2015-12-05 15:14:22 -05:00
|
|
|
class Sensor < ActiveRecord::Migration::Current
|
2014-08-28 17:31:31 -04:00
|
|
|
attr_reader :went_up, :went_down
|
2012-01-13 17:13:31 -05:00
|
|
|
|
2016-08-06 14:20:22 -04:00
|
|
|
def initialize(name = self.class.name, version = nil)
|
2012-01-13 17:13:31 -05:00
|
|
|
super
|
2016-10-28 23:05:58 -04:00
|
|
|
@went_up = false
|
2014-08-28 17:31:31 -04:00
|
|
|
@went_down = false
|
2012-01-13 17:13:31 -05:00
|
|
|
end
|
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def up; @went_up = true; end
|
|
|
|
def down; @went_down = true; end
|
|
|
|
end
|
2012-01-13 17:13:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def setup
|
|
|
|
super
|
|
|
|
ActiveRecord::SchemaMigration.create_table
|
|
|
|
ActiveRecord::SchemaMigration.delete_all rescue nil
|
2014-08-28 20:27:26 -04:00
|
|
|
@verbose_was = ActiveRecord::Migration.verbose
|
2014-08-28 21:36:27 -04:00
|
|
|
ActiveRecord::Migration.message_count = 0
|
2014-08-28 20:40:33 -04:00
|
|
|
ActiveRecord::Migration.class_eval do
|
|
|
|
undef :puts
|
|
|
|
def puts(*)
|
|
|
|
ActiveRecord::Migration.message_count += 1
|
|
|
|
end
|
|
|
|
end
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-12 18:29:16 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
teardown do
|
|
|
|
ActiveRecord::SchemaMigration.delete_all rescue nil
|
2014-08-28 20:27:26 -04:00
|
|
|
ActiveRecord::Migration.verbose = @verbose_was
|
2014-08-28 20:40:33 -04:00
|
|
|
ActiveRecord::Migration.class_eval do
|
|
|
|
undef :puts
|
|
|
|
def puts(*)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-12 18:29:16 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_with_duplicate_names
|
2017-01-24 19:46:29 -05:00
|
|
|
e = assert_raises(ActiveRecord::DuplicateMigrationNameError) do
|
2016-08-06 12:26:20 -04:00
|
|
|
list = [ActiveRecord::Migration.new("Chunky"), ActiveRecord::Migration.new("Chunky")]
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, list)
|
2012-01-12 18:29:16 -05:00
|
|
|
end
|
2017-01-24 19:46:29 -05:00
|
|
|
assert_match(/Multiple migrations have the name Chunky/, e.message)
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-12 18:46:04 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_with_duplicate_versions
|
|
|
|
assert_raises(ActiveRecord::DuplicateMigrationVersionError) do
|
2016-08-06 12:26:20 -04:00
|
|
|
list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 1)]
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, list)
|
|
|
|
end
|
|
|
|
end
|
2012-01-12 18:46:04 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_with_missing_version_numbers
|
|
|
|
assert_raises(ActiveRecord::UnknownMigrationVersionError) do
|
2016-08-06 12:26:20 -04:00
|
|
|
list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, list, 3).run
|
2012-01-12 18:46:04 -05:00
|
|
|
end
|
2017-09-25 17:10:17 -04:00
|
|
|
|
|
|
|
assert_raises(ActiveRecord::UnknownMigrationVersionError) do
|
|
|
|
list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
|
|
|
|
ActiveRecord::Migrator.new(:up, list, -1).run
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises(ActiveRecord::UnknownMigrationVersionError) do
|
|
|
|
list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
|
|
|
|
ActiveRecord::Migrator.new(:up, list, 0).run
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises(ActiveRecord::UnknownMigrationVersionError) do
|
|
|
|
list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
|
|
|
|
ActiveRecord::Migrator.new(:up, list, 3).migrate
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises(ActiveRecord::UnknownMigrationVersionError) do
|
|
|
|
list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
|
|
|
|
ActiveRecord::Migrator.new(:up, list, -1).migrate
|
|
|
|
end
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-12 18:46:04 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_finds_migrations
|
2018-01-10 10:25:13 -05:00
|
|
|
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid").migrations
|
2012-01-12 18:46:04 -05:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
[[1, "ValidPeopleHaveLastNames"], [2, "WeNeedReminders"], [3, "InnocentJointable"]].each_with_index do |pair, i|
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal migrations[i].version, pair.first
|
|
|
|
assert_equal migrations[i].name, pair.last
|
2012-01-12 18:46:04 -05:00
|
|
|
end
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-12 18:46:04 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_finds_migrations_in_subdirectories
|
2018-01-10 10:25:13 -05:00
|
|
|
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid_with_subdirectories").migrations
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
[[1, "ValidPeopleHaveLastNames"], [2, "WeNeedReminders"], [3, "InnocentJointable"]].each_with_index do |pair, i|
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal migrations[i].version, pair.first
|
|
|
|
assert_equal migrations[i].name, pair.last
|
2012-12-12 15:51:38 -05:00
|
|
|
end
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-12-12 15:51:38 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_finds_migrations_from_two_directories
|
2016-08-06 12:26:20 -04:00
|
|
|
directories = [MIGRATIONS_ROOT + "/valid_with_timestamps", MIGRATIONS_ROOT + "/to_copy_with_timestamps"]
|
2018-01-10 10:25:13 -05:00
|
|
|
migrations = ActiveRecord::MigrationContext.new(directories).migrations
|
2014-08-28 17:31:31 -04:00
|
|
|
|
|
|
|
[[20090101010101, "PeopleHaveHobbies"],
|
|
|
|
[20090101010202, "PeopleHaveDescriptions"],
|
|
|
|
[20100101010101, "ValidWithTimestampsPeopleHaveLastNames"],
|
|
|
|
[20100201010101, "ValidWithTimestampsWeNeedReminders"],
|
|
|
|
[20100301010101, "ValidWithTimestampsInnocentJointable"]].each_with_index do |pair, i|
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_equal pair.first, migrations[i].version
|
|
|
|
assert_equal pair.last, migrations[i].name
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
|
|
|
end
|
2012-01-12 18:53:47 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_finds_migrations_in_numbered_directory
|
2018-01-10 10:25:13 -05:00
|
|
|
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/10_urban").migrations
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal 9, migrations[0].version
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "AddExpressions", migrations[0].name
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_relative_migrations
|
|
|
|
list = Dir.chdir(MIGRATIONS_ROOT) do
|
2018-01-10 10:25:13 -05:00
|
|
|
ActiveRecord::MigrationContext.new("valid").migrations
|
2012-01-12 18:53:47 -05:00
|
|
|
end
|
2012-01-13 13:30:52 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
migration_proxy = list.find { |item|
|
2016-08-06 12:26:20 -04:00
|
|
|
item.name == "ValidPeopleHaveLastNames"
|
2014-08-28 17:31:31 -04:00
|
|
|
}
|
2016-08-06 12:26:20 -04:00
|
|
|
assert migration_proxy, "should find pending migration"
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-13 13:30:52 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_finds_pending_migrations
|
2016-08-06 13:37:57 -04:00
|
|
|
ActiveRecord::SchemaMigration.create!(version: "1")
|
2016-08-06 12:26:20 -04:00
|
|
|
migration_list = [ActiveRecord::Migration.new("foo", 1), ActiveRecord::Migration.new("bar", 3)]
|
2014-08-28 17:31:31 -04:00
|
|
|
migrations = ActiveRecord::Migrator.new(:up, migration_list).pending_migrations
|
2012-01-13 17:13:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal 1, migrations.size
|
|
|
|
assert_equal migration_list.last, migrations.first
|
|
|
|
end
|
2012-01-13 17:13:31 -05:00
|
|
|
|
2016-05-28 00:46:59 -04:00
|
|
|
def test_migrations_status
|
|
|
|
path = MIGRATIONS_ROOT + "/valid"
|
|
|
|
|
|
|
|
ActiveRecord::SchemaMigration.create(version: 2)
|
|
|
|
ActiveRecord::SchemaMigration.create(version: 10)
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
["down", "001", "Valid people have last names"],
|
|
|
|
["up", "002", "We need reminders"],
|
|
|
|
["down", "003", "Innocent jointable"],
|
|
|
|
["up", "010", "********** NO FILE **********"],
|
2018-01-10 10:25:13 -05:00
|
|
|
], ActiveRecord::MigrationContext.new(path).migrations_status
|
2016-05-28 00:46:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_migrations_status_in_subdirectories
|
|
|
|
path = MIGRATIONS_ROOT + "/valid_with_subdirectories"
|
|
|
|
|
|
|
|
ActiveRecord::SchemaMigration.create(version: 2)
|
|
|
|
ActiveRecord::SchemaMigration.create(version: 10)
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
["down", "001", "Valid people have last names"],
|
|
|
|
["up", "002", "We need reminders"],
|
|
|
|
["down", "003", "Innocent jointable"],
|
|
|
|
["up", "010", "********** NO FILE **********"],
|
2018-01-10 10:25:13 -05:00
|
|
|
], ActiveRecord::MigrationContext.new(path).migrations_status
|
2016-05-28 00:46:59 -04:00
|
|
|
end
|
|
|
|
|
2017-03-04 10:29:41 -05:00
|
|
|
def test_migrations_status_with_schema_define_in_subdirectories
|
2018-01-18 21:51:07 -05:00
|
|
|
path = MIGRATIONS_ROOT + "/valid_with_subdirectories"
|
|
|
|
prev_paths = ActiveRecord::Migrator.migrations_paths
|
|
|
|
ActiveRecord::Migrator.migrations_paths = path
|
2017-03-04 10:29:41 -05:00
|
|
|
|
2018-01-18 21:51:07 -05:00
|
|
|
ActiveRecord::Schema.define(version: 3) do
|
|
|
|
end
|
2017-03-04 10:29:41 -05:00
|
|
|
|
|
|
|
assert_equal [
|
2018-01-18 21:51:07 -05:00
|
|
|
["up", "001", "Valid people have last names"],
|
|
|
|
["up", "002", "We need reminders"],
|
|
|
|
["up", "003", "Innocent jointable"],
|
|
|
|
], ActiveRecord::MigrationContext.new(path).migrations_status
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Migrator.migrations_paths = prev_paths
|
2017-03-04 10:29:41 -05:00
|
|
|
end
|
|
|
|
|
2016-05-28 00:46:59 -04:00
|
|
|
def test_migrations_status_from_two_directories
|
|
|
|
paths = [MIGRATIONS_ROOT + "/valid_with_timestamps", MIGRATIONS_ROOT + "/to_copy_with_timestamps"]
|
|
|
|
|
|
|
|
ActiveRecord::SchemaMigration.create(version: "20100101010101")
|
|
|
|
ActiveRecord::SchemaMigration.create(version: "20160528010101")
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
["down", "20090101010101", "People have hobbies"],
|
|
|
|
["down", "20090101010202", "People have descriptions"],
|
|
|
|
["up", "20100101010101", "Valid with timestamps people have last names"],
|
|
|
|
["down", "20100201010101", "Valid with timestamps we need reminders"],
|
|
|
|
["down", "20100301010101", "Valid with timestamps innocent jointable"],
|
|
|
|
["up", "20160528010101", "********** NO FILE **********"],
|
2018-01-10 10:25:13 -05:00
|
|
|
], ActiveRecord::MigrationContext.new(paths).migrations_status
|
2016-05-28 00:46:59 -04:00
|
|
|
end
|
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_interleaved_migrations
|
2016-08-06 12:26:20 -04:00
|
|
|
pass_one = [Sensor.new("One", 1)]
|
2012-01-13 17:13:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, pass_one).migrate
|
|
|
|
assert pass_one.first.went_up
|
|
|
|
assert_not pass_one.first.went_down
|
2012-01-13 17:13:31 -05:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
pass_two = [Sensor.new("One", 1), Sensor.new("Three", 3)]
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, pass_two).migrate
|
|
|
|
assert_not pass_two[0].went_up
|
|
|
|
assert pass_two[1].went_up
|
|
|
|
assert pass_two.all? { |x| !x.went_down }
|
2012-01-13 17:13:31 -05:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
pass_three = [Sensor.new("One", 1),
|
|
|
|
Sensor.new("Two", 2),
|
|
|
|
Sensor.new("Three", 3)]
|
2012-01-16 13:33:39 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:down, pass_three).migrate
|
|
|
|
assert pass_three[0].went_down
|
|
|
|
assert_not pass_three[1].went_down
|
|
|
|
assert pass_three[2].went_down
|
|
|
|
end
|
2012-01-16 13:33:39 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_up_calls_up
|
|
|
|
migrations = [Sensor.new(nil, 0), Sensor.new(nil, 1), Sensor.new(nil, 2)]
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = ActiveRecord::Migrator.new(:up, migrations)
|
|
|
|
migrator.migrate
|
2014-10-27 12:28:53 -04:00
|
|
|
assert migrations.all?(&:went_up)
|
2014-08-28 17:31:31 -04:00
|
|
|
assert migrations.all? { |m| !m.went_down }
|
2018-01-10 10:25:13 -05:00
|
|
|
assert_equal 2, migrator.current_version
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-16 13:33:39 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_down_calls_down
|
|
|
|
test_up_calls_up
|
2012-01-16 13:33:39 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
migrations = [Sensor.new(nil, 0), Sensor.new(nil, 1), Sensor.new(nil, 2)]
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = ActiveRecord::Migrator.new(:down, migrations)
|
|
|
|
migrator.migrate
|
2014-08-28 17:31:31 -04:00
|
|
|
assert migrations.all? { |m| !m.went_up }
|
2014-10-27 12:28:53 -04:00
|
|
|
assert migrations.all?(&:went_down)
|
2018-01-10 10:25:13 -05:00
|
|
|
assert_equal 0, migrator.current_version
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_current_version
|
2016-08-06 13:37:57 -04:00
|
|
|
ActiveRecord::SchemaMigration.create!(version: "1000")
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = ActiveRecord::MigrationContext.new("db/migrate")
|
|
|
|
assert_equal 1000, migrator.current_version
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_one_up
|
|
|
|
calls, migrations = sensors(3)
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, migrations, 1).migrate
|
|
|
|
assert_equal [[:up, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, migrations, 2).migrate
|
|
|
|
assert_equal [[:up, 2]], calls
|
|
|
|
end
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_one_down
|
|
|
|
calls, migrations = sensors(3)
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, migrations).migrate
|
|
|
|
assert_equal [[:up, 1], [:up, 2], [:up, 3]], calls
|
|
|
|
calls.clear
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:down, migrations, 1).migrate
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [[:down, 3], [:down, 2]], calls
|
|
|
|
end
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_one_up_one_down
|
|
|
|
calls, migrations = sensors(3)
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, migrations, 1).migrate
|
|
|
|
assert_equal [[:up, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:down, migrations, 0).migrate
|
|
|
|
assert_equal [[:down, 1]], calls
|
|
|
|
end
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_double_up
|
|
|
|
calls, migrations = sensors(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = ActiveRecord::Migrator.new(:up, migrations, 1)
|
|
|
|
assert_equal(0, migrator.current_version)
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.migrate
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [[:up, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.migrate
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [], calls
|
|
|
|
end
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_double_down
|
|
|
|
calls, migrations = sensors(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = ActiveRecord::Migrator.new(:up, migrations, 1)
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
assert_equal 0, migrator.current_version
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.run
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [[:up, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = ActiveRecord::Migrator.new(:down, migrations, 1)
|
|
|
|
migrator.run
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [[:down, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.run
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [], calls
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
assert_equal 0, migrator.current_version
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-16 14:28:00 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_verbosity
|
|
|
|
_, migrations = sensors(3)
|
2012-01-16 14:28:00 -05:00
|
|
|
|
2017-04-25 18:01:53 -04:00
|
|
|
ActiveRecord::Migration.verbose = true
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:up, migrations, 1).migrate
|
|
|
|
assert_not_equal 0, ActiveRecord::Migration.message_count
|
2012-01-16 14:28:00 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migration.message_count = 0
|
2012-01-16 14:28:00 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migrator.new(:down, migrations, 0).migrate
|
|
|
|
assert_not_equal 0, ActiveRecord::Migration.message_count
|
|
|
|
end
|
2012-01-16 14:28:00 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_verbosity_off
|
|
|
|
_, migrations = sensors(3)
|
2012-01-16 14:28:00 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
ActiveRecord::Migration.verbose = false
|
|
|
|
ActiveRecord::Migrator.new(:up, migrations, 1).migrate
|
|
|
|
assert_equal 0, ActiveRecord::Migration.message_count
|
|
|
|
ActiveRecord::Migrator.new(:down, migrations, 0).migrate
|
|
|
|
assert_equal 0, ActiveRecord::Migration.message_count
|
|
|
|
end
|
2012-01-16 16:58:32 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_target_version_zero_should_run_only_once
|
|
|
|
calls, migrations = sensors(3)
|
2012-01-16 16:58:32 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
# migrate up to 1
|
|
|
|
ActiveRecord::Migrator.new(:up, migrations, 1).migrate
|
|
|
|
assert_equal [[:up, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 16:58:32 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
# migrate down to 0
|
|
|
|
ActiveRecord::Migrator.new(:down, migrations, 0).migrate
|
|
|
|
assert_equal [[:down, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 16:58:32 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
# migrate down to 0 again
|
|
|
|
ActiveRecord::Migrator.new(:down, migrations, 0).migrate
|
|
|
|
assert_equal [], calls
|
|
|
|
end
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_going_down_due_to_version_target
|
|
|
|
calls, migrator = migrator_class(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = migrator.new("valid")
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.up(1)
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [[:up, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.migrate(0)
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [[:down, 1]], calls
|
|
|
|
calls.clear
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.migrate
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [[:up, 1], [:up, 2], [:up, 3]], calls
|
|
|
|
end
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2017-01-07 06:09:34 -05:00
|
|
|
def test_migrator_output_when_running_multiple_migrations
|
2017-01-06 14:30:42 -05:00
|
|
|
_, migrator = migrator_class(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = migrator.new("valid")
|
2017-01-06 14:30:42 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
result = migrator.migrate
|
2017-01-06 14:30:42 -05:00
|
|
|
assert_equal(3, result.count)
|
|
|
|
|
|
|
|
# Nothing migrated from duplicate run
|
2018-01-10 10:25:13 -05:00
|
|
|
result = migrator.migrate
|
2017-01-06 14:30:42 -05:00
|
|
|
assert_equal(0, result.count)
|
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
result = migrator.rollback
|
2017-01-06 14:30:42 -05:00
|
|
|
assert_equal(1, result.count)
|
|
|
|
end
|
|
|
|
|
2017-01-07 06:09:34 -05:00
|
|
|
def test_migrator_output_when_running_single_migration
|
|
|
|
_, migrator = migrator_class(1)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = migrator.new("valid")
|
|
|
|
|
|
|
|
result = migrator.run(:up, 1)
|
2017-01-07 06:09:34 -05:00
|
|
|
|
|
|
|
assert_equal(1, result.version)
|
|
|
|
end
|
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_rollback
|
|
|
|
_, migrator = migrator_class(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = migrator.new("valid")
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.migrate
|
|
|
|
assert_equal(3, migrator.current_version)
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.rollback
|
|
|
|
assert_equal(2, migrator.current_version)
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.rollback
|
|
|
|
assert_equal(1, migrator.current_version)
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.rollback
|
|
|
|
assert_equal(0, migrator.current_version)
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.rollback
|
|
|
|
assert_equal(0, migrator.current_version)
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_db_has_no_schema_migrations_table
|
|
|
|
_, migrator = migrator_class(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = migrator.new("valid")
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2019-06-11 06:57:38 -04:00
|
|
|
ActiveRecord::SchemaMigration.drop_table
|
|
|
|
assert_not_predicate ActiveRecord::SchemaMigration, :table_exists?
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.migrate(1)
|
2019-06-11 06:57:38 -04:00
|
|
|
assert_predicate ActiveRecord::SchemaMigration, :table_exists?
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_migrator_forward
|
|
|
|
_, migrator = migrator_class(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = migrator.new("/valid")
|
|
|
|
migrator.migrate(1)
|
|
|
|
assert_equal(1, migrator.current_version)
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.forward(2)
|
|
|
|
assert_equal(3, migrator.current_version)
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.forward
|
|
|
|
assert_equal(3, migrator.current_version)
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
2012-01-16 18:23:16 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_only_loads_pending_migrations
|
|
|
|
# migrate up to 1
|
2016-08-06 13:37:57 -04:00
|
|
|
ActiveRecord::SchemaMigration.create!(version: "1")
|
2012-01-16 18:23:16 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
calls, migrator = migrator_class(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = migrator.new("valid")
|
|
|
|
migrator.migrate
|
2012-01-16 18:23:16 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
assert_equal [[:up, 2], [:up, 3]], calls
|
|
|
|
end
|
2012-01-16 18:24:27 -05:00
|
|
|
|
2014-08-28 17:31:31 -04:00
|
|
|
def test_get_all_versions
|
|
|
|
_, migrator = migrator_class(3)
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = migrator.new("valid")
|
2012-01-16 18:24:27 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.migrate
|
|
|
|
assert_equal([1, 2, 3], migrator.get_all_versions)
|
2012-01-16 18:24:27 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.rollback
|
|
|
|
assert_equal([1, 2], migrator.get_all_versions)
|
2012-01-16 18:24:27 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.rollback
|
|
|
|
assert_equal([1], migrator.get_all_versions)
|
2012-01-16 18:24:27 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator.rollback
|
|
|
|
assert_equal([], migrator.get_all_versions)
|
2014-08-28 17:31:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def m(name, version)
|
|
|
|
x = Sensor.new name, version
|
|
|
|
x.extend(Module.new {
|
|
|
|
define_method(:up) { yield(:up, x); super() }
|
|
|
|
define_method(:down) { yield(:down, x); super() }
|
|
|
|
}) if block_given?
|
|
|
|
end
|
2012-01-16 13:57:31 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def sensors(count)
|
|
|
|
calls = []
|
|
|
|
migrations = count.times.map { |i|
|
2016-10-28 23:05:58 -04:00
|
|
|
m(nil, i + 1) { |c, migration|
|
2016-08-06 13:55:02 -04:00
|
|
|
calls << [c, migration.version]
|
|
|
|
}
|
2012-01-16 13:57:31 -05:00
|
|
|
}
|
2016-08-06 13:55:02 -04:00
|
|
|
[calls, migrations]
|
|
|
|
end
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def migrator_class(count)
|
|
|
|
calls, migrations = sensors(count)
|
2012-01-16 18:05:55 -05:00
|
|
|
|
2018-01-10 10:25:13 -05:00
|
|
|
migrator = Class.new(ActiveRecord::MigrationContext) {
|
|
|
|
define_method(:migrations) { |*|
|
2016-08-06 13:55:02 -04:00
|
|
|
migrations
|
|
|
|
}
|
2018-01-10 10:25:13 -05:00
|
|
|
}
|
2016-08-06 13:55:02 -04:00
|
|
|
[calls, migrator]
|
|
|
|
end
|
2012-01-12 18:29:16 -05:00
|
|
|
end
|