Merge pull request #51162 from Shopify/refactor-internal-metadata-pool

Refactor InternalMetadata, MigrationContext to belong to the pool
This commit is contained in:
Jean Boussier 2024-02-22 13:04:56 +01:00 committed by GitHub
commit 9f1dec2ea5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
31 changed files with 376 additions and 333 deletions

View File

@ -173,6 +173,22 @@ module ActiveRecord
@reaper.run
end
def migration_context # :nodoc:
MigrationContext.new(migrations_paths, schema_migration, internal_metadata)
end
def migrations_paths # :nodoc:
db_config.migrations_paths || Migrator.migrations_paths
end
def schema_migration # :nodoc:
SchemaMigration.new(self)
end
def internal_metadata # :nodoc:
InternalMetadata.new(self)
end
# Retrieve the connection associated with the current thread, or call
# #checkout to obtain one if necessary.
#

View File

@ -214,7 +214,7 @@ module ActiveRecord
end
def truncate_tables(*table_names) # :nodoc:
table_names -= [schema_migration.table_name, internal_metadata.table_name]
table_names -= [pool.schema_migration.table_name, pool.internal_metadata.table_name]
return if table_names.empty?

View File

@ -1317,7 +1317,7 @@ module ActiveRecord
end
def dump_schema_information # :nodoc:
versions = schema_migration.versions
versions = pool.schema_migration.versions
insert_versions_sql(versions) if versions.any?
end
@ -1327,8 +1327,9 @@ module ActiveRecord
def assume_migrated_upto_version(version)
version = version.to_i
sm_table = quote_table_name(schema_migration.table_name)
sm_table = quote_table_name(pool.schema_migration.table_name)
migration_context = pool.migration_context
migrated = migration_context.get_all_versions
versions = migration_context.migrations.map(&:version)
@ -1838,7 +1839,7 @@ module ActiveRecord
end
def insert_versions_sql(versions)
sm_table = quote_table_name(schema_migration.table_name)
sm_table = quote_table_name(pool.schema_migration.table_name)
if versions.is_a?(Array)
sql = +"INSERT INTO #{sm_table} (version) VALUES\n"

View File

@ -211,10 +211,6 @@ module ActiveRecord
@config[:replica] || false
end
def use_metadata_table?
@config.fetch(:use_metadata_table, true)
end
def connection_retries
(@config[:connection_retries] || 1).to_i
end
@ -242,22 +238,6 @@ module ActiveRecord
connection_class.current_preventing_writes
end
def migrations_paths # :nodoc:
@config[:migrations_paths] || Migrator.migrations_paths
end
def migration_context # :nodoc:
MigrationContext.new(migrations_paths, schema_migration, internal_metadata)
end
def schema_migration # :nodoc:
SchemaMigration.new(self)
end
def internal_metadata # :nodoc:
InternalMetadata.new(self)
end
def prepared_statements?
@prepared_statements && !prepared_statements_disabled_cache.include?(object_id)
end
@ -872,7 +852,7 @@ module ActiveRecord
# numbered migration that has been executed, or 0 if no schema
# information is present / the database is empty.
def schema_version
migration_context.current_version
pool.migration_context.current_version
end
class << self

View File

@ -91,6 +91,10 @@ module ActiveRecord
def schema_cache_path
raise NotImplementedError
end
def use_metadata_table?
raise NotImplementedError
end
end
end
end

View File

@ -154,6 +154,10 @@ module ActiveRecord
!replica? && !!configuration_hash.fetch(:database_tasks, true)
end
def use_metadata_table? # :nodoc:
configuration_hash.fetch(:use_metadata_table, true)
end
private
def schema_file_type(format)
case format

View File

@ -13,17 +13,13 @@ module ActiveRecord
class NullInternalMetadata # :nodoc:
end
attr_reader :connection, :arel_table
attr_reader :arel_table
def initialize(connection)
@connection = connection
def initialize(pool)
@pool = pool
@arel_table = Arel::Table.new(table_name)
end
def enabled?
connection.use_metadata_table?
end
def primary_key
"key"
end
@ -36,50 +32,66 @@ module ActiveRecord
"#{ActiveRecord::Base.table_name_prefix}#{ActiveRecord::Base.internal_metadata_table_name}#{ActiveRecord::Base.table_name_suffix}"
end
def enabled?
@pool.db_config.use_metadata_table?
end
def []=(key, value)
return unless enabled?
update_or_create_entry(key, value)
@pool.with_connection do |connection|
update_or_create_entry(connection, key, value)
end
end
def [](key)
return unless enabled?
if entry = select_entry(key)
entry[value_key]
@pool.with_connection do |connection|
if entry = select_entry(connection, key)
entry[value_key]
end
end
end
def delete_all_entries
dm = Arel::DeleteManager.new(arel_table)
connection.delete(dm, "#{self.class} Destroy")
@pool.with_connection do |connection|
connection.delete(dm, "#{self.class} Destroy")
end
end
def count
sm = Arel::SelectManager.new(arel_table)
sm.project(*Arel::Nodes::Count.new([Arel.star]))
connection.select_values(sm, "#{self.class} Count").first
@pool.with_connection do |connection|
connection.select_values(sm, "#{self.class} Count").first
end
end
def create_table_and_set_flags(environment, schema_sha1 = nil)
return unless enabled?
create_table
update_or_create_entry(:environment, environment)
update_or_create_entry(:schema_sha1, schema_sha1) if schema_sha1
@pool.with_connection do |connection|
create_table
update_or_create_entry(connection, :environment, environment)
update_or_create_entry(connection, :schema_sha1, schema_sha1) if schema_sha1
end
end
# Creates an internal metadata table with columns +key+ and +value+
def create_table
return unless enabled?
unless connection.table_exists?(table_name)
connection.create_table(table_name, id: false) do |t|
t.string :key, **connection.internal_string_options_for_primary_key
t.string :value
t.timestamps
@pool.with_connection do |connection|
unless connection.table_exists?(table_name)
connection.create_table(table_name, id: false) do |t|
t.string :key, **connection.internal_string_options_for_primary_key
t.string :value
t.timestamps
end
end
end
end
@ -87,49 +99,51 @@ module ActiveRecord
def drop_table
return unless enabled?
connection.drop_table table_name, if_exists: true
@pool.with_connection do |connection|
connection.drop_table table_name, if_exists: true
end
end
def table_exists?
@connection.schema_cache.data_source_exists?(table_name)
@pool.schema_cache.data_source_exists?(table_name)
end
private
def update_or_create_entry(key, value)
entry = select_entry(key)
def update_or_create_entry(connection, key, value)
entry = select_entry(connection, key)
if entry
if entry[value_key] != value
update_entry(key, value)
update_entry(connection, key, value)
else
entry[value_key]
end
else
create_entry(key, value)
create_entry(connection, key, value)
end
end
def current_time
def current_time(connection)
connection.default_timezone == :utc ? Time.now.utc : Time.now
end
def create_entry(key, value)
def create_entry(connection, key, value)
im = Arel::InsertManager.new(arel_table)
im.insert [
[arel_table[primary_key], key],
[arel_table[value_key], value],
[arel_table[:created_at], current_time],
[arel_table[:updated_at], current_time]
[arel_table[:created_at], current_time(connection)],
[arel_table[:updated_at], current_time(connection)]
]
connection.insert(im, "#{self.class} Create", primary_key, key)
end
def update_entry(key, new_value)
def update_entry(connection, key, new_value)
um = Arel::UpdateManager.new(arel_table)
um.set [
[arel_table[value_key], new_value],
[arel_table[:updated_at], current_time]
[arel_table[:updated_at], current_time(connection)]
]
um.where(arel_table[primary_key].eq(key))
@ -137,7 +151,7 @@ module ActiveRecord
connection.update(um, "#{self.class} Update")
end
def select_entry(key)
def select_entry(connection, key)
sm = Arel::SelectManager.new(arel_table)
sm.project(Arel::Nodes::SqlLiteral.new("*"))
sm.where(arel_table[primary_key].eq(Arel::Nodes::BindParam.new(key)))

View File

@ -161,8 +161,7 @@ module ActiveRecord
def initialize(message = nil, pending_migrations: nil)
if pending_migrations.nil?
connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection
pending_migrations = connection.migration_context.open.pending_migrations
pending_migrations = connection_pool.migration_context.open.pending_migrations
end
super(message || detailed_migration_message(pending_migrations))
@ -183,8 +182,8 @@ module ActiveRecord
message
end
def connection
ActiveRecord::Tasks::DatabaseTasks.migration_connection
def connection_pool
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
end
end
@ -701,8 +700,8 @@ module ActiveRecord
def check_all_pending!
pending_migrations = []
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: env) do |connection|
if pending = connection.migration_context.open.pending_migrations
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: env) do |pool|
if pending = pool.migration_context.open.pending_migrations
pending_migrations << pending
end
end
@ -773,8 +772,8 @@ module ActiveRecord
pending_migrations = []
ActiveRecord::Base.configurations.configs_for(env_name: env).each do |db_config|
ActiveRecord::PendingMigrationConnection.establish_temporary_connection(db_config) do |conn|
if pending = conn.migration_context.open.pending_migrations
ActiveRecord::PendingMigrationConnection.with_temporary_pool(db_config) do |pool|
if pending = pool.migration_context.open.pending_migrations
pending_migrations << pending
end
end
@ -799,6 +798,7 @@ module ActiveRecord
@name = name
@version = version
@connection = nil
@pool = nil
end
def execution_strategy
@ -1034,6 +1034,10 @@ module ActiveRecord
@connection || ActiveRecord::Tasks::DatabaseTasks.migration_connection
end
def connection_pool
@pool || ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
end
def method_missing(method, *arguments, &block)
say_with_time "#{method}(#{format_arguments(arguments)})" do
unless connection.respond_to? :revert
@ -1206,8 +1210,8 @@ module ActiveRecord
def initialize(migrations_paths, schema_migration = nil, internal_metadata = nil)
@migrations_paths = migrations_paths
@schema_migration = schema_migration || SchemaMigration.new(connection)
@internal_metadata = internal_metadata || InternalMetadata.new(connection)
@schema_migration = schema_migration || SchemaMigration.new(connection_pool)
@internal_metadata = internal_metadata || InternalMetadata.new(connection_pool)
end
# Runs the migrations in the +migrations_path+.
@ -1339,11 +1343,12 @@ module ActiveRecord
end
def last_stored_environment # :nodoc:
return nil unless connection.internal_metadata.enabled?
internal_metadata = connection_pool.internal_metadata
return nil unless internal_metadata.enabled?
return nil if current_version == 0
raise NoEnvironmentInSchemaError unless connection.internal_metadata.table_exists?
raise NoEnvironmentInSchemaError unless internal_metadata.table_exists?
environment = connection.internal_metadata[:environment]
environment = internal_metadata[:environment]
raise NoEnvironmentInSchemaError unless environment
environment
end
@ -1353,6 +1358,10 @@ module ActiveRecord
ActiveRecord::Tasks::DatabaseTasks.migration_connection
end
def connection_pool
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
end
def migration_files
paths = Array(migrations_paths)
Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
@ -1397,8 +1406,9 @@ module ActiveRecord
# For cases where a table doesn't exist like loading from schema cache
def current_version
connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection
connection_pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
schema_migration = SchemaMigration.new(connection)
internal_metadata = InternalMetadata.new(connection)
internal_metadata = InternalMetadata.new(connection_pool)
MigrationContext.new(migrations_paths, schema_migration, internal_metadata).current_version
end

View File

@ -2,10 +2,10 @@
module ActiveRecord
class PendingMigrationConnection # :nodoc:
def self.establish_temporary_connection(db_config, &block)
def self.with_temporary_pool(db_config, &block)
pool = ActiveRecord::Base.connection_handler.establish_connection(db_config, owner_name: self)
yield pool.connection
yield pool
ensure
ActiveRecord::Base.connection_handler.remove_connection_pool(self.name)
end

View File

@ -9,10 +9,10 @@ databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
db_namespace = namespace :db do
desc "Set the environment value for the database"
task "environment:set" => :load_config do
connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection
raise ActiveRecord::EnvironmentStorageError unless connection.internal_metadata.enabled?
pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
raise ActiveRecord::EnvironmentStorageError unless pool.internal_metadata.enabled?
connection.internal_metadata.create_table_and_set_flags(connection.migration_context.current_environment)
pool.internal_metadata.create_table_and_set_flags(pool.migration_context.current_environment)
end
task check_protected_environments: :load_config do
@ -135,7 +135,7 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Migrate #{name} database for current environment"
task name => :load_config do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: Rails.env, name: name) do |conn|
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do
ActiveRecord::Tasks::DatabaseTasks.migrate
end
@ -186,7 +186,7 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.check_target_version
ActiveRecord::Tasks::DatabaseTasks.migration_connection.migration_context.run(
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.migration_context.run(
:up,
ActiveRecord::Tasks::DatabaseTasks.target_version
)
@ -199,9 +199,9 @@ db_namespace = namespace :db do
task name => :load_config do
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: Rails.env, name: name) do |conn|
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do |pool|
ActiveRecord::Tasks::DatabaseTasks.check_target_version
conn.migration_context.run(:up, ActiveRecord::Tasks::DatabaseTasks.target_version)
pool.migration_context.run(:up, ActiveRecord::Tasks::DatabaseTasks.target_version)
end
db_namespace["_dump:#{name}"].invoke
@ -217,7 +217,7 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.check_target_version
ActiveRecord::Tasks::DatabaseTasks.migration_connection.migration_context.run(
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.migration_context.run(
:down,
ActiveRecord::Tasks::DatabaseTasks.target_version
)
@ -230,9 +230,9 @@ db_namespace = namespace :db do
task name => :load_config do
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: Rails.env, name: name) do |conn|
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do |pool|
ActiveRecord::Tasks::DatabaseTasks.check_target_version
conn.migration_context.run(:down, ActiveRecord::Tasks::DatabaseTasks.target_version)
pool.migration_context.run(:down, ActiveRecord::Tasks::DatabaseTasks.target_version)
end
db_namespace["_dump:#{name}"].invoke
@ -242,7 +242,7 @@ db_namespace = namespace :db do
desc "Display status of migrations"
task status: :load_config do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each do
ActiveRecord::Tasks::DatabaseTasks.migrate_status
end
end
@ -251,7 +251,7 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Display status of migrations for #{name} database"
task name => :load_config do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: Rails.env, name: name) do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do
ActiveRecord::Tasks::DatabaseTasks.migrate_status
end
end
@ -265,8 +265,8 @@ db_namespace = namespace :db do
task name => :load_config do
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: Rails.env, name: name) do |conn|
conn.migration_context.rollback(step)
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do |pool|
pool.migration_context.rollback(step)
end
db_namespace["_dump:#{name}"].invoke
@ -281,7 +281,7 @@ db_namespace = namespace :db do
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
ActiveRecord::Tasks::DatabaseTasks.migration_connection.migration_context.rollback(step)
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.migration_context.rollback(step)
db_namespace["_dump"].invoke
end
@ -290,7 +290,7 @@ db_namespace = namespace :db do
task forward: :load_config do
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
ActiveRecord::Tasks::DatabaseTasks.migration_connection.migration_context.forward(step)
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.migration_context.forward(step)
db_namespace["_dump"].invoke
end
@ -321,9 +321,9 @@ db_namespace = namespace :db do
desc "Retrieve the current schema version number"
task version: :load_config do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: Rails.env) do |connection|
puts "\ndatabase: #{connection.pool.db_config.database}\n"
puts "Current version: #{connection.schema_version}"
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env) do |pool|
puts "\ndatabase: #{pool.db_config.database}\n"
puts "Current version: #{pool.migration_context.current_version}"
puts
end
end
@ -344,8 +344,8 @@ db_namespace = namespace :db do
task abort_if_pending_migrations: :load_config do
pending_migrations = []
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each do |conn|
pending_migrations << conn.migration_context.open.pending_migrations
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each do |pool|
pending_migrations << pool.migration_context.open.pending_migrations
end
pending_migrations = pending_migrations.flatten!
@ -365,8 +365,8 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
# desc "Raise an error if there are pending migrations for #{name} database"
task name => :load_config do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: Rails.env, name: name) do |conn|
pending_migrations = conn.migration_context.open.pending_migrations
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do |pool|
pending_migrations = pool.migration_context.open.pending_migrations
if pending_migrations.any?
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
@ -462,8 +462,8 @@ db_namespace = namespace :db do
namespace :schema do
desc "Create a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`)"
task dump: :load_config do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each do |conn|
db_config = conn.pool.db_config
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each do |pool|
db_config = pool.db_config
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)
end
@ -480,8 +480,8 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Create a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) for #{name} database"
task name => :load_config do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(name: name) do |conn|
db_config = conn.pool.db_config
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(name: name) do |pool|
db_config = pool.db_config
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)
end
@ -495,8 +495,8 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Load a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) into the #{name} database"
task name => "db:test:purge:#{name}" do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(name: name) do |conn|
db_config = conn.pool.db_config
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(name: name) do |pool|
db_config = pool.db_config
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
end
@ -507,11 +507,11 @@ db_namespace = namespace :db do
namespace :cache do
desc "Create a db/schema_cache.yml file."
task dump: :load_config do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each do |conn|
db_config = conn.pool.db_config
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each do |pool|
db_config = pool.db_config
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(conn, filename)
ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(pool, filename)
end
end
@ -544,8 +544,8 @@ db_namespace = namespace :db do
namespace :test do
# desc "Recreate the test database from an existent schema file (schema.rb or structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`)"
task load_schema: %w(db:test:purge) do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: "test") do |conn|
db_config = conn.pool.db_config
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: "test") do |pool|
db_config = pool.db_config
ActiveRecord::Schema.verbose = false
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
@ -570,8 +570,8 @@ db_namespace = namespace :db do
# desc "Recreate the #{name} test database from an existent schema.rb file"
namespace :load_schema do
task name => "db:test:purge:#{name}" do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: "test", name: name) do |conn|
db_config = conn.pool.db_config
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: "test", name: name) do |pool|
db_config = pool.db_config
ActiveRecord::Schema.verbose = false
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
@ -582,8 +582,8 @@ db_namespace = namespace :db do
# desc "Empty the #{name} test database"
namespace :purge do
task name => %w(load_config check_protected_environments) do
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: "test", name: name) do |conn|
db_config = conn.pool.db_config
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: "test", name: name) do |pool|
db_config = pool.db_config
ActiveRecord::Tasks::DatabaseTasks.purge(db_config)
end
end

View File

@ -52,14 +52,16 @@ module ActiveRecord
end
def define(info, &block) # :nodoc:
instance_eval(&block)
connection_pool.with_connection do |connection|
instance_eval(&block)
connection.schema_migration.create_table
if info[:version].present?
connection.assume_migrated_upto_version(info[:version])
connection_pool.schema_migration.create_table
if info[:version].present?
connection.assume_migrated_upto_version(info[:version])
end
connection_pool.internal_metadata.create_table_and_set_flags(connection_pool.migration_context.current_environment)
end
connection.internal_metadata.create_table_and_set_flags(connection.migration_context.current_environment)
end
end

View File

@ -41,8 +41,10 @@ module ActiveRecord
cattr_accessor :unique_ignore_pattern, default: /^uniq_rails_[0-9a-f]{10}$/
class << self
def dump(connection = ActiveRecord::Base.connection, stream = $stdout, config = ActiveRecord::Base)
connection.create_schema_dumper(generate_options(config)).dump(stream)
def dump(pool = ActiveRecord::Base.connection_pool, stream = $stdout, config = ActiveRecord::Base)
pool.with_connection do |connection|
connection.create_schema_dumper(generate_options(config)).dump(stream)
end
stream
end
@ -68,9 +70,9 @@ module ActiveRecord
private
attr_accessor :table_name
def initialize(connection, options = {})
def initialize(connection, version, options = {})
@connection = connection
@version = connection.migration_context.current_version rescue nil
@version = connection.pool.migration_context.current_version rescue nil
@options = options
@ignore_tables = [
ActiveRecord::Base.schema_migrations_table_name,

View File

@ -9,29 +9,35 @@ module ActiveRecord
class NullSchemaMigration # :nodoc:
end
attr_reader :connection, :arel_table
attr_reader :arel_table
def initialize(connection)
@connection = connection
def initialize(pool)
@pool = pool
@arel_table = Arel::Table.new(table_name)
end
def create_version(version)
im = Arel::InsertManager.new(arel_table)
im.insert(arel_table[primary_key] => version)
connection.insert(im, "#{self.class} Create", primary_key, version)
@pool.with_connection do |connection|
connection.insert(im, "#{self.class} Create", primary_key, version)
end
end
def delete_version(version)
dm = Arel::DeleteManager.new(arel_table)
dm.wheres = [arel_table[primary_key].eq(version)]
connection.delete(dm, "#{self.class} Destroy")
@pool.with_connection do |connection|
connection.delete(dm, "#{self.class} Destroy")
end
end
def delete_all_versions
versions.each do |version|
delete_version(version)
@pool.with_connection do |connection|
versions.each do |version|
delete_version(version)
end
end
end
@ -44,15 +50,19 @@ module ActiveRecord
end
def create_table
unless connection.table_exists?(table_name)
connection.create_table(table_name, id: false) do |t|
t.string :version, **connection.internal_string_options_for_primary_key
@pool.with_connection do |connection|
unless connection.table_exists?(table_name)
connection.create_table(table_name, id: false) do |t|
t.string :version, **connection.internal_string_options_for_primary_key
end
end
end
end
def drop_table
connection.drop_table table_name, if_exists: true
@pool.with_connection do |connection|
connection.drop_table table_name, if_exists: true
end
end
def normalize_migration_number(number)
@ -68,7 +78,9 @@ module ActiveRecord
sm.project(arel_table[primary_key])
sm.order(arel_table[primary_key].asc)
connection.select_values(sm, "#{self.class} Load")
@pool.with_connection do |connection|
connection.select_values(sm, "#{self.class} Load")
end
end
def integer_versions
@ -79,11 +91,15 @@ module ActiveRecord
sm = Arel::SelectManager.new(arel_table)
sm.project(*Arel::Nodes::Count.new([Arel.star]))
connection.select_values(sm, "#{self.class} Count").first
@pool.with_connection do |connection|
connection.select_values(sm, "#{self.class} Count").first
end
end
def table_exists?
connection.data_source_exists?(table_name)
@pool.with_connection do |connection|
connection.data_source_exists?(table_name)
end
end
end
end

View File

@ -179,7 +179,7 @@ module ActiveRecord
each_current_configuration(env) do |db_config|
with_temporary_pool(db_config) do
begin
database_initialized = migration_connection.schema_migration.table_exists?
database_initialized = migration_connection_pool.schema_migration.table_exists?
rescue ActiveRecord::NoDatabaseError
create(db_config)
retry
@ -240,7 +240,7 @@ module ActiveRecord
check_target_version
migration_connection.migration_context.migrate(target_version) do |migration|
migration_connection_pool.migration_context.migrate(target_version) do |migration|
if version.blank?
scope.blank? || scope == migration.scope
else
@ -250,7 +250,7 @@ module ActiveRecord
Migration.write("No migrations ran. (using #{scope} scope)") if scope.present? && migrations_ran.empty?
end
migration_connection.schema_cache.clear!
migration_connection_pool.schema_cache.clear!
ensure
Migration.verbose = verbose_was
end
@ -258,9 +258,9 @@ module ActiveRecord
def db_configs_with_versions # :nodoc:
db_configs_with_versions = Hash.new { |h, k| h[k] = [] }
with_temporary_connection_for_each do |conn|
db_config = conn.pool.db_config
versions_to_run = conn.migration_context.pending_migration_versions
with_temporary_pool_for_each do |pool|
db_config = pool.db_config
versions_to_run = pool.migration_context.pending_migration_versions
target_version = ActiveRecord::Tasks::DatabaseTasks.target_version
versions_to_run.each do |version|
@ -273,15 +273,15 @@ module ActiveRecord
end
def migrate_status
unless migration_connection.schema_migration.table_exists?
unless migration_connection_pool.schema_migration.table_exists?
Kernel.abort "Schema migrations table does not exist yet."
end
# output
puts "\ndatabase: #{migration_connection.pool.db_config.database}\n\n"
puts "\ndatabase: #{migration_connection_pool.db_config.database}\n\n"
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
puts "-" * 50
migration_connection.migration_context.migrations_status.each do |status, version, name|
migration_connection_pool.migration_context.migrations_status.each do |status, version, name|
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
end
puts
@ -362,7 +362,7 @@ module ActiveRecord
raise ArgumentError, "unknown format #{format.inspect}"
end
migration_connection.internal_metadata.create_table_and_set_flags(db_config.env_name, schema_sha1(file))
migration_connection_pool.internal_metadata.create_table_and_set_flags(db_config.env_name, schema_sha1(file))
ensure
Migration.verbose = verbose_was
end
@ -374,11 +374,12 @@ module ActiveRecord
return true unless file && File.exist?(file)
with_temporary_connection(db_config) do |connection|
return false unless connection.internal_metadata.enabled?
return false unless connection.internal_metadata.table_exists?
with_temporary_pool(db_config) do |pool|
internal_metadata = pool.internal_metadata
return false unless internal_metadata.enabled?
return false unless internal_metadata.table_exists?
connection.internal_metadata[:schema_sha1] == schema_sha1(file)
internal_metadata[:schema_sha1] == schema_sha1(file)
end
end
@ -411,11 +412,11 @@ module ActiveRecord
case format
when :ruby
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(migration_connection, file)
ActiveRecord::SchemaDumper.dump(migration_connection_pool, file)
end
when :sql
structure_dump(db_config, filename)
if migration_connection.schema_migration.table_exists?
if migration_connection_pool.schema_migration.table_exists?
File.open(filename, "a") do |f|
f.puts migration_connection.dump_schema_information
f.print "\n"
@ -489,21 +490,21 @@ module ActiveRecord
#
# ==== Examples
# ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(ActiveRecord::Base.connection, "tmp/schema_dump.yaml")
def dump_schema_cache(conn, filename)
conn.schema_cache.dump_to(filename)
def dump_schema_cache(conn_or_pool, filename)
conn_or_pool.schema_cache.dump_to(filename)
end
def clear_schema_cache(filename)
FileUtils.rm_f filename, verbose: false
end
def with_temporary_connection_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, clobber: false, &block) # :nodoc:
def with_temporary_pool_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, clobber: false, &block) # :nodoc:
if name
db_config = ActiveRecord::Base.configurations.configs_for(env_name: env, name: name)
with_temporary_connection(db_config, clobber: clobber, &block)
with_temporary_pool(db_config, clobber: clobber, &block)
else
ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config|
with_temporary_connection(db_config, clobber: clobber, &block)
with_temporary_pool(db_config, clobber: clobber, &block)
end
end
end
@ -522,6 +523,10 @@ module ActiveRecord
migration_class.connection
end
def migration_connection_pool # :nodoc:
migration_class.connection_pool
end
private
def schema_cache_env
if ENV["SCHEMA_CACHE"]
@ -626,11 +631,11 @@ module ActiveRecord
def check_current_protected_environment!(db_config)
with_temporary_pool(db_config) do |pool|
connection = pool.connection
current = connection.migration_context.current_environment
stored = connection.migration_context.last_stored_environment
migration_context = pool.migration_context
current = migration_context.current_environment
stored = migration_context.last_stored_environment
if connection.migration_context.protected_environment?
if migration_context.protected_environment?
raise ActiveRecord::ProtectedEnvironmentError.new(stored)
end

View File

@ -9,7 +9,8 @@ class ActiveRecordSchemaTest < ActiveRecord::TestCase
@original_verbose = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
@connection = ActiveRecord::Base.connection
@schema_migration = @connection.schema_migration
@pool = ActiveRecord::Base.connection_pool
@schema_migration = @pool.schema_migration
@schema_migration.delete_all_versions
end
@ -72,7 +73,7 @@ class ActiveRecordSchemaTest < ActiveRecord::TestCase
t.column :flavor, :string
end
end
assert_equal 7, @connection.migration_context.current_version
assert_equal 7, @pool.migration_context.current_version
ensure
ActiveRecord::Base.table_name_prefix = old_table_name_prefix
@connection.drop_table :nep_fruits

View File

@ -4,7 +4,8 @@ require "cases/helper"
class SchemaMigrationsTest < ActiveRecord::AbstractMysqlTestCase
def setup
@schema_migration = ActiveRecord::Base.connection.schema_migration
@pool = ActiveRecord::Base.connection_pool
@schema_migration = @pool.schema_migration
end
def test_renaming_index_on_foreign_key
@ -30,7 +31,7 @@ class SchemaMigrationsTest < ActiveRecord::AbstractMysqlTestCase
def test_initializes_internal_metadata_for_encoding_utf8mb4
with_encoding_utf8mb4 do
internal_metadata = connection.internal_metadata
internal_metadata = @pool.internal_metadata
table_name = internal_metadata.table_name
connection.drop_table table_name, if_exists: true
@ -39,7 +40,7 @@ class SchemaMigrationsTest < ActiveRecord::AbstractMysqlTestCase
assert connection.column_exists?(table_name, :key, :string)
end
ensure
connection.internal_metadata[:environment] = connection.migration_context.current_environment
@pool.internal_metadata[:environment] = @pool.migration_context.current_environment
end
private

View File

@ -93,7 +93,7 @@ class DefaultEngineOptionTest < ActiveRecord::AbstractMysqlTestCase
ActiveRecord::Base.logger = @logger_was
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::Base.connection.drop_table "mysql_table_options", if_exists: true
ActiveRecord::Base.connection.schema_migration.delete_all_versions rescue nil
ActiveRecord::Base.connection_pool.schema_migration.delete_all_versions rescue nil
end
test "new migrations do not contain default ENGINE=InnoDB option" do
@ -113,8 +113,8 @@ class DefaultEngineOptionTest < ActiveRecord::AbstractMysqlTestCase
end
end.new
connection = ActiveRecord::Base.connection
ActiveRecord::Migrator.new(:up, [migration], connection.schema_migration, connection.internal_metadata).migrate
pool = ActiveRecord::Base.connection_pool
ActiveRecord::Migrator.new(:up, [migration], pool.schema_migration, pool.internal_metadata).migrate
assert_match %r{ENGINE=InnoDB}, @log.string

View File

@ -27,6 +27,7 @@ class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase
super
@connection = ActiveRecord::Base.connection
@pool = ActiveRecord::Base.connection_pool
@old_table_name_prefix = ActiveRecord::Base.table_name_prefix
@old_table_name_suffix = ActiveRecord::Base.table_name_suffix
@ -34,12 +35,12 @@ class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase
ActiveRecord::Base.table_name_prefix = "p_"
ActiveRecord::Base.table_name_suffix = "_s"
@connection.schema_migration.delete_all_versions rescue nil
@pool.schema_migration.delete_all_versions rescue nil
ActiveRecord::Migration.verbose = false
end
def teardown
@connection.schema_migration.delete_all_versions rescue nil
@pool.schema_migration.delete_all_versions rescue nil
ActiveRecord::Migration.verbose = true
ActiveRecord::Base.table_name_prefix = @old_table_name_prefix
@ -52,7 +53,7 @@ class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase
@connection.disable_extension("hstore")
migrations = [EnableHstore.new(nil, 1)]
ActiveRecord::Migrator.new(:up, migrations, @connection.schema_migration, @connection.internal_metadata).migrate
ActiveRecord::Migrator.new(:up, migrations, @pool.schema_migration, @pool.internal_metadata).migrate
assert @connection.extension_enabled?("hstore"), "extension hstore should be enabled"
end
@ -61,7 +62,7 @@ class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase
@connection.create_schema "other_schema"
migrations = [EnableHstoreInSchema.new(nil, 1)]
ActiveRecord::Migrator.new(:up, migrations, @connection.schema_migration, @connection.internal_metadata).migrate
ActiveRecord::Migrator.new(:up, migrations, @pool.schema_migration, @pool.internal_metadata).migrate
assert @connection.extension_enabled?("hstore"), "extension hstore should be enabled"
ensure
@ -73,7 +74,7 @@ class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase
@connection.enable_extension("hstore")
migrations = [DisableHstore.new(nil, 1)]
ActiveRecord::Migrator.new(:up, migrations, @connection.schema_migration, @connection.internal_metadata).migrate
ActiveRecord::Migrator.new(:up, migrations, @pool.schema_migration, @pool.internal_metadata).migrate
assert_not @connection.extension_enabled?("hstore"), "extension hstore should not be enabled"
end

View File

@ -304,15 +304,15 @@ class PostgresqlUUIDGenerationTest < ActiveRecord::PostgreSQLTestCase
end
end.new
connection = ActiveRecord::Base.connection
ActiveRecord::Migrator.new(:up, [migration], connection.schema_migration, connection.internal_metadata).migrate
pool = ActiveRecord::Base.connection_pool
ActiveRecord::Migrator.new(:up, [migration], pool.schema_migration, pool.internal_metadata).migrate
schema = dump_table_schema "pg_uuids_4"
assert_match(/\bcreate_table "pg_uuids_4", id: :uuid, default: -> { "uuid_generate_v4\(\)" }/, schema)
ensure
drop_table "pg_uuids_4"
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::Base.connection.schema_migration.delete_all_versions
ActiveRecord::Base.connection_pool.schema_migration.delete_all_versions
end
uses_transaction :test_schema_dumper_for_uuid_primary_key_default_in_legacy_migration
end
@ -356,15 +356,15 @@ class PostgresqlUUIDTestNilDefault < ActiveRecord::PostgreSQLTestCase
end
end.new
connection = ActiveRecord::Base.connection
ActiveRecord::Migrator.new(:up, [migration], connection.schema_migration, connection.internal_metadata).migrate
pool = ActiveRecord::Base.connection_pool
ActiveRecord::Migrator.new(:up, [migration], pool.schema_migration, pool.internal_metadata).migrate
schema = dump_table_schema "pg_uuids_4"
assert_match(/\bcreate_table "pg_uuids_4", id: :uuid, default: nil/, schema)
ensure
drop_table "pg_uuids_4"
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::Base.connection.schema_migration.delete_all_versions
ActiveRecord::Base.connection_pool.schema_migration.delete_all_versions
end
uses_transaction :test_schema_dumper_for_uuid_primary_key_with_default_nil_in_legacy_migration
end

View File

@ -16,8 +16,9 @@ module ActiveRecord
def setup
super
@connection = ActiveRecord::Base.connection
@schema_migration = @connection.schema_migration
@internal_metadata = @connection.internal_metadata
@pool = ActiveRecord::Base.connection_pool
@schema_migration = @pool.schema_migration
@internal_metadata = @pool.internal_metadata
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
@ -828,8 +829,9 @@ class BaseCompatibilityTest < ActiveRecord::TestCase
def setup
@connection = ActiveRecord::Base.connection
@schema_migration = @connection.schema_migration
@internal_metadata = @connection.internal_metadata
@pool = ActiveRecord::Base.connection_pool
@schema_migration = @pool.schema_migration
@internal_metadata = @pool.internal_metadata
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
@ -916,8 +918,9 @@ module LegacyPolymorphicReferenceIndexTestCases
def setup
@connection = ActiveRecord::Base.connection
@schema_migration = @connection.schema_migration
@internal_metadata = @connection.internal_metadata
@pool = ActiveRecord::Base.connection_pool
@schema_migration = @pool.schema_migration
@internal_metadata = @pool.internal_metadata
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
@ -1055,7 +1058,7 @@ module LegacyPrimaryKeyTestCases
def teardown
@migration.migrate(:down) if @migration
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::Base.connection.schema_migration.delete_all_versions rescue nil
ActiveRecord::Base.connection_pool.schema_migration.delete_all_versions rescue nil
LegacyPrimaryKey.reset_column_information
end

View File

@ -17,14 +17,14 @@ module ActiveRecord
def setup
super
@schema_migration = ActiveRecord::Base.connection.schema_migration
@schema_migration = ActiveRecord::Base.connection_pool.schema_migration
@schema_migration.create_table
@schema_migration.delete_all_versions
@internal_metadata = ActiveRecord::Base.connection.internal_metadata
@internal_metadata = ActiveRecord::Base.connection_pool.internal_metadata
end
teardown do
@schema_migration.delete_all_versions
@schema_migration&.delete_all_versions
end
def test_migration_should_be_run_without_logger

View File

@ -24,7 +24,7 @@ module ActiveRecord
end
def run_migrations
migrator = Base.connection.migration_context
migrator = Base.connection_pool.migration_context
capture(:stdout) { migrator.migrate }
end

View File

@ -44,8 +44,9 @@ class MigrationTest < ActiveRecord::TestCase
end
Reminder.reset_column_information
@verbose_was, ActiveRecord::Migration.verbose = ActiveRecord::Migration.verbose, false
@schema_migration = ActiveRecord::Base.connection.schema_migration
@internal_metadata = ActiveRecord::Base.connection.internal_metadata
@pool = ActiveRecord::Base.connection_pool
@schema_migration = @pool.schema_migration
@internal_metadata = @pool.internal_metadata
ActiveRecord::Base.schema_cache.clear!
end
@ -675,7 +676,7 @@ class MigrationTest < ActiveRecord::TestCase
end
def test_internal_metadata_stores_environment
current_env = env_name(@internal_metadata.connection)
current_env = env_name(@pool)
migrations_path = MIGRATIONS_ROOT + "/valid"
migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration, @internal_metadata)
@ -685,7 +686,7 @@ class MigrationTest < ActiveRecord::TestCase
def test_internal_metadata_stores_environment_when_migration_fails
@internal_metadata.delete_all_entries
current_env = env_name(@internal_metadata.connection)
current_env = env_name(@pool)
migration = Class.new(ActiveRecord::Migration::Current) {
def version; 101 end
@ -703,7 +704,7 @@ class MigrationTest < ActiveRecord::TestCase
@internal_metadata.delete_all_entries
@internal_metadata[:foo] = "bar"
current_env = env_name(@internal_metadata.connection)
current_env = env_name(@pool)
migrations_path = MIGRATIONS_ROOT + "/valid"
migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration, @internal_metadata)
@ -714,10 +715,9 @@ class MigrationTest < ActiveRecord::TestCase
def test_internal_metadata_not_used_when_not_enabled
@internal_metadata.drop_table
original_config = ActiveRecord::Base.connection.instance_variable_get("@config")
original_config = @pool.db_config.instance_variable_get(:@configuration_hash)
modified_config = original_config.dup.merge(use_metadata_table: false)
ActiveRecord::Base.connection.instance_variable_set("@config", modified_config)
@pool.db_config.instance_variable_set(:@configuration_hash, modified_config)
assert_not @internal_metadata.enabled?
assert_not @internal_metadata.table_exists?
@ -729,7 +729,7 @@ class MigrationTest < ActiveRecord::TestCase
assert_not @internal_metadata[:environment]
assert_not @internal_metadata.table_exists?
ensure
ActiveRecord::Base.connection.instance_variable_set("@config", original_config)
@pool.db_config.instance_variable_set(:@configuration_hash, original_config)
@internal_metadata.create_table
end
@ -742,18 +742,18 @@ class MigrationTest < ActiveRecord::TestCase
def test_updating_an_existing_entry_into_internal_metadata
@internal_metadata[:version] = "foo"
updated_at = @internal_metadata.send(:select_entry, :version)["updated_at"]
updated_at = @internal_metadata.send(:select_entry, @pool.connection, :version)["updated_at"]
assert_equal "foo", @internal_metadata[:version]
# same version doesn't update timestamps
@internal_metadata[:version] = "foo"
assert_equal "foo", @internal_metadata[:version]
assert_equal updated_at, @internal_metadata.send(:select_entry, :version)["updated_at"]
assert_equal updated_at, @internal_metadata.send(:select_entry, @pool.connection, :version)["updated_at"]
# updated version updates timestamps
@internal_metadata[:version] = "not_foo"
assert_equal "not_foo", @internal_metadata[:version]
assert_not_equal updated_at, @internal_metadata.send(:select_entry, :version)["updated_at"]
assert_not_equal updated_at, @internal_metadata.send(:select_entry, @pool.connection, :version)["updated_at"]
ensure
@internal_metadata.delete_all_entries
end
@ -762,22 +762,24 @@ class MigrationTest < ActiveRecord::TestCase
@internal_metadata.drop_table
assert_not_predicate @internal_metadata, :table_exists?
@internal_metadata.connection.transaction do
@internal_metadata.create_table
assert_predicate @internal_metadata, :table_exists?
@pool.with_connection do |connection|
connection.transaction do
@internal_metadata.create_table
assert_predicate @internal_metadata, :table_exists?
@internal_metadata[:version] = "foo"
assert_equal "foo", @internal_metadata[:version]
raise ActiveRecord::Rollback
end
@internal_metadata[:version] = "foo"
assert_equal "foo", @internal_metadata[:version]
raise ActiveRecord::Rollback
end
@internal_metadata.connection.transaction do
@internal_metadata.create_table
assert_predicate @internal_metadata, :table_exists?
connection.transaction do
@internal_metadata.create_table
assert_predicate @internal_metadata, :table_exists?
@internal_metadata[:version] = "bar"
assert_equal "bar", @internal_metadata[:version]
raise ActiveRecord::Rollback
@internal_metadata[:version] = "bar"
assert_equal "bar", @internal_metadata[:version]
raise ActiveRecord::Rollback
end
end
ensure
@internal_metadata.create_table
@ -787,20 +789,22 @@ class MigrationTest < ActiveRecord::TestCase
@schema_migration.drop_table
assert_not_predicate @schema_migration, :table_exists?
@schema_migration.connection.transaction do
@schema_migration.create_table
assert_predicate @schema_migration, :table_exists?
@pool.with_connection do |connection|
connection.transaction do
@schema_migration.create_table
assert_predicate @schema_migration, :table_exists?
assert_equal "foo", @schema_migration.create_version("foo")
raise ActiveRecord::Rollback
end
assert_equal "foo", @schema_migration.create_version("foo")
raise ActiveRecord::Rollback
end
@schema_migration.connection.transaction do
@schema_migration.create_table
assert_predicate @schema_migration, :table_exists?
connection.transaction do
@schema_migration.create_table
assert_predicate @schema_migration, :table_exists?
assert_equal "bar", @schema_migration.create_version("bar")
raise ActiveRecord::Rollback
assert_equal "bar", @schema_migration.create_version("bar")
raise ActiveRecord::Rollback
end
end
ensure
@schema_migration.create_table
@ -1142,8 +1146,8 @@ class MigrationTest < ActiveRecord::TestCase
other_process.join
end
def env_name(connection)
connection.pool.db_config.env_name
def env_name(pool)
pool.db_config.env_name
end
end
@ -1789,8 +1793,8 @@ class CopyMigrationsTest < ActiveRecord::TestCase
class MigrationValidationTest < ActiveRecord::TestCase
def setup
@verbose_was, ActiveRecord::Migration.verbose = ActiveRecord::Migration.verbose, false
@schema_migration = ActiveRecord::Base.connection.schema_migration
@internal_metadata = ActiveRecord::Base.connection.internal_metadata
@schema_migration = ActiveRecord::Base.connection_pool.schema_migration
@internal_metadata = ActiveRecord::Base.connection_pool.internal_metadata
@active_record_validate_timestamps_was = ActiveRecord.validate_migration_timestamps
ActiveRecord.validate_migration_timestamps = true
ActiveRecord::Base.schema_cache.clear!

View File

@ -21,10 +21,11 @@ class MigratorTest < ActiveRecord::TestCase
def setup
super
@schema_migration = ActiveRecord::Base.connection.schema_migration
@pool = ActiveRecord::Base.connection_pool
@schema_migration = @pool.schema_migration
@schema_migration.create_table
@schema_migration.delete_all_versions rescue nil
@internal_metadata = ActiveRecord::Base.connection.internal_metadata
@internal_metadata = @pool.internal_metadata
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.message_count = 0
ActiveRecord::Migration.class_eval do
@ -89,9 +90,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_finds_migrations
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid", schema_migration, internal_metadata).migrations
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid", @schema_migration, @internal_metadata).migrations
[[1, "ValidPeopleHaveLastNames"], [2, "WeNeedReminders"], [3, "InnocentJointable"]].each_with_index do |pair, i|
assert_equal migrations[i].version, pair.first
@ -100,9 +99,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_finds_migrations_in_subdirectories
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid_with_subdirectories", schema_migration, internal_metadata).migrations
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid_with_subdirectories", @schema_migration, @internal_metadata).migrations
[[1, "ValidPeopleHaveLastNames"], [2, "WeNeedReminders"], [3, "InnocentJointable"]].each_with_index do |pair, i|
assert_equal migrations[i].version, pair.first
@ -111,10 +108,8 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_finds_migrations_from_two_directories
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
directories = [MIGRATIONS_ROOT + "/valid_with_timestamps", MIGRATIONS_ROOT + "/to_copy_with_timestamps"]
migrations = ActiveRecord::MigrationContext.new(directories, schema_migration, internal_metadata).migrations
migrations = ActiveRecord::MigrationContext.new(directories, @schema_migration, @internal_metadata).migrations
[[20090101010101, "PeopleHaveHobbies"],
[20090101010202, "PeopleHaveDescriptions"],
@ -127,18 +122,14 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_finds_migrations_in_numbered_directory
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/10_urban", schema_migration, internal_metadata).migrations
migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/10_urban", @schema_migration, @internal_metadata).migrations
assert_equal 9, migrations[0].version
assert_equal "AddExpressions", migrations[0].name
end
def test_relative_migrations
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
list = Dir.chdir(MIGRATIONS_ROOT) do
ActiveRecord::MigrationContext.new("valid", schema_migration, internal_metadata).migrations
ActiveRecord::MigrationContext.new("valid", @schema_migration, @internal_metadata).migrations
end
migration_proxy = list.find { |item|
@ -158,8 +149,6 @@ class MigratorTest < ActiveRecord::TestCase
def test_migrations_status
path = MIGRATIONS_ROOT + "/valid"
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
@schema_migration.create_version(2)
@schema_migration.create_version(10)
@ -169,14 +158,11 @@ class MigratorTest < ActiveRecord::TestCase
["up", "002", "We need reminders"],
["down", "003", "Innocent jointable"],
["up", "010", "********** NO FILE **********"],
], ActiveRecord::MigrationContext.new(path, schema_migration, internal_metadata).migrations_status
], ActiveRecord::MigrationContext.new(path, @schema_migration, @internal_metadata).migrations_status
end
def test_migrations_status_order_new_and_old_version
path = MIGRATIONS_ROOT + "/old_and_new_versions"
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
@schema_migration.create_version(230)
@schema_migration.create_version(231)
@schema_migration.create_version(20210716122844)
@ -187,13 +173,11 @@ class MigratorTest < ActiveRecord::TestCase
["up", "231", "Add people last name"],
["up", "20210716122844", "Add people description"],
["up", "20210716123013", "Add people number of legs"],
], ActiveRecord::MigrationContext.new(path, schema_migration, internal_metadata).migrations_status
], ActiveRecord::MigrationContext.new(path, @schema_migration, @internal_metadata).migrations_status
end
def test_migrations_status_order_new_and_old_version_applied_out_of_order
path = MIGRATIONS_ROOT + "/old_and_new_versions"
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
@schema_migration.create_version(230)
@schema_migration.create_version(231)
@ -209,13 +193,11 @@ class MigratorTest < ActiveRecord::TestCase
["up", "231", "Add people last name"],
["down", "20210716122844", "Add people description"],
["up", "20210716123013", "Add people number of legs"],
], ActiveRecord::MigrationContext.new(path, schema_migration, internal_metadata).migrations_status
], ActiveRecord::MigrationContext.new(path, @schema_migration, @internal_metadata).migrations_status
end
def test_migrations_status_in_subdirectories
path = MIGRATIONS_ROOT + "/valid_with_subdirectories"
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
@schema_migration.create_version(2)
@schema_migration.create_version(10)
@ -225,14 +207,12 @@ class MigratorTest < ActiveRecord::TestCase
["up", "002", "We need reminders"],
["down", "003", "Innocent jointable"],
["up", "010", "********** NO FILE **********"],
], ActiveRecord::MigrationContext.new(path, schema_migration, internal_metadata).migrations_status
], ActiveRecord::MigrationContext.new(path, @schema_migration, @internal_metadata).migrations_status
end
def test_migrations_status_with_schema_define_in_subdirectories
path = MIGRATIONS_ROOT + "/valid_with_subdirectories"
prev_paths = ActiveRecord::Migrator.migrations_paths
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
ActiveRecord::Migrator.migrations_paths = path
ActiveRecord::Schema.define(version: 3) do
@ -242,15 +222,13 @@ class MigratorTest < ActiveRecord::TestCase
["up", "001", "Valid people have last names"],
["up", "002", "We need reminders"],
["up", "003", "Innocent jointable"],
], ActiveRecord::MigrationContext.new(path, schema_migration, internal_metadata).migrations_status
], ActiveRecord::MigrationContext.new(path, @schema_migration, @internal_metadata).migrations_status
ensure
ActiveRecord::Migrator.migrations_paths = prev_paths
end
def test_migrations_status_from_two_directories
paths = [MIGRATIONS_ROOT + "/valid_with_timestamps", MIGRATIONS_ROOT + "/to_copy_with_timestamps"]
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
@schema_migration.create_version("20100101010101")
@schema_migration.create_version("20160528010101")
@ -262,7 +240,7 @@ class MigratorTest < ActiveRecord::TestCase
["down", "20100201010101", "Valid with timestamps we need reminders"],
["down", "20100301010101", "Valid with timestamps innocent jointable"],
["up", "20160528010101", "********** NO FILE **********"],
], ActiveRecord::MigrationContext.new(paths, schema_migration, internal_metadata).migrations_status
], ActiveRecord::MigrationContext.new(paths, @schema_migration, @internal_metadata).migrations_status
end
def test_migrator_interleaved_migrations
@ -310,8 +288,8 @@ class MigratorTest < ActiveRecord::TestCase
def test_current_version
@schema_migration.create_version("1000")
schema_migration = ActiveRecord::Base.connection.schema_migration
internal_metadata = ActiveRecord::Base.connection.internal_metadata
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
internal_metadata = ActiveRecord::Base.connection_pool.internal_metadata
migrator = ActiveRecord::MigrationContext.new("db/migrate", schema_migration, internal_metadata)
assert_equal 1000, migrator.current_version
end
@ -426,7 +404,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_migrator_going_down_due_to_version_target
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
calls, migrator = migrator_class(3)
migrator = migrator.new("valid", schema_migration)
@ -443,7 +421,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_migrator_output_when_running_multiple_migrations
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
_, migrator = migrator_class(3)
migrator = migrator.new("valid", schema_migration)
@ -459,7 +437,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_migrator_output_when_running_single_migration
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
_, migrator = migrator_class(1)
migrator = migrator.new("valid", schema_migration)
@ -469,7 +447,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_migrator_rollback
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
_, migrator = migrator_class(3)
migrator = migrator.new("valid", schema_migration)
@ -490,7 +468,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_migrator_db_has_no_schema_migrations_table
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
_, migrator = migrator_class(3)
migrator = migrator.new("valid", schema_migration)
@ -501,7 +479,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_migrator_forward
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
_, migrator = migrator_class(3)
migrator = migrator.new("/valid", schema_migration)
migrator.migrate(1)
@ -518,7 +496,7 @@ class MigratorTest < ActiveRecord::TestCase
# migrate up to 1
@schema_migration.create_version("1")
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
calls, migrator = migrator_class(3)
migrator = migrator.new("valid", schema_migration)
migrator.migrate
@ -527,7 +505,7 @@ class MigratorTest < ActiveRecord::TestCase
end
def test_get_all_versions
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
_, migrator = migrator_class(3)
migrator = migrator.new("valid", schema_migration)

View File

@ -21,24 +21,24 @@ class MultiDbMigratorTest < ActiveRecord::TestCase
def setup
super
@connection_a = ActiveRecord::Base.connection
@connection_b = ARUnit2Model.connection
@pool_a = ActiveRecord::Base.connection_pool
@pool_b = ARUnit2Model.connection_pool
@connection_a.schema_migration.create_table
@connection_b.schema_migration.create_table
@pool_a.schema_migration.create_table
@pool_b.schema_migration.create_table
@connection_a.schema_migration.delete_all_versions rescue nil
@connection_b.schema_migration.delete_all_versions rescue nil
@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 = @connection_a.schema_migration
@internal_metadata_a = @connection_a.internal_metadata
@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 = @connection_b.schema_migration
@internal_metadata_b = @connection_b.internal_metadata
@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"]]
@ -56,8 +56,8 @@ class MultiDbMigratorTest < ActiveRecord::TestCase
end
teardown do
@connection_a.schema_migration.delete_all_versions rescue nil
@connection_b.schema_migration.delete_all_versions rescue nil
@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
@ -70,9 +70,9 @@ class MultiDbMigratorTest < ActiveRecord::TestCase
def test_schema_migration_is_different_for_different_connections
assert_not_equal @schema_migration_a, @schema_migration_b
assert_not_equal @schema_migration_a.connection, @schema_migration_b.connection
assert_equal "ActiveRecord::Base", @schema_migration_a.connection.pool.pool_config.connection_name
assert_equal "ARUnit2Model", @schema_migration_b.connection.pool.pool_config.connection_name
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
@ -157,17 +157,17 @@ class MultiDbMigratorTest < ActiveRecord::TestCase
migrator = migrator.new(@path_a, @schema_migration_a, @internal_metadata_a)
@schema_migration_a.drop_table
assert_not @connection_a.table_exists?("schema_migrations")
assert_not @pool_a.connection.table_exists?("schema_migrations")
migrator.migrate(1)
assert @connection_a.table_exists?("schema_migrations")
assert @pool_a.connection.table_exists?("schema_migrations")
_, migrator = migrator_class(3)
migrator = migrator.new(@path_b, @schema_migration_b, @internal_metadata_b)
@schema_migration_b.drop_table
assert_not @connection_b.table_exists?("schema_migrations")
assert_not @pool_b.connection.table_exists?("schema_migrations")
migrator.migrate(1)
assert @connection_b.table_exists?("schema_migrations")
assert @pool_b.connection.table_exists?("schema_migrations")
end
def test_migrator_forward

View File

@ -8,10 +8,10 @@ class SchemaDumperTest < ActiveRecord::TestCase
self.use_transactional_tests = false
setup do
@schema_migration = ActiveRecord::Base.connection.schema_migration
@schema_migration = ActiveRecord::Base.connection_pool.schema_migration
@schema_migration.create_table
ARUnit2Model.connection.schema_migration.create_table
ARUnit2Model.connection_pool.schema_migration.create_table
end
def standard_dump
@ -160,7 +160,7 @@ class SchemaDumperTest < ActiveRecord::TestCase
end
def test_schema_dump_with_regexp_ignored_table
output = dump_all_table_schema([/^courses/], connection: ARUnit2Model.connection)
output = dump_all_table_schema([/^courses/], pool: ARUnit2Model.connection_pool)
assert_no_match %r{create_table "courses"}, output
assert_match %r{create_table "colleges"}, output
assert_no_match %r{create_table "schema_migrations"}, output
@ -566,7 +566,7 @@ class SchemaDumperTest < ActiveRecord::TestCase
migration.migrate(:up)
stream = StringIO.new
output = ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream).string
output = ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, stream).string
assert_match %r{create_table "omg_cats"}, output
assert_no_match %r{create_table "cats"}, output

View File

@ -71,9 +71,9 @@ module ActiveRecord
def test_raises_an_error_when_called_with_protected_environment
protected_environments = ActiveRecord::Base.protected_environments
current_env = ActiveRecord::Base.connection.migration_context.current_environment
current_env = ActiveRecord::Base.connection_pool.migration_context.current_environment
ActiveRecord::Base.connection.internal_metadata[:environment] = current_env
ActiveRecord::Base.connection_pool.internal_metadata[:environment] = current_env
assert_called_on_instance_of(
ActiveRecord::MigrationContext,
@ -97,9 +97,9 @@ module ActiveRecord
def test_raises_an_error_when_called_with_protected_environment_which_name_is_a_symbol
protected_environments = ActiveRecord::Base.protected_environments
current_env = ActiveRecord::Base.connection.migration_context.current_environment
current_env = ActiveRecord::Base.connection_pool.migration_context.current_environment
ActiveRecord::Base.connection.internal_metadata[:environment] = current_env
ActiveRecord::Base.connection_pool.internal_metadata[:environment] = current_env
assert_called_on_instance_of(
ActiveRecord::MigrationContext,
@ -121,9 +121,9 @@ module ActiveRecord
end
def test_raises_an_error_if_no_migrations_have_been_made
connection = ActiveRecord::Base.connection
internal_metadata = connection.internal_metadata
schema_migration = connection.schema_migration
pool = ActiveRecord::Base.connection_pool
internal_metadata = pool.internal_metadata
schema_migration = pool.schema_migration
schema_migration.create_table
schema_migration.create_version("1")
@ -135,17 +135,18 @@ module ActiveRecord
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!("arunit")
end
ensure
pool.automatic_reconnect = true
schema_migration.delete_version("1")
internal_metadata.create_table
end
private
def recreate_metadata_tables
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = ActiveRecord::Base.connection_pool.schema_migration
schema_migration.drop_table
schema_migration.create_table
internal_metadata = ActiveRecord::Base.connection.internal_metadata
internal_metadata = ActiveRecord::Base.connection_pool.internal_metadata
internal_metadata.drop_table
internal_metadata.create_table
end
@ -161,22 +162,22 @@ module ActiveRecord
with_multi_db_configurations(env) do
protected_environments = ActiveRecord::Base.protected_environments
current_env = ActiveRecord::Base.connection.migration_context.current_environment
current_env = ActiveRecord::Base.connection_pool.migration_context.current_environment
assert_equal current_env, env
ActiveRecord::Base.establish_connection(:primary)
ActiveRecord::Base.connection.internal_metadata.create_table_and_set_flags(current_env)
ActiveRecord::Base.connection_pool.internal_metadata.create_table_and_set_flags(current_env)
ActiveRecord::Base.establish_connection(:secondary)
ActiveRecord::Base.connection.internal_metadata.create_table_and_set_flags(current_env)
ActiveRecord::Base.connection_pool.internal_metadata.create_table_and_set_flags(current_env)
assert_not_includes protected_environments, current_env
# Assert not raises
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!(env)
ActiveRecord::Base.establish_connection(:secondary)
connection = ActiveRecord::Base.connection
schema_migration = connection.schema_migration
pool = ActiveRecord::Base.connection_pool
schema_migration = pool.schema_migration
schema_migration.create_table
schema_migration.create_version("1")
@ -211,8 +212,8 @@ module ActiveRecord
ensure
[:primary, :secondary].each do |db|
ActiveRecord::Base.establish_connection(db)
ActiveRecord::Base.connection.schema_migration.delete_all_versions
ActiveRecord::Base.connection.internal_metadata.delete_all_entries
ActiveRecord::Base.connection_pool.schema_migration.delete_all_versions
ActiveRecord::Base.connection_pool.internal_metadata.delete_all_entries
end
ActiveRecord::Base.configurations = old_configurations
ActiveRecord::Base.establish_connection(:arunit)
@ -1259,7 +1260,7 @@ module ActiveRecord
class DatabaseTasksMigrateStatusTest < DatabaseTasksMigrationTestCase
if current_adapter?(:SQLite3Adapter) && !in_memory_db?
def setup
@schema_migration = ActiveRecord::Base.connection.schema_migration
@schema_migration = ActiveRecord::Base.connection_pool.schema_migration
end
def test_migrate_status_table
@ -1400,12 +1401,12 @@ module ActiveRecord
fixtures :courses, :colleges
def setup
connection = ARUnit2Model.connection
@schema_migration = connection.schema_migration
pool = ARUnit2Model.connection_pool
@schema_migration = pool.schema_migration
@schema_migration.create_table
@schema_migration.create_version(@schema_migration.table_name)
@internal_metadata = connection.internal_metadata
@internal_metadata = pool.internal_metadata
@internal_metadata.create_table
@internal_metadata[@internal_metadata.table_name] = nil

View File

@ -2,22 +2,22 @@
module SchemaDumpingHelper
def dump_table_schema(*tables)
connection = ActiveRecord::Base.connection
pool = ActiveRecord::Base.connection_pool
old_ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
ActiveRecord::SchemaDumper.ignore_tables = connection.data_sources - tables
ActiveRecord::SchemaDumper.ignore_tables = pool.connection.data_sources - tables
output, = capture_io do
ActiveRecord::SchemaDumper.dump(connection)
ActiveRecord::SchemaDumper.dump(pool)
end
output
ensure
ActiveRecord::SchemaDumper.ignore_tables = old_ignore_tables
end
def dump_all_table_schema(ignore_tables = [], connection: ActiveRecord::Base.connection)
def dump_all_table_schema(ignore_tables = [], pool: ActiveRecord::Base.connection_pool)
old_ignore_tables, ActiveRecord::SchemaDumper.ignore_tables = ActiveRecord::SchemaDumper.ignore_tables, ignore_tables
output, = capture_io do
ActiveRecord::SchemaDumper.dump(connection)
ActiveRecord::SchemaDumper.dump(pool)
end
output
ensure

View File

@ -5,6 +5,6 @@ require_relative "create_groups_migration"
# Writing and reading roles are required for the "previewing on the writer DB" test
ActiveRecord::Base.connects_to(database: { writing: :primary, reading: :replica })
ActiveRecord::Base.connection.migration_context.migrate
ActiveRecord::Base.connection_pool.migration_context.migrate
ActiveStorageCreateUsers.migrate(:up)
ActiveStorageCreateGroups.migrate(:up)

View File

@ -95,11 +95,11 @@ module Rails
# The name of the database adapter for the current environment.
property "Database adapter" do
ActiveRecord::Base.connection.pool.db_config.adapter
ActiveRecord::Base.connection_pool.db_config.adapter
end
property "Database schema version" do
ActiveRecord::Base.connection.migration_context.current_version rescue nil
ActiveRecord::Base.connection_pool.migration_context.current_version rescue nil
end
end
end

View File

@ -616,8 +616,8 @@ module ApplicationTests
app_file "db/schema.rb", ""
rails "db:setup"
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::Base.connection.internal_metadata[:environment]").strip }
development_environment = lambda { rails("runner", "puts ActiveRecord::Base.connection.internal_metadata[:environment]").strip }
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::Base.connection_pool.internal_metadata[:environment]").strip }
development_environment = lambda { rails("runner", "puts ActiveRecord::Base.connection_pool.internal_metadata[:environment]").strip }
assert_equal "test", test_environment.call
assert_equal "development", development_environment.call
@ -637,7 +637,7 @@ module ApplicationTests
app_file "db/schema.rb", ""
rails "db:test:prepare"
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::Base.connection.internal_metadata[:environment]").strip }
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::Base.connection_pool.internal_metadata[:environment]").strip }
assert_equal "test", test_environment.call
@ -739,8 +739,8 @@ module ApplicationTests
tables = rails("runner", "p ActiveRecord::Base.connection.tables.sort").strip
assert_equal('["ar_internal_metadata", "books", "recipes", "schema_migrations"]', tables)
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::Base.connection.internal_metadata[:environment]").strip }
development_environment = lambda { rails("runner", "puts ActiveRecord::Base.connection.internal_metadata[:environment]").strip }
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::Base.connection_pool.internal_metadata[:environment]").strip }
development_environment = lambda { rails("runner", "puts ActiveRecord::Base.connection_pool.internal_metadata[:environment]").strip }
assert_equal "development", development_environment.call
assert_equal "test", test_environment.call