Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Refact…
Browse files Browse the repository at this point in the history
…oring
  • Loading branch information
VladislavSokov committed Jul 5, 2024
1 parent e796256 commit c26ace6
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 32 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,21 @@ 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
Add the following line to your initializer file (`config/initializers/actual_db_schema.rb`):

```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:
Expand Down
4 changes: 2 additions & 2 deletions lib/actual_db_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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?
}
Expand Down
6 changes: 3 additions & 3 deletions lib/actual_db_schema/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,8 +23,6 @@ def call
def call_impl
raise NotImplementedError
end

attr_reader :context
end
end
end
4 changes: 0 additions & 4 deletions lib/actual_db_schema/commands/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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, context: nil)
def initialize(context, manual_mode: false)
@manual_mode = manual_mode || manual_mode_default?
super(context: context)
super(context)
end

private
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_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|
Expand All @@ -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)
Expand All @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
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::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

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

Expand Down
2 changes: 1 addition & 1 deletion test/dummy_app/db/secondary_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c26ace6

Please sign in to comment.