Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavSokov committed Jul 2, 2024
1 parent a970630 commit 05576d8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 62 deletions.
46 changes: 14 additions & 32 deletions app/controllers/actual_db_schema/migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def load_migrations
migration = indexed_phantom_migrations[version]
next unless migration

@migrations << build_migration_hash(status, version, migration)
@migrations << build_migration_struct(status, migration)
end
end
end

def load_migration(version, database)
ActualDbSchema.for_each_db_connection do
next unless db_config[:database] == database
next unless ActualDbSchema.db_config[:database] == database

context = prepare_context
migration = find_migration_in_context(context, version)
Expand All @@ -51,7 +51,7 @@ def load_migration(version, database)

def rollback_migration(version, database)
ActualDbSchema.for_each_db_connection do
next unless db_config[:database] == database
next unless ActualDbSchema.db_config[:database] == database

context = prepare_context
if context.migrations.detect { |m| m.version.to_s == version }
Expand All @@ -66,42 +66,24 @@ def find_migration_in_context(context, version)
return unless migration

status = context.migrations_status.detect { |_s, v| v.to_s == version }&.first || "unknown"
build_migration_hash(status, migration.version.to_s, migration)
build_migration_struct(status, migration)
end

def prepare_context
context = fetch_migration_context
context = ActualDbSchema.fetch_migration_context
context.extend(ActualDbSchema::Patches::MigrationContext)
context
end

def build_migration_hash(status, version, migration)
{
status: status,
version: version,
name: migration.name,
branch: branch_for(version),
database: db_config[:database],
filename: migration.filename
}
end

def 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 db_config
if ActiveRecord::Base.respond_to?(:connection_db_config)
ActiveRecord::Base.connection_db_config.configuration_hash
else
ActiveRecord::Base.connection_config
end
def build_migration_struct(status, migration)
ActualDbSchema::MigrationStruct.new(
status,
migration.version.to_s,
migration.name,
branch_for(migration.version),
ActualDbSchema.db_config[:database],
migration.filename
)
end

def branch_for(version)
Expand Down
19 changes: 19 additions & 0 deletions lib/actual_db_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class << self
auto_rollback_disabled: ENV["ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED"].present?
}

MigrationStruct = Struct.new(:status, :version, :name, :branch, :database, :filename)

def self.migrated_folder
migrated_folders.first
end
Expand Down Expand Up @@ -59,6 +61,23 @@ 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
else
ActiveRecord::Base.connection_config
end
end

def self.migration_filename(fullpath)
fullpath.split("/").last
end
Expand Down
12 changes: 1 addition & 11 deletions lib/actual_db_schema/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,10 @@ def call_impl
end

def context
@context ||= fetch_migration_context.tap do |c|
@context ||= ActualDbSchema.fetch_migration_context.tap do |c|
c.extend(ActualDbSchema::Patches::MigrationContext)
end
end

def 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
end
end
end
11 changes: 1 addition & 10 deletions lib/actual_db_schema/commands/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,11 @@ def preambule
puts "\nPhantom migrations\n\n"
puts "Below is a list of irrelevant migrations executed in unmerged branches."
puts "To bring your database schema up to date, the migrations marked as \"up\" should be rolled back."
database_path = db_config[:database]
puts "\ndatabase: #{database_path}\n\n"
puts "\ndatabase: #{ActualDbSchema.db_config[:database]}\n\n"
puts header.join(" ")
puts "-" * separator_width
end

def db_config
if ActiveRecord::Base.respond_to?(:connection_db_config)
ActiveRecord::Base.connection_db_config.configuration_hash
else
ActiveRecord::Base.connection_config
end
end

def separator_width
header.map(&:length).sum + (header.size - 1) * 2
end
Expand Down
10 changes: 1 addition & 9 deletions lib/actual_db_schema/patches/migration_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def show_info_for(migration)
puts "\n[ActualDbSchema] A phantom migration was found and is about to be rolled back."
puts "Please make a decision from the options below to proceed.\n\n"
puts "Branch: #{branch_for(migration.version.to_s)}"
puts "Database: #{db_config[:database]}"
puts "Database: #{ActualDbSchema.db_config[:database]}"
puts "Version: #{migration.version}\n\n"
puts File.read(migration.filename)
end
Expand All @@ -69,14 +69,6 @@ def migrate(migration)
migrator.migrate
end

def db_config
@db_config ||= if ActiveRecord::Base.respond_to?(:connection_db_config)
ActiveRecord::Base.connection_db_config.configuration_hash
else
ActiveRecord::Base.connection_config
end
end

def branch_for(version)
metadata.fetch(version, {})[:branch] || "unknown"
end
Expand Down

0 comments on commit 05576d8

Please sign in to comment.