opens new pool after calling migrator close to avoid sql: database is…#2040
Merged
Conversation
… closed error - retry of failed migrations works now
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to make failed migrations retryable by resetting schema_migrations using a fresh DB connection after migrator.Close() (which closes the underlying *sql.DB). It also introduces a new migration that removes indexes/columns from component_dependencies and attempts to enforce deduplication at the schema level.
Changes:
- Update migration runner to create a fresh pool/DB for resetting
schema_migrationsafter a failed migration. - Add a SQL migration that drops obsolete indexes/columns on
component_dependencies, de-dupes rows, and replaces the surrogate PK with a composite primary key.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| database/migrations/20260521095426_remove_obsolete_indexes_and_columns_from_component_dependencies.up.sql | Drops columns/indexes and replaces component_dependencies PK with a composite PK after deduplication. |
| database/migrations.go | On migration failure, closes migrator and uses a new pool to reset schema_migrations dirty/version state. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
82
to
86
| // Release the migrator's connection (advisory lock + any open tx) before | ||
| // touching schema_migrations on the same pool — with MaxOpenConns=1 this | ||
| // would otherwise deadlock. | ||
| // touching schema_migrations — migrator.Close() also closes the underlying | ||
| // sql.DB it was given, so we need a fresh connection for the reset. | ||
| migrator.Close() | ||
| // clear dirty flag and restore version so the migration can be retried — safe in postgres since DDL is transactional |
Comment on lines
+87
to
+93
| resetCfg := GetPoolConfigFromEnv() | ||
| resetCfg.MaxOpenConns = 1 | ||
| resetCfg.MinConns = 0 | ||
| resetDB := NewGormDB(NewPgxConnPool(resetCfg)) | ||
| if resetSQLDB, dbErr := resetDB.DB(); dbErr == nil { | ||
| defer resetSQLDB.Close() | ||
| if _, err = resetSQLDB.Exec("UPDATE schema_migrations SET dirty = false, version = $1", versionBefore); err != nil { |
| if _, err = resetSQLDB.Exec("UPDATE schema_migrations SET dirty = false, version = $1", versionBefore); err != nil { | ||
| monitoring.Alert("failed to reset migration state after failed migration", err) | ||
| } | ||
| slog.Info("successfully reset migration state - feel free to try again") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
… closed error - retry of failed migrations works now