diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-08-18 16:06:54 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-18 16:06:54 -0400 |
| commit | 3ad5a6ab1ef53330f18014b710534d0ed0cd55b0 (patch) | |
| tree | 5b5d1ed42e59314e65a4ddb8bff2dd7c059fedd9 /internal/storage/postgresql.go | |
| parent | Add a couple helper scripts (diff) | |
| download | cli-3ad5a6ab1ef53330f18014b710534d0ed0cd55b0.zip | |
Fix using a schema with a custom migration table name (#7)
* Fix using a schema with a custom migration table name
* Fix test
Diffstat (limited to 'internal/storage/postgresql.go')
| -rw-r--r-- | internal/storage/postgresql.go | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/internal/storage/postgresql.go b/internal/storage/postgresql.go index dee0288..d93d014 100644 --- a/internal/storage/postgresql.go +++ b/internal/storage/postgresql.go @@ -29,19 +29,25 @@ func (p PostgresqlAdapter) TableExists( tableName string, ) (bool, error) { var exists bool - // Assumption: Anytime we check if a table exists, it will be - // for the 'public' schema. If someone wants to have bolt_migrations - // table outside of the 'public' schema on postgresql, this would be - // an issue. + + schemaName := "public" + parts := strings.Split(tableName, ".") + if len(parts) == 2 { + schemaName = parts[0] + tableName = parts[1] + } else { + tableName = parts[0] + } + err := executor.QueryRow(` SELECT EXISTS ( SELECT FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace - WHERE n.nspname = 'public' - AND c.relname = $1 + WHERE n.nspname = $1 + AND c.relname = $2 AND c.relkind = 'r' -- Only tables ); - `, tableName).Scan(&exists) + `, schemaName, tableName).Scan(&exists) if err != nil { return false, fmt.Errorf( "unable to check if %s exists: %w", |