Skip to content

Commit

Permalink
Made remove_invalid_indexes independent of safe_by_default
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 8, 2024
1 parent f8a55e6 commit 2daa2dc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/strong_migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class << self
self.check_down = false
self.alphabetize_schema = false
self.skip_databases = []
self.remove_invalid_indexes = false # does not affect safe_by_default
self.remove_invalid_indexes = false

# private
def self.developer_env?
Expand Down
1 change: 0 additions & 1 deletion lib/strong_migrations/safe_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def safe_by_default_method?(method)

def safe_add_index(*args, **options)
disable_transaction
remove_invalid_index_if_needed(*args, **options.merge(algorithm: :concurrently))
@migration.add_index(*args, **options.merge(algorithm: :concurrently))
end

Expand Down
28 changes: 12 additions & 16 deletions test/safe_by_default_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,21 @@ def test_add_index_invalid
end
end

# fail if same name but different options
error = assert_raises(ActiveRecord::StatementInvalid) do
migrate AddIndexUnique
end
assert_kind_of PG::DuplicateTable, error.cause

# TODO do not fail if already safe
error = assert_raises(ActiveRecord::StatementInvalid) do
migrate AddIndexConcurrently
end
assert_kind_of PG::DuplicateTable, error.cause

migrate AddIndexes
StrongMigrations.stub(:remove_invalid_indexes, true) do
# fail if same name but different options
error = assert_raises(ActiveRecord::StatementInvalid) do
migrate AddIndexUnique
end
assert_kind_of PG::DuplicateTable, error.cause

# fail if trying to add the same index in a future migration
error = assert_raises(ActiveRecord::StatementInvalid) do
migrate AddIndexes

# fail if trying to add the same index in a future migration
error = assert_raises(ActiveRecord::StatementInvalid) do
migrate AddIndexes
end
assert_kind_of PG::DuplicateTable, error.cause
end
assert_kind_of PG::DuplicateTable, error.cause

migrate AddIndexes, direction: :down
end
Expand Down
16 changes: 0 additions & 16 deletions test/timeouts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,6 @@ def test_lock_timeout_retries_add_index
migrate AddIndexConcurrently, direction: :down
end

def test_lock_timeout_retries_add_index_safe_by_default
skip unless postgresql?

StrongMigrations.safe_by_default = true

assert_raises(ActiveRecord::LockWaitTimeout) do
with_lock_timeout_retries do
migrate AddIndex
end
end

migrate AddIndex, direction: :down
ensure
StrongMigrations.safe_by_default = false
end

def test_lock_timeout_retries_add_index_remove_invalid_indexes
skip unless postgresql?

Expand Down

0 comments on commit 2daa2dc

Please sign in to comment.