Skip to content

fix: only qualify DROP/ALTER INDEX with an explicit schema, never CURRENT_SCHEMA() - #353

Merged
tr1v3r merged 1 commit into
go-gorm:masterfrom
tr1v3r:fix/drop-index-current-schema
Sep 14, 2026
Merged

tr1v3r merged 1 commit into
go-gorm:masterfrom
tr1v3r:fix/drop-index-current-schema

Conversation

@tr1v3r

@tr1v3r tr1v3r commented Sep 14, 2026

Copy link
Copy Markdown
Member

Fixes #350.

Problem

Since v1.6.1 (PR #340), Migrator().DropIndex() and Migrator().RenameIndex() fail in two ways:

  1. No explicit schema (the common default): CurrentSchema returns clause.Expr{SQL: "CURRENT_SCHEMA()"}, and the new DROP INDEX ?.? / ALTER INDEX ?.? RENAME TO ? templates splice it into an identifier position:

    DROP INDEX CURRENT_SCHEMA()."idx_users_name"
    ERROR: syntax error at or near "CURRENT_SCHEMA" (SQLSTATE 42601)

    CURRENT_SCHEMA() is a function call; PostgreSQL cannot use it to qualify an identifier.

  2. Explicit schema (TableName() like public.users): the schema is returned as a plain string, which ? binding sends as a parameter:

    DROP INDEX $1."idx_users_name"

    also invalid — identifiers must be quoted, not bound.

This breaks AutoMigrate flows that drop/recreate indexes (e.g. gormigrate) and keeps the gorm core repo's postgres CI matrix red.

Fix

Qualify the index only when the table has an explicit schema, using a quoted identifier:

  • CurrentSchema returns a string (explicit schema from schema.table or TableExpr) → DROP INDEX "schema"."idx" / ALTER INDEX "schema"."idx" RENAME TO "idx_new"
  • otherwise → DROP INDEX "idx" / ALTER INDEX "idx" RENAME TO "idx_new" (pre-v1.6.1 behavior; the index resolves via the search_path)

Tests

New migrator_drop_index_test.go records the SQL through an injected gorm.ConnPool (no live PostgreSQL needed) and asserts both scenarios for DropIndex and RenameIndex. Verified red → green: pre-fix the tests fail with exactly the two invalid statements above; post-fix all pass, full package go test ./... green.

@tr1v3r

tr1v3r commented Sep 14, 2026

Copy link
Copy Markdown
Member Author

The red run-tests check here is a repo-wide workflow issue, not this change: the workflow pins go: 1.23.0 while master's go.mod requires go >= 1.25.0 (fails identically on #352). Proposed workflow fix in #354; this PR goes green once it lands (rebase or re-run).

For reviewers comparing with #351: this PR deliberately leaves CurrentSchema untouched, so all existing call sites keep the inline CURRENT_SCHEMA() comparison (evaluated by PostgreSQL itself) and no extra SELECT current_schema() round-trip is added per migrator operation. Only DropIndex/RenameIndex change their behavior — the two methods that are broken today. Both failure modes from #350 are covered by the new gorm.ConnPool-based regression tests (red → green verified).

@tr1v3r
tr1v3r force-pushed the fix/drop-index-current-schema branch 2 times, most recently from 3828a53 to d46e218 Compare September 14, 2026 15:47
@tr1v3r
tr1v3r force-pushed the fix/drop-index-current-schema branch from d46e218 to e2d2a21 Compare September 14, 2026 15:50
@tr1v3r
tr1v3r merged commit 1a2d67b into go-gorm:master Sep 14, 2026
2 checks passed
oierxjn added a commit to YourTongji/YourTJ-Hub that referenced this pull request Sep 17, 2026
v1.6.2 regressed Migrator().DropIndex/RenameIndex on PostgreSQL without
an explicit schema (CURRENT_SCHEMA() rendered in identifier position,
SQLSTATE 42601), failing the TestSchema* PG gate (go-gorm/postgres#350).
v1.6.3 carries the fix (go-gorm/postgres#353).

Dependabot's resolver still pins v1.6.2 after @dependabot recreate, so
bump manually on this branch to unblock ci-backend-pg.
oierxjn added a commit to YourTongji/YourTJ-Hub that referenced this pull request Sep 17, 2026
…s/gooseforum (#436)

* chore(deps): bump gorm.io/driver/postgres in /apps/gooseforum

Bumps [gorm.io/driver/postgres](https://github.com/go-gorm/postgres) from 1.6.0 to 1.6.2.
- [Commits](go-gorm/postgres@v1.6.0...v1.6.2)

---
updated-dependencies:
- dependency-name: gorm.io/driver/postgres
  dependency-version: 1.6.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* chore(deps): bump gorm.io/driver/postgres to v1.6.3

v1.6.2 regressed Migrator().DropIndex/RenameIndex on PostgreSQL without
an explicit schema (CURRENT_SCHEMA() rendered in identifier position,
SQLSTATE 42601), failing the TestSchema* PG gate (go-gorm/postgres#350).
v1.6.3 carries the fix (go-gorm/postgres#353).

Dependabot's resolver still pins v1.6.2 after @dependabot recreate, so
bump manually on this branch to unblock ci-backend-pg.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: oierxjn <1934672944@qq.com>
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.

Error when dropping index on table without explicit schema name

1 participant