From e796256a6009081c4f7ea5cf1341352971f7c88f Mon Sep 17 00:00:00 2001 From: Vladislav Sokov Date: Fri, 5 Jul 2024 18:21:28 +0300 Subject: [PATCH] fixup! fixup! fixup! Add DatabaseConnection class --- lib/actual_db_schema.rb | 24 --------------------- lib/actual_db_schema/commands/base.rb | 8 ++++--- lib/actual_db_schema/commands/list.rb | 4 ++++ lib/actual_db_schema/commands/rollback.rb | 4 ++-- lib/actual_db_schema/database_connection.rb | 2 +- lib/actual_db_schema/migration.rb | 6 +++--- lib/tasks/db.rake | 12 +++++------ 7 files changed, 21 insertions(+), 39 deletions(-) diff --git a/lib/actual_db_schema.rb b/lib/actual_db_schema.rb index 7fd9c5b..2f30f8e 100644 --- a/lib/actual_db_schema.rb +++ b/lib/actual_db_schema.rb @@ -60,15 +60,6 @@ def self.migrations_paths end end - def self.fetch_migration_context - ar_version = Gem::Version.new(ActiveRecord::VERSION::STRING) - if ar_version >= Gem::Version.new("7.2.0") || (ar_version >= Gem::Version.new("7.1.0") && ar_version.prerelease?) - ActiveRecord::Base.connection_pool.migration_context - else - ActiveRecord::Base.connection.migration_context - end - end - def self.db_config if ActiveRecord::Base.respond_to?(:connection_db_config) ActiveRecord::Base.connection_db_config.configuration_hash @@ -80,21 +71,6 @@ def self.db_config def self.migration_filename(fullpath) fullpath.split("/").last end - - def self.for_each_db_connection - configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env) - configs.each do |db_config| - config = db_config.respond_to?(:config) ? db_config.config : db_config - ActiveRecord::Base.establish_connection(config) - yield - end - end - - def self.prepare_context - fetch_migration_context.tap do |c| - c.extend(ActualDbSchema::Patches::MigrationContext) - end - end end ActiveRecord::MigrationProxy.prepend(ActualDbSchema::Patches::MigrationProxy) diff --git a/lib/actual_db_schema/commands/base.rb b/lib/actual_db_schema/commands/base.rb index 9ca09da..fa7917b 100644 --- a/lib/actual_db_schema/commands/base.rb +++ b/lib/actual_db_schema/commands/base.rb @@ -4,6 +4,10 @@ module ActualDbSchema module Commands # Base class for all commands class Base + def initialize(context: nil) + @context = context + end + def call unless ActualDbSchema.config.fetch(:enabled, true) raise "ActualDbSchema is disabled. Set ActualDbSchema.config[:enabled] = true to enable it." @@ -18,9 +22,7 @@ def call_impl raise NotImplementedError end - def context - @context ||= ActualDbSchema.prepare_context - end + attr_reader :context end end end diff --git a/lib/actual_db_schema/commands/list.rb b/lib/actual_db_schema/commands/list.rb index e36c9ee..1c07c04 100644 --- a/lib/actual_db_schema/commands/list.rb +++ b/lib/actual_db_schema/commands/list.rb @@ -4,6 +4,10 @@ module ActualDbSchema module Commands # Shows the list of phantom migrations class List < Base + def initialize(context: nil) + super(context: context) + end + private def call_impl diff --git a/lib/actual_db_schema/commands/rollback.rb b/lib/actual_db_schema/commands/rollback.rb index e0fb7ef..b87734a 100644 --- a/lib/actual_db_schema/commands/rollback.rb +++ b/lib/actual_db_schema/commands/rollback.rb @@ -4,9 +4,9 @@ module ActualDbSchema module Commands # Rolls back all phantom migrations class Rollback < Base - def initialize(manual_mode: false) + def initialize(manual_mode: false, context: nil) @manual_mode = manual_mode || manual_mode_default? - super() + super(context: context) end private diff --git a/lib/actual_db_schema/database_connection.rb b/lib/actual_db_schema/database_connection.rb index baf7e60..dcc83da 100644 --- a/lib/actual_db_schema/database_connection.rb +++ b/lib/actual_db_schema/database_connection.rb @@ -5,7 +5,7 @@ module ActualDbSchema class DatabaseConnection include Singleton - def for_each_db_connection + def for_each_migration_context configs.each do |db_config| establish_connection(db_config) yield context diff --git a/lib/actual_db_schema/migration.rb b/lib/actual_db_schema/migration.rb index d734fe2..a4e5453 100644 --- a/lib/actual_db_schema/migration.rb +++ b/lib/actual_db_schema/migration.rb @@ -22,7 +22,7 @@ def self.rollback(version, database) def all migrations = [] - DatabaseConnection.instance.for_each_db_connection do |context| + DatabaseConnection.instance.for_each_migration_context do |context| indexed_migrations = context.migrations.index_by { |m| m.version.to_s } context.migrations_status.each do |status, version| @@ -35,7 +35,7 @@ def all end def find(version, database) - DatabaseConnection.instance.for_each_db_connection do |context| + DatabaseConnection.instance.for_each_migration_context do |context| next unless ActualDbSchema.db_config[:database] == database migration = find_migration_in_context(context, version) @@ -45,7 +45,7 @@ def find(version, database) end def rollback(version, database) - DatabaseConnection.instance.for_each_db_connection do |context| + DatabaseConnection.instance.for_each_migration_context do |context| next unless ActualDbSchema.db_config[:database] == database if context.migrations.detect { |m| m.version.to_s == version } diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 3379f06..8b7fbf2 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -4,8 +4,8 @@ namespace :db do desc "Rollback migrations that were run inside not a merged branch." task rollback_branches: :load_config do ActualDbSchema.failed = [] - ActualDbSchema.for_each_db_connection do - ActualDbSchema::Commands::Rollback.new.call + ActualDbSchema::DatabaseConnection.instance.for_each_migration_context do |context| + ActualDbSchema::Commands::Rollback.new(context: context).call end end @@ -13,16 +13,16 @@ namespace :db do desc "Manually rollback phantom migrations one by one" task manual: :load_config do ActualDbSchema.failed = [] - ActualDbSchema.for_each_db_connection do - ActualDbSchema::Commands::Rollback.new(manual_mode: true).call + ActualDbSchema::DatabaseConnection.instance.for_each_migration_context do |context| + ActualDbSchema::Commands::Rollback.new(manual_mode: true, context: context).call end end end desc "List all phantom migrations - non-relevant migrations that were run inside not a merged branch." task phantom_migrations: :load_config do - ActualDbSchema.for_each_db_connection do - ActualDbSchema::Commands::List.new.call + ActualDbSchema::DatabaseConnection.instance.for_each_migration_context do |context| + ActualDbSchema::Commands::List.new(context: context).call end end