mirror of https://github.com/pressly/goose.git
fix: use schema name if explicitly set on table name for table existence (#883)
parent
bfb35504f3
commit
3c91df140a
|
@ -7,6 +7,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [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]
|
## [v3.24.0]
|
||||||
|
|
||||||
- Add support for loading environment variables from `.env` files, enabled by default.
|
- Add support for loading environment variables from `.env` files, enabled by default.
|
||||||
|
|
|
@ -5,13 +5,6 @@ import (
|
||||||
"strings"
|
"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{}
|
type Postgres struct{}
|
||||||
|
|
||||||
var _ Querier = (*Postgres)(nil)
|
var _ Querier = (*Postgres)(nil)
|
||||||
|
@ -53,14 +46,18 @@ func (p *Postgres) GetLatestVersion(tableName string) string {
|
||||||
|
|
||||||
func (p *Postgres) TableExists(tableName string) string {
|
func (p *Postgres) TableExists(tableName string) string {
|
||||||
schemaName, tableName := parseTableIdentifier(tableName)
|
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)
|
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) {
|
func parseTableIdentifier(name string) (schema, table string) {
|
||||||
schema, table, found := strings.Cut(name, ".")
|
schema, table, found := strings.Cut(name, ".")
|
||||||
if !found {
|
if !found {
|
||||||
return defaultSchemaName, name
|
return "", name
|
||||||
}
|
}
|
||||||
return schema, table
|
return schema, table
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue