From c26ace63e7099f242a1d98e51962869dabb26340 Mon Sep 17 00:00:00 2001 From: Vladislav Sokov Date: Fri, 5 Jul 2024 18:54:01 +0300 Subject: [PATCH] fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Refactoring --- CHANGELOG.md | 4 +++- README.md | 9 +-------- lib/actual_db_schema.rb | 4 ++-- lib/actual_db_schema/commands/base.rb | 6 +++--- lib/actual_db_schema/commands/list.rb | 4 ---- lib/actual_db_schema/commands/rollback.rb | 4 ++-- lib/actual_db_schema/migration.rb | 6 +++--- .../{database_connection.rb => migration_context.rb} | 4 ++-- lib/tasks/db.rake | 12 ++++++------ test/dummy_app/db/secondary_schema.rb | 2 +- 10 files changed, 23 insertions(+), 32 deletions(-) rename lib/actual_db_schema/{database_connection.rb => migration_context.rb} (94%) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc29883..f760bb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ -## [0.7.6] - 2024-07-02 +## [0.7.6] - 2024-07-05 - Added UI +- Added environment variable `ACTUAL_DB_SCHEMA_UI_ENABLED` to enable/disable the UI in specific environments +- Added configuration option `ActualDbSchema.config[:ui_enabled]` to enable/disable the UI in specific environments ## [0.7.5] - 2024-06-20 - Added db:rollback_migrations:manual task to manually rolls back phantom migrations one by one diff --git a/README.md b/README.md index 4d48090..bf0d3e8 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,10 @@ By default, the UI is enabled in the development environment. If you prefer to e ### 1. Using Environment Variable -Set the environment variables `ACTUAL_DB_SCHEMA_UI_ENABLED` and `ACTUAL_DB_SCHEMA_ENABLED` to `true`: +Set the environment variable `ACTUAL_DB_SCHEMA_UI_ENABLED` to `true`: ```sh export ACTUAL_DB_SCHEMA_UI_ENABLED=true -export ACTUAL_DB_SCHEMA_ENABLED=true ``` ### 2. Using Initializer @@ -88,16 +87,10 @@ Add the following line to your initializer file (`config/initializers/actual_db_ ```ruby ActualDbSchema.config[:ui_enabled] = true -ActualDbSchema.config[:enabled] = true ``` > With this option, the UI can be disabled for all environments or be enabled in specific ones. -> [!WARNING] -> ActualDbSchema.config[:enabled] = false -This option will completely disable the functionality of the gem in all environments - - ## Disabling Automatic Rollback By default, the automatic rollback of migrations is enabled. If you prefer to perform manual rollbacks, you can disable the automatic rollback in two ways: diff --git a/lib/actual_db_schema.rb b/lib/actual_db_schema.rb index 2f30f8e..b51cd45 100644 --- a/lib/actual_db_schema.rb +++ b/lib/actual_db_schema.rb @@ -7,7 +7,7 @@ require_relative "actual_db_schema/store" require_relative "actual_db_schema/version" require_relative "actual_db_schema/migration" -require_relative "actual_db_schema/database_connection" +require_relative "actual_db_schema/migration_context" require_relative "actual_db_schema/patches/migration_proxy" require_relative "actual_db_schema/patches/migrator" require_relative "actual_db_schema/patches/migration_context" @@ -26,7 +26,7 @@ class << self self.failed = [] self.config = { - enabled: Rails.env.development? || ENV["ACTUAL_DB_SCHEMA_ENABLED"].present?, + enabled: Rails.env.development?, auto_rollback_disabled: ENV["ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED"].present?, ui_enabled: Rails.env.development? || ENV["ACTUAL_DB_SCHEMA_UI_ENABLED"].present? } diff --git a/lib/actual_db_schema/commands/base.rb b/lib/actual_db_schema/commands/base.rb index fa7917b..fc49684 100644 --- a/lib/actual_db_schema/commands/base.rb +++ b/lib/actual_db_schema/commands/base.rb @@ -4,7 +4,9 @@ module ActualDbSchema module Commands # Base class for all commands class Base - def initialize(context: nil) + attr_reader :context + + def initialize(context) @context = context end @@ -21,8 +23,6 @@ def call def call_impl raise NotImplementedError 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 1c07c04..e36c9ee 100644 --- a/lib/actual_db_schema/commands/list.rb +++ b/lib/actual_db_schema/commands/list.rb @@ -4,10 +4,6 @@ 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 b87734a..3ea0850 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, context: nil) + def initialize(context, manual_mode: false) @manual_mode = manual_mode || manual_mode_default? - super(context: context) + super(context) end private diff --git a/lib/actual_db_schema/migration.rb b/lib/actual_db_schema/migration.rb index a4e5453..300cd30 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_migration_context do |context| + MigrationContext.instance.each 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_migration_context do |context| + MigrationContext.instance.each 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_migration_context do |context| + MigrationContext.instance.each do |context| next unless ActualDbSchema.db_config[:database] == database if context.migrations.detect { |m| m.version.to_s == version } diff --git a/lib/actual_db_schema/database_connection.rb b/lib/actual_db_schema/migration_context.rb similarity index 94% rename from lib/actual_db_schema/database_connection.rb rename to lib/actual_db_schema/migration_context.rb index dcc83da..dd5fd01 100644 --- a/lib/actual_db_schema/database_connection.rb +++ b/lib/actual_db_schema/migration_context.rb @@ -2,10 +2,10 @@ module ActualDbSchema # The class manages connections to each database and provides the appropriate migration context for each connection. - class DatabaseConnection + class MigrationContext include Singleton - def for_each_migration_context + def each configs.each do |db_config| establish_connection(db_config) yield context diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 8b7fbf2..2c42b8f 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::DatabaseConnection.instance.for_each_migration_context do |context| - ActualDbSchema::Commands::Rollback.new(context: context).call + ActualDbSchema::MigrationContext.instance.each do |context| + ActualDbSchema::Commands::Rollback.new(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::DatabaseConnection.instance.for_each_migration_context do |context| - ActualDbSchema::Commands::Rollback.new(manual_mode: true, context: context).call + ActualDbSchema::MigrationContext.instance.each do |context| + ActualDbSchema::Commands::Rollback.new(context, manual_mode: true).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::DatabaseConnection.instance.for_each_migration_context do |context| - ActualDbSchema::Commands::List.new(context: context).call + ActualDbSchema::MigrationContext.instance.each do |context| + ActualDbSchema::Commands::List.new(context).call end end diff --git a/test/dummy_app/db/secondary_schema.rb b/test/dummy_app/db/secondary_schema.rb index b200061..79b5c13 100644 --- a/test/dummy_app/db/secondary_schema.rb +++ b/test/dummy_app/db/secondary_schema.rb @@ -10,6 +10,6 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2013_09_06_111515) do +ActiveRecord::Schema.define(version: 0) do end