Skip to content

fix: RenameIndex and DropIndex failing on CURRENT_SCHEMA() - #351

Open
keif888 wants to merge 3 commits into
go-gorm:masterfrom
keif888:current_schema
Open

fix: RenameIndex and DropIndex failing on CURRENT_SCHEMA()#351
keif888 wants to merge 3 commits into
go-gorm:masterfrom
keif888:current_schema

Conversation

@keif888

@keif888 keif888 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor
  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

This pull request fixes issue #350, by retrieving the value of current_schema() when needed, and wrapping the result in a clause.Column within RenameIndex and DropIndex

PR #349 is another attempt to address this issue, in a slightly different way.
Which of these solutions is better I leave up to the Maintainers to decide.

User Case Description

As a developer I want to be able to drop indexes within a Postgres database using code as per the TestMigrateIndexes gorm testing function:

	if err := DB.Migrator().DropIndex(&IndexStruct{}, "Name"); err != nil {
		t.Fatalf("Failed to drop index for user's name, got err %v", err)
	}

As a developer I want to be able to rename indexes within a Postgres database using code as per the TestMigrateIndexes gorm testing function:

	if err := DB.Migrator().RenameIndex(&IndexStruct{}, "idx_index_structs_name", "idx_users_name_1"); err != nil {
		t.Fatalf("no error should happen when rename index, but got %v", err)
	}

Testing

This PR has been tested via the gorm test_all.sh script, with the go.mod file modified to use this version of the postgres driver, instead of the published version.
mod.go change:

replace gorm.io/gorm => ../

replace gorm.io/driver/postgres => ../../postgres

Test results:

GORM_DIALECT=postgres ./tests_all.sh
[+] up 1/1
 ✔ Container tests-postgres-1 Healthy                                                                               0.5s
testing postgres...
ok      gorm.io/gorm    1.024s
ok      gorm.io/gorm/callbacks  1.020s
ok      gorm.io/gorm/clause     1.682s
?       gorm.io/gorm/internal/lru       [no test files]
?       gorm.io/gorm/internal/stmt_store        [no test files]
ok      gorm.io/gorm/logger     1.014s
?       gorm.io/gorm/migrator   [no test files]
ok      gorm.io/gorm/schema     1.315s
ok      gorm.io/gorm/utils      1.018s
?       gorm.io/gorm/utils/tests        [no test files]
ok      gorm.io/gorm/tests      65.132s

…e for ALTER and DROP statements. CurrentSchema function now retrieves the value from current_schema() and returns instead.
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