-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop invalid index in safe_add_index #241
Conversation
Small update to fix the errors in pre-6.1 rails versions |
@ankane please show tis PR when enable -- add_index(:users, :email_last_open_at, {:algorithm=>:concurrently, :using=>:brin})
-- Lock timeout. Retrying in 10 seconds...
-- add_index(:users, :email_last_open_at, {:algorithm=>:concurrently, :using=>:brin})
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
PG::DuplicateTable: ERROR: relation "index_users_on_email_last_open_at" already exists
We must run index deletion before re-creation
|
@ankane For my case, this pull request is helpful because when indices are which in CI would not generate a proper database and make the tests fail ( and in a team of many developers, this creates issues where the solution is to manually remove the index from psql or in some other way, but it would be great to have this PR merged to automatically handle the case Thanks! |
Hi @dgsuarez, thanks for the PR, and sorry for the delay. Merged a version of this in the commit above (requires Active Record 7.1+, but may decide to support earlier versions before the release). |
… and Active Record < 7.1 - #241 Co-authored-by: Diego Guerra <[email protected]>
Spent some more time on this and think it's better to call |
It looks like Also, this seems like a good option to have independent of StrongMigrations.remove_invalid_indexes = true |
Hi! Saw this TODO in the code, we've been running something similar in our codebase, so better contribute it back :)
Allow
safe_add_index
to be seamlessly retried.When the
add_index...concurrently
fails, the index is left in Postgres withINVALID
status.The new code drops the index if it's found and invalid. With this, if the migration fails because of a lock timeout for example, on retry it can work. Without it the retries always fail until you manually drop the index.