Skip to content

Commit

Permalink
fix: use schema name if explicitly set on table name for table existe…
Browse files Browse the repository at this point in the history
…nce (#883)
  • Loading branch information
mfridman authored Jan 5, 2025
1 parent bfb3550 commit 3c91df1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Fix regression (`v3.23.1` and `v3.24.0`) in postgres migration table existence check for
non-default schema. (#882, #883)

## [v3.24.0]

- Add support for loading environment variables from `.env` files, enabled by default.
Expand Down
17 changes: 7 additions & 10 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 @@ -53,14 +46,18 @@ func (p *Postgres) GetLatestVersion(tableName string) string {

func (p *Postgres) TableExists(tableName string) string {
schemaName, tableName := parseTableIdentifier(tableName)
q := `SELECT EXISTS ( SELECT FROM pg_tables WHERE schemaname = '%s' AND tablename = '%s' )`
return fmt.Sprintf(q, schemaName, tableName)
if schemaName != "" {
q := `SELECT EXISTS ( SELECT 1 FROM pg_tables WHERE schemaname = '%s' AND tablename = '%s' )`
return fmt.Sprintf(q, schemaName, tableName)
}
q := `SELECT EXISTS ( SELECT 1 FROM pg_tables WHERE tablename = '%s' )`
return fmt.Sprintf(q, 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 3c91df1

Please sign in to comment.