Skip to content

Commit

Permalink
Added skip_databases option - closes #255 and closes #281
Browse files Browse the repository at this point in the history
Co-authored-by: jenskdsgn <[email protected]>
  • Loading branch information
ankane and jenskdsgn committed Nov 1, 2024
1 parent ee29dea commit 6efff12
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.0 (unreleased)

- Added `skip_databases` option

## 2.0.2 (2024-10-30)

- Fixed migrations not running with Active Record 8 rc2
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,14 @@ By default, checks are disabled when migrating down. Enable them with:
StrongMigrations.check_down = true
```

## Multiple Databases

Skip checks for specific databases with: [unreleased]

```ruby
StrongMigrations.skip_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, :skip_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.skip_databases = []

# private
def self.developer_env?
Expand Down
6 changes: 6 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 yield if skip?

check_version_supported
set_timeouts
check_lock_timeout
Expand Down Expand Up @@ -129,6 +131,10 @@ def version_safe?
version && version <= StrongMigrations.start_after
end

def skip?
StrongMigrations.skip_databases.map(&:to_s).include?(connection.pool.db_config.name)
end

private

def check_version_supported
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, ...)
# 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 if checker.skip?

# 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
20 changes: 20 additions & 0 deletions test/multiple_databases_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ def test_target_version_unconfigured
assert_equal "StrongMigrations.target_version is not configured for :animals database", error.message
end

def test_skip_databases
with_skip_databases([:animals]) do
with_database(:primary) do
assert_unsafe CreateTableForce
end
with_database(:animals) do
assert_safe CreateTableForce, direction: :up
end
end
# confirm migration ran
assert ActiveRecord::Base.connection.table_exists?("admins")
migrate CreateTableForce, direction: :down
end

private

def with_database(database, &block)
Expand All @@ -44,4 +58,10 @@ def with_database(database, &block)
ActiveRecord::Base.configurations = previous_configurations if previous_configurations
ActiveRecord::Base.establish_connection(previous_db_config) if previous_db_config
end

def with_skip_databases(skip_databases)
StrongMigrations.stub(:skip_databases, skip_databases) do
yield
end
end
end

0 comments on commit 6efff12

Please sign in to comment.