Skip to content

Commit

Permalink
Added ignore option
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 28, 2024
1 parent 95c82f8 commit 1b0a225
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
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 `ignore` 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
2 changes: 1 addition & 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, :ignore
attr_writer :lock_timeout_limit
end
self.auto_analyze = false
Expand Down
25 changes: 25 additions & 0 deletions lib/strong_migrations/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def self.safety_assured
end

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

private

def enabled?
if StrongMigrations.ignore
# 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.ignore is not supported for Active Record < 6.1"
end

if StrongMigrations.ignore.map(&:to_s).include?(connection.pool.db_config.name)
return false
end
end

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.ignore to silence this warning."
end

true
end

def check_version_supported
return if defined?(@version_checked)

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_ignore
skip unless multiple_dbs?

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

def test_ignore_unsupported
skip if multiple_dbs?

with_ignore([:animals]) do
error = assert_raises(StrongMigrations::Error) do
assert_safe ExecuteArbitrarySQL
end
assert_equal "StrongMigrations.ignore 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_ignore(databases)
StrongMigrations.ignore = databases
yield
ensure
StrongMigrations.ignore = nil
end

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

0 comments on commit 1b0a225

Please sign in to comment.