Skip to content

Commit

Permalink
fixup! fixup! fixup! Add DatabaseConnection class
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavSokov committed Jul 5, 2024
1 parent ae996f9 commit e796256
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 39 deletions.
24 changes: 0 additions & 24 deletions lib/actual_db_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
8 changes: 5 additions & 3 deletions lib/actual_db_schema/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -18,9 +22,7 @@ def call_impl
raise NotImplementedError
end

def context
@context ||= ActualDbSchema.prepare_context
end
attr_reader :context
end
end
end
4 changes: 4 additions & 0 deletions lib/actual_db_schema/commands/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/actual_db_schema/commands/rollback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/actual_db_schema/database_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/actual_db_schema/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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)
Expand All @@ -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 }
Expand Down
12 changes: 6 additions & 6 deletions lib/tasks/db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ 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

namespace :rollback_branches 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

Expand Down

0 comments on commit e796256

Please sign in to comment.