From 45549aa17f867ae526bf4b0e61b0af398530cee8 Mon Sep 17 00:00:00 2001 From: DoDiODev Date: Mon, 20 Jul 2026 15:25:26 +0200 Subject: [PATCH] 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 --- backend/impls/dalgorm/dalgorm.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/impls/dalgorm/dalgorm.go b/backend/impls/dalgorm/dalgorm.go index ba635355f41..169825050bb 100644 --- a/backend/impls/dalgorm/dalgorm.go +++ b/backend/impls/dalgorm/dalgorm.go @@ -452,9 +452,18 @@ func (d *Dalgorm) RenameTable(oldName, newName string) errors.Error { // DropIndexes drops indexes for specified table func (d *Dalgorm) DropIndexes(table string, indexNames ...string) errors.Error { + migrator := d.db.Migrator() for _, indexName := range indexNames { - err := d.db.Migrator().DropIndex(table, indexName) - if err != nil { + // Skip indexes that are not present so the operation is idempotent and + // database-neutral. GORM's DropIndex emits a bare `DROP INDEX` (without + // `IF EXISTS`), which makes PostgreSQL fail with SQLSTATE 42704 when the + // index is missing (e.g. it was already removed by an earlier column + // rewrite). Guarding with HasIndex keeps genuine errors from being + // swallowed while making repeated/partial migrations safe. + if !migrator.HasIndex(table, indexName) { + continue + } + if err := migrator.DropIndex(table, indexName); err != nil { return d.convertGormError(err) } }