1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #38684 from eileencodes/refactor-schema-migration

Refactor schema migration on connection
This commit is contained in:
Eileen M. Uchitelle 2020-03-09 12:05:10 -04:00 committed by GitHub
commit 9519c1aadc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 11 deletions

View file

@ -130,19 +130,17 @@ module ActiveRecord
def schema_migration # :nodoc:
@schema_migration ||= begin
conn = self
name = conn.pool.db_config.name
schema_migration_name = "#{name}::SchemaMigration"
spec_name = conn.pool.pool_config.connection_specification_name
return ActiveRecord::SchemaMigration if spec_name == "ActiveRecord::Base"
schema_migration_name = "#{spec_name}::SchemaMigration"
Class.new(ActiveRecord::SchemaMigration) do
define_singleton_method(:name) { schema_migration_name }
define_singleton_method(:to_s) { schema_migration_name }
connection_handler.connection_pool_names.each do |pool_name|
if conn.pool == connection_handler.retrieve_connection_pool(pool_name)
self.connection_specification_name = pool_name
break
end
end
self.connection_specification_name = spec_name
end
end
end

View file

@ -67,6 +67,11 @@ class MultiDbMigratorTest < ActiveRecord::TestCase
end
end
def test_schema_migration_class_names
assert_equal "ActiveRecord::SchemaMigration", @schema_migration_a.name
assert_equal "ARUnit2Model::SchemaMigration", @schema_migration_b.name
end
def test_finds_migrations
@migrations_a_list.each_with_index do |pair, i|
assert_equal @migrations_a[i].version, pair.first

View file

@ -133,12 +133,12 @@ class LoadingTest < ActiveSupport::TestCase
require "#{rails_root}/config/environment"
setup_ar!
initial = [ActiveStorage::Blob, ActiveStorage::Attachment, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata, ApplicationRecord, "primary::SchemaMigration"].collect(&:to_s).sort
initial = [ActiveStorage::Blob, ActiveStorage::Attachment, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata, ApplicationRecord].collect(&:to_s).sort
assert_equal initial, ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq
get "/load"
assert_equal [Post].collect(&:to_s).sort, ActiveRecord::Base.descendants.collect(&:to_s).sort - initial
get "/unload"
assert_equal ["ActiveRecord::InternalMetadata", "ActiveRecord::SchemaMigration", "primary::SchemaMigration"], ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq
assert_equal ["ActiveRecord::InternalMetadata", "ActiveRecord::SchemaMigration"], ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq
end
test "initialize cant be called twice" do

View file

@ -101,7 +101,7 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase
test "autoloaded? and overridden class names" do
invalid_constant_name = Module.new do
def self.name
"primary::SchemaMigration"
"MyModule::SchemaMigration"
end
end
assert_not deps.autoloaded?(invalid_constant_name)