fix(dalgorm): make DropIndexes idempotent by skipping missing indexes#9001
Open
DoDiODev wants to merge 1 commit into
Open
fix(dalgorm): make DropIndexes idempotent by skipping missing indexes#9001DoDiODev wants to merge 1 commit into
DoDiODev wants to merge 1 commit into
Conversation
GORM's DropIndex emits a bare `DROP INDEX` without `IF EXISTS`, which makes PostgreSQL fail with SQLSTATE 42704 when the target index is absent (e.g. it was already removed by an earlier column rewrite via ChangeColumnsType). Guard each drop with HasIndex so the operation is idempotent and database-neutral, without swallowing genuine errors. Signed-off-by: DoDiODev <DoDiDev@proton.me>
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.
Summary
Make
Dalgorm.DropIndexesidempotent by skipping indexes that are not present, so dropping an already-absent index no longer aborts a migration.Problem
Dalgorm.DropIndexescalls GORM'sMigrator().DropIndex, which emits a bareDROP INDEXwithoutIF EXISTS. On PostgreSQL this fails hard withERROR: index "..." does not exist (SQLSTATE 42704)when the target index is missing, causing the migrator to panic and the server to crash on startup.This is reproducible on PostgreSQL with the migration
change _tool_sonarqube_issue_code_blocks.component type to text, which unconditionally dropsidx__tool_sonarqube_issue_code_blocks_component. That index is no longer present after the earliermodify component type to varchar(500)migration rewrites the column viaChangeColumnsType(rename column, then AutoMigrate skips the name-colliding index, then the old column and its index are dropped). The result is a table withcomponent varchar(500)and no component index, so the later unconditionalDropIndexesaborts.Observed panic:
Fix
Guard each drop with
migrator.HasIndex(table, indexName)and skip indexes that do not exist. This makes the operation idempotent and database-neutral while still surfacing genuine drop errors (it does not blanket-swallow errors).Related
chore(docker): bump PostgreSQL to 18.1 and MySQL to 8.4.10) surfaces this crash during its PostgreSQL manual gate and recommends this fix be applied first. The two PRs are functionally independent and do not touch overlapping files.Testing
go build ./impls/dalgorm/...A DB-backed unit test was intentionally omitted to avoid introducing an sqlite driver dependency that is not currently in
go.mod.Rollback
Revert this single-file change; behaviour returns to the unconditional drop.