Skip to content

fix(dalgorm): make DropIndexes idempotent by skipping missing indexes#9001

Open
DoDiODev wants to merge 1 commit into
apache:mainfrom
DoDiODev:pr/idempotent-dropindexes
Open

fix(dalgorm): make DropIndexes idempotent by skipping missing indexes#9001
DoDiODev wants to merge 1 commit into
apache:mainfrom
DoDiODev:pr/idempotent-dropindexes

Conversation

@DoDiODev

@DoDiODev DoDiODev commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Make Dalgorm.DropIndexes idempotent by skipping indexes that are not present, so dropping an already-absent index no longer aborts a migration.

Problem

Dalgorm.DropIndexes calls GORM's Migrator().DropIndex, which emits a bare DROP INDEX without IF EXISTS. On PostgreSQL this fails hard with ERROR: 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 drops idx__tool_sonarqube_issue_code_blocks_component. That index is no longer present after the earlier modify component type to varchar(500) migration rewrites the column via ChangeColumnsType (rename column, then AutoMigrate skips the name-colliding index, then the old column and its index are dropped). The result is a table with component varchar(500) and no component index, so the later unconditional DropIndexes aborts.

Observed panic:

ERROR: index "idx__tool_sonarqube_issue_code_blocks_component" does not exist (SQLSTATE 42704)
DROP INDEX "idx__tool_sonarqube_issue_code_blocks_component"
panic: ... (500)
server/services.InitExecuteMigration()

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

Testing

  • go build ./impls/dalgorm/...
  • Manual PostgreSQL migration run: with the fix, the previously-crashing migration chain completes and the server reaches ready state; without it, PostgreSQL aborts on the missing index.

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.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant