Skip to content

Commit

Permalink
Added ignored_databases option
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 28, 2024
1 parent 95c82f8 commit ab8c42a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 1.7.1 (unreleased)

- Added `ignored_databases` option
- Added warning for unsupported adapter
- Updated instructions for removing a column to append to `ignored_columns`

## 1.7.0 (2024-01-05)
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,14 @@ By default, checks are disabled when migrating down. Enable them with:
StrongMigrations.check_down = true
```

## Multiple Databases

Disable all functionality for specific databases with: [unreleased]

```ruby
StrongMigrations.ignored_databases += [:catalog]
```

## Custom Messages

To customize specific messages, create an initializer with:
Expand Down
3 changes: 2 additions & 1 deletion lib/strong_migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class << self
:target_postgresql_version, :target_mysql_version, :target_mariadb_version,
:enabled_checks, :lock_timeout, :statement_timeout, :check_down, :target_version,
:safe_by_default, :target_sql_mode, :lock_timeout_retries, :lock_timeout_retry_delay,
:alphabetize_schema
:alphabetize_schema, :ignored_databases
attr_writer :lock_timeout_limit
end
self.auto_analyze = false
Expand All @@ -40,6 +40,7 @@ class << self
self.safe_by_default = false
self.check_down = false
self.alphabetize_schema = false
self.ignored_databases = []

# private
def self.developer_env?
Expand Down
24 changes: 24 additions & 0 deletions lib/strong_migrations/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def self.safety_assured
end

def perform(method, *args)
return if !enabled?
check_adapter
check_version_supported
set_timeouts
check_lock_timeout
Expand Down Expand Up @@ -131,6 +133,28 @@ def version_safe?

private

def enabled?
return true if StrongMigrations.ignored_databases.empty?

# Active Record 6.0 supports multiple databases
# but connection.pool.spec.name always returns "primary"
# in migrations with rails db:migrate
if ActiveRecord::VERSION::STRING.to_f < 6.1
# error class is not shown in db:migrate output so ensure message is descriptive
raise StrongMigrations::Error, "StrongMigrations.ignored_databases is not supported for Active Record < 6.1"
end

!StrongMigrations.ignored_databases.map(&:to_s).include?(connection.pool.db_config.name)
end

def check_adapter
if adapter.instance_of?(Adapters::AbstractAdapter)
# TODO raise error in 2.0
# TODO add database name for Active Record 6.1+
warn "[strong_migrations] Unsupported adapter: #{connection.adapter_name}. Use StrongMigrations.ignored_databases to silence this warning."
end
end

def check_version_supported
return if defined?(@version_checked)

Expand Down
4 changes: 3 additions & 1 deletion lib/strong_migrations/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ def ddl_transaction(migration, *args)
# handle MigrationProxy class
migration = migration.send(:migration) if migration.respond_to?(:migration, true)

# retry migration since the entire transaction needs to be rerun
checker = migration.send(:strong_migrations_checker)
return super unless checker.send(:enabled?)

# retry migration since the entire transaction needs to be rerun
checker.retry_lock_timeouts(check_committed: true) do
# failed transaction reverts timeout, so need to re-apply
checker.timeouts_set = false
Expand Down
31 changes: 31 additions & 0 deletions test/multiple_databases_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ def test_target_version_unsupported
end
end

def test_ignored_databases
skip unless multiple_dbs?

with_ignored_databases([:animals]) do
with_database(:primary) do
assert_unsafe ExecuteArbitrarySQL
end
with_database(:animals) do
assert_safe ExecuteArbitrarySQL
end
end
end

def test_ignored_databases_unsupported
skip if multiple_dbs?

with_ignored_databases([:animals]) do
error = assert_raises(StrongMigrations::Error) do
assert_safe ExecuteArbitrarySQL
end
assert_equal "StrongMigrations.ignored_databases is not supported for Active Record < 6.1", error.message
end
end

private

def with_database(database, &block)
Expand All @@ -59,6 +83,13 @@ def with_database(database, &block)
ActiveRecord::Base.establish_connection(previous_db_config) if previous_db_config
end

def with_ignored_databases(databases)
StrongMigrations.ignored_databases = databases
yield
ensure
StrongMigrations.ignored_databases = []
end

def multiple_dbs?
ActiveRecord::VERSION::STRING.to_f >= 6.1
end
Expand Down

0 comments on commit ab8c42a

Please sign in to comment.