1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
# frozen_string_literal: true
require "cases/helper"
require "cases/migration/helper"
class MultiDbMigratorTest < ActiveRecord::TestCase
# Use this class to sense if migrations have gone
# up or down.
class Sensor < ActiveRecord::Migration::Current
attr_reader :went_up, :went_down
def initialize(name = self.class.name, version = nil)
super
@went_up = false
@went_down = false
end
def up; @went_up = true; end
def down; @went_down = true; end
end
def setup
super
@pool_a = ActiveRecord::Base.connection_pool
@pool_b = ARUnit2Model.connection_pool
@pool_a.schema_migration.create_table
@pool_b.schema_migration.create_table
@pool_a.schema_migration.delete_all_versions rescue nil
@pool_b.schema_migration.delete_all_versions rescue nil
@path_a = MIGRATIONS_ROOT + "/valid"
@path_b = MIGRATIONS_ROOT + "/to_copy"
@schema_migration_a = @pool_a.schema_migration
@internal_metadata_a = @pool_a.internal_metadata
@migrations_a = ActiveRecord::MigrationContext.new(@path_a, @schema_migration_a, @internal_metadata_a).migrations
@schema_migration_b = @pool_b.schema_migration
@internal_metadata_b = @pool_b.internal_metadata
@migrations_b = ActiveRecord::MigrationContext.new(@path_b, @schema_migration_b, @internal_metadata_b).migrations
@migrations_a_list = [[1, "ValidPeopleHaveLastNames"], [2, "WeNeedReminders"], [3, "InnocentJointable"]]
@migrations_b_list = [[1, "PeopleHaveHobbies"], [2, "PeopleHaveDescriptions"]]
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.message_count = 0
ActiveRecord::Migration.class_eval do
undef :puts
def puts(*)
ActiveRecord::Migration.message_count += 1
end
end
end
teardown do
@pool_q.schema_migration.delete_all_versions rescue nil
@pool_b.schema_migration.delete_all_versions rescue nil
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::Migration.class_eval do
undef :puts
def puts(*)
super
end
end
end
def test_schema_migration_is_different_for_different_connections
assert_not_equal @schema_migration_a, @schema_migration_b
assert_not_equal @schema_migration_a.instance_variable_get(:@pool), @schema_migration_b.instance_variable_get(:@pool)
assert_equal "ActiveRecord::Base", @pool_a.pool_config.connection_name
assert_equal "ARUnit2Model", @pool_b.pool_config.connection_name
end
def test_finds_migrations
@migrations_a_list.each_with_index do |pair, i|
assert_equal @migrations_a[i].version, pair.first
assert_equal @migrations_a[i].name, pair.last
end
@migrations_b_list.each_with_index do |pair, i|
assert_equal @migrations_b[i].version, pair.first
assert_equal @migrations_b[i].name, pair.last
end
end
def test_migrations_status
@schema_migration_a.create_version(2)
@schema_migration_a.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 **********"],
], ActiveRecord::MigrationContext.new(@path_a, @schema_migration_a, @internal_metadata_a).migrations_status
@schema_migration_b.create_version(4)
assert_equal [
["down", "001", "People have hobbies"],
["down", "002", "People have descriptions"],
["up", "004", "********** NO FILE **********"]
], ActiveRecord::MigrationContext.new(@path_b, @schema_migration_b, @internal_metadata_b).migrations_status
end
def test_get_all_versions
_, migrator_a = migrator_class(3)
migrator_a = migrator_a.new(@path_a, @schema_migration_a)
migrator_a.migrate
assert_equal([1, 2, 3], migrator_a.get_all_versions)
migrator_a.rollback
assert_equal([1, 2], migrator_a.get_all_versions)
migrator_a.rollback
assert_equal([1], migrator_a.get_all_versions)
migrator_a.rollback
assert_equal([], migrator_a.get_all_versions)
_, migrator_b = migrator_class(2)
migrator_b = migrator_b.new(@path_b, @schema_migration_b)
migrator_b.migrate
assert_equal([1, 2], migrator_b.get_all_versions)
migrator_b.rollback
assert_equal([1], migrator_b.get_all_versions)
migrator_b.rollback
assert_equal([], migrator_b.get_all_versions)
end
def test_finds_pending_migrations
@schema_migration_a.create_version("1")
migration_list_a = [ActiveRecord::Migration.new("foo", 1), ActiveRecord::Migration.new("bar", 3)]
migrations_a = ActiveRecord::Migrator.new(:up, migration_list_a, @schema_migration_a, @internal_metadata_a).pending_migrations
assert_equal 1, migrations_a.size
assert_equal migration_list_a.last, migrations_a.first
@schema_migration_b.create_version("1")
migration_list_b = [ActiveRecord::Migration.new("foo", 1), ActiveRecord::Migration.new("bar", 3)]
migrations_b = ActiveRecord::Migrator.new(:up, migration_list_b, @schema_migration_b, @internal_metadata_b).pending_migrations
assert_equal 1, migrations_b.size
assert_equal migration_list_b.last, migrations_b.first
end
def test_migrator_db_has_no_schema_migrations_table
_, migrator = migrator_class(3)
migrator = migrator.new(@path_a, @schema_migration_a, @internal_metadata_a)
@schema_migration_a.drop_table
assert_not @pool_a.lease_connection.table_exists?("schema_migrations")
migrator.migrate(1)
assert @pool_a.lease_connection.table_exists?("schema_migrations")
migrator.rollback
_, migrator = migrator_class(3)
migrator = migrator.new(@path_b, @schema_migration_b, @internal_metadata_b)
@schema_migration_b.drop_table
assert_not @pool_b.lease_connection.table_exists?("schema_migrations")
migrator.migrate(1)
assert @pool_b.lease_connection.table_exists?("schema_migrations")
migrator.rollback
end
def test_migrator_forward
_, migrator = migrator_class(3)
migrator = migrator.new(@path_a, @schema_migration_a, @internal_metadata_a)
migrator.migrate(1)
assert_equal(1, migrator.current_version)
migrator.forward(2)
assert_equal(3, migrator.current_version)
migrator.forward
assert_equal(3, migrator.current_version)
_, migrator_b = migrator_class(3)
migrator_b = migrator_b.new(@path_b, @schema_migration_b, @internal_metadata_b)
migrator_b.migrate(1)
assert_equal(1, migrator_b.current_version)
migrator_b.forward(2)
assert_equal(3, migrator_b.current_version)
migrator_b.forward
assert_equal(3, migrator_b.current_version)
end
def test_internal_metadata_stores_environment
current_env = ActiveRecord::Base.lease_connection.pool.db_config.env_name
migrations_path = MIGRATIONS_ROOT + "/valid"
migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration_b, @internal_metadata_b)
migrator.up
assert_equal current_env, @internal_metadata_b[:environment]
ensure
migrator.down if migrator
end
private
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
def sensors(count)
calls = []
migrations = count.times.map { |i|
m(nil, i + 1) { |c, migration|
calls << [c, migration.version]
}
}
[calls, migrations]
end
def migrator_class(count)
calls, migrations = sensors(count)
migrator = Class.new(ActiveRecord::MigrationContext) {
define_method(:migrations) { |*|
migrations
}
}
[calls, migrator]
end
end
|