fix(Migrator): use lowercase current_schema() for PG14 compatibility - #349
fix(Migrator): use lowercase current_schema() for PG14 compatibility#349feiguoL wants to merge 1 commit into
Conversation
34492e8 to
accd563
Compare
|
This PR does not work. |
CURRENT_SCHEMA (uppercase) is a reserved keyword in PostgreSQL. When used as CURRENT_SCHEMA() in DDL contexts like DROP INDEX, PostgreSQL 14 raises 'syntax error at or near CURRENT_SCHEMA' (SQLSTATE 42601) because it parses the keyword first, not the function call. Using lowercase current_schema() is the function form that works across all supported PostgreSQL versions (13+).
accd563 to
489079c
Compare
|
@keif888 Thank you for the feedback. You are absolutely right — I have reworked the fix to address this properly:
This restores PG14 compatibility while preserving the non-default-schema qualification added in #340. The query-context callers ( PTAL. |
|
I should have said in my comment that I was writing a PR to address this. My bad... I believe you should use the queryRaw function, rather than calling m.DB.Exec directly, as these calls should bypass the DryRun capability, and execute so that a valid result can be returned via DryRun. Also note that the result from SELECT current_schema() is nullable, so your query will throw a scan error when a NULL is returned. See #351 which is my attempt to fix this. These are my updated tests, with the SQL generated and errors if expected in comments. It was run via the Gorm Playground, which was pointed at my version of the postgres driver via the go.mod file. |
Motivation
PostgreSQL 14 raises
syntax error at or near "CURRENT_SCHEMA" (SQLSTATE 42601)when the migrator executesDROP INDEXand other DDL that referencesCurrentSchema().The current code returns
clause.Expr{SQL: "CURRENT_SCHEMA()"}.CURRENT_SCHEMA(uppercase) is a reserved keyword in PostgreSQL; PG14 parses the keyword first, then sees()as a syntax error. PG15+ tolerates it, but PG14 does not.This broke the gorm CI matrix (
postgres:14, oldstable) for every recent PR — see e.g. go-gorm/gorm#7837, go-gorm/gorm#7839, go-gorm/gorm#7840 — becausetests_all.shrunsgo get -u -t ./...and picks upv1.6.2.Fix
Use the lowercase function form
current_schema(), which is valid across all supported PostgreSQL versions (13+).Verification
go build ./...— OKgo vet ./...— OKgo test ./...— passesThe gorm
postgres:14, oldstablejob is expected to turn green once this lands and a new tag is released.