From 79450909a2af26eb1387a9c0fafa43e8dfd47761 Mon Sep 17 00:00:00 2001 From: Mike Fridman Date: Sat, 4 Jan 2025 08:52:17 -0500 Subject: [PATCH] fix: only use postgres schema name if explicitly set --- internal/dialect/dialectquery/postgres.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/internal/dialect/dialectquery/postgres.go b/internal/dialect/dialectquery/postgres.go index facbc2ff7..a8df1397d 100644 --- a/internal/dialect/dialectquery/postgres.go +++ b/internal/dialect/dialectquery/postgres.go @@ -5,13 +5,6 @@ import ( "strings" ) -const ( - // defaultSchemaName is the default schema name for Postgres. - // - // https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PUBLIC - defaultSchemaName = "public" -) - type Postgres struct{} var _ Querier = (*Postgres)(nil) @@ -52,15 +45,18 @@ func (p *Postgres) GetLatestVersion(tableName string) string { } func (p *Postgres) TableExists(tableName string) string { + q := `SELECT EXISTS ( SELECT 1 FROM pg_tables WHERE tablename = '%s' )` schemaName, tableName := parseTableIdentifier(tableName) - q := `SELECT EXISTS ( SELECT FROM pg_tables WHERE schemaname = '%s' AND tablename = '%s' )` + if schemaName != "" { + q = `SELECT EXISTS ( SELECT 1 FROM pg_tables WHERE schemaname = '%s' AND tablename = '%s' )` + } return fmt.Sprintf(q, schemaName, tableName) } func parseTableIdentifier(name string) (schema, table string) { schema, table, found := strings.Cut(name, ".") if !found { - return defaultSchemaName, name + return "", name } return schema, table }