Skip to content

Commit

Permalink
fix: only use postgres schema name if explicitly set
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Jan 4, 2025
1 parent bfb3550 commit 7945090
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions internal/dialect/dialectquery/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 7945090

Please sign in to comment.