Skip to content

Commit

Permalink
Added experimental remove_invalid_indexes option
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 7, 2024
1 parent ff41ced commit eafbf0a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## 2.1.0 (unreleased)

- Added `skip_databases` option
- Added experimental `remove_invalid_indexes` option
- Added warning for unsupported adapters
- Improved output for `db:forward`, `db:rollback`, `db:migrate:up`, and `db:migrate:down`
- Improved handling of invalid indexes with `add_index` and `safe_by_default`

## 2.0.2 (2024-10-30)

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, :skip_databases
:alphabetize_schema, :skip_databases, :remove_invalid_indexes
attr_writer :lock_timeout_limit
end
self.auto_analyze = false
Expand All @@ -41,6 +41,7 @@ class << self
self.check_down = false
self.alphabetize_schema = false
self.skip_databases = []
self.remove_invalid_indexes = false

# private
def self.developer_env?
Expand Down
12 changes: 12 additions & 0 deletions lib/strong_migrations/checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,5 +460,17 @@ def migration_suffix
def model_name(table)
table.to_s.classify
end

def remove_invalid_index(*args, **options)
return unless StrongMigrations.remove_invalid_indexes && postgresql? && direction == :up

table, columns = args
index_name = options.fetch(:name, connection.index_name(table, columns))
if adapter.index_invalid?(table, index_name)
@migration.say("Removing invalid index")
# TODO pass index schema for extra safety?
@migration.remove_index(table, **{name: index_name}.merge(options.slice(:algorithm)))
end
end
end
end
14 changes: 1 addition & 13 deletions lib/strong_migrations/safe_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,10 @@ def safe_by_default_method?(method)

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

def remove_invalid_index(*args, **options)
return if direction != :up

table, columns = args
index_name = options.fetch(:name, @migration.connection.index_name(table, columns))
if adapter.index_invalid?(table, index_name)
@migration.say("Removing invalid index")
# TODO pass index schema for extra safety?
safe_remove_index(table, name: index_name)
end
end

def safe_remove_index(*args, **options)
disable_transaction
@migration.remove_index(*args, **options.merge(algorithm: :concurrently))
Expand Down
8 changes: 7 additions & 1 deletion test/safe_by_default_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ def test_add_index_invalid
end
end

assert_safe AddIndex
assert_raises(ActiveRecord::StatementInvalid) do
migrate AddIndex
end

StrongMigrations.stub(:remove_invalid_indexes, true) do
assert_safe AddIndex
end
end

def test_add_index_extra_arguments
Expand Down

0 comments on commit eafbf0a

Please sign in to comment.