From bc4e4fc102289a1dd1dfa2848fff4a0c705abf9a Mon Sep 17 00:00:00 2001 From: lihan3238 Date: Fri, 8 May 2026 23:24:23 +0800 Subject: [PATCH] fix(Migrator): add schema qualification to GetIndexes, RenameIndex, DropIndex GetIndexes, RenameIndex, and DropIndex did not use CurrentSchema to qualify index operations, causing incorrect results when tables reside in non-default schemas. This aligns them with HasIndex, HasTable, and HasColumn which already call CurrentSchema. Closes #226 --- migrator.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/migrator.go b/migrator.go index af44552..9cb1c23 100644 --- a/migrator.go +++ b/migrator.go @@ -172,9 +172,10 @@ func (m Migrator) CreateIndex(value interface{}, name string) error { func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error { + currentSchema, _ := m.CurrentSchema(stmt, stmt.Table) return m.DB.Exec( - "ALTER INDEX ? RENAME TO ?", - clause.Column{Name: oldName}, clause.Column{Name: newName}, + "ALTER INDEX ?.? RENAME TO ?", + currentSchema, clause.Column{Name: oldName}, clause.Column{Name: newName}, ).Error }) } @@ -187,7 +188,8 @@ func (m Migrator) DropIndex(value interface{}, name string) error { } } - return m.DB.Exec("DROP INDEX ?", clause.Column{Name: name}).Error + currentSchema, _ := m.CurrentSchema(stmt, stmt.Table) + return m.DB.Exec("DROP INDEX ?.?", currentSchema, clause.Column{Name: name}).Error }) } @@ -743,7 +745,8 @@ func (m Migrator) GetIndexes(value interface{}) ([]gorm.Index, error) { err := m.RunWithValue(value, func(stmt *gorm.Statement) error { result := make([]*Index, 0) - scanErr := m.queryRaw(indexSql, stmt.Table).Scan(&result).Error + currentSchema, curTable := m.CurrentSchema(stmt, stmt.Table) + scanErr := m.queryRaw(indexSql+" AND ct.relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = ?)", curTable, currentSchema).Scan(&result).Error if scanErr != nil { return scanErr }