Skip to content

Commit 9626294

Browse files
committed
fix(dalgorm): make DropIndexes idempotent by skipping missing indexes
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>
1 parent 5ebdee9 commit 9626294

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

backend/impls/dalgorm/dalgorm.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,18 @@ func (d *Dalgorm) RenameTable(oldName, newName string) errors.Error {
452452

453453
// DropIndexes drops indexes for specified table
454454
func (d *Dalgorm) DropIndexes(table string, indexNames ...string) errors.Error {
455+
migrator := d.db.Migrator()
455456
for _, indexName := range indexNames {
456-
err := d.db.Migrator().DropIndex(table, indexName)
457-
if err != nil {
457+
// Skip indexes that are not present so the operation is idempotent and
458+
// database-neutral. GORM's DropIndex emits a bare `DROP INDEX` (without
459+
// `IF EXISTS`), which makes PostgreSQL fail with SQLSTATE 42704 when the
460+
// index is missing (e.g. it was already removed by an earlier column
461+
// rewrite). Guarding with HasIndex keeps genuine errors from being
462+
// swallowed while making repeated/partial migrations safe.
463+
if !migrator.HasIndex(table, indexName) {
464+
continue
465+
}
466+
if err := migrator.DropIndex(table, indexName); err != nil {
458467
return d.convertGormError(err)
459468
}
460469
}

0 commit comments

Comments
 (0)