Skip to content

Commit a4741ca

Browse files
committed
split CREATE SCHEMA IF NOT EXISTS query
1 parent c0ded42 commit a4741ca

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

dbos/system_database.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,21 @@ func runMigrations(pool *pgxpool.Pool, schema string) error {
166166
}
167167
defer tx.Rollback(ctx)
168168

169-
// Create the schema if it doesn't exist
170-
createSchemaQuery := fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s", pgx.Identifier{schema}.Sanitize())
171-
_, err = tx.Exec(ctx, createSchemaQuery)
169+
// Check if the schema exists
170+
var schemaExists bool
171+
checkSchemaQuery := fmt.Sprintf("SELECT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s')", schema)
172+
err = tx.QueryRow(ctx, checkSchemaQuery).Scan(&schemaExists)
172173
if err != nil {
173-
return fmt.Errorf("failed to create schema %s: %v", schema, err)
174+
return fmt.Errorf("failed to check if schema %s exists: %v", schema, err)
175+
}
176+
177+
// Create the schema if it doesn't exist
178+
if !schemaExists {
179+
createSchemaQuery := fmt.Sprintf("CREATE SCHEMA %s", pgx.Identifier{schema}.Sanitize())
180+
_, err = tx.Exec(ctx, createSchemaQuery)
181+
if err != nil {
182+
return fmt.Errorf("failed to create schema %s: %v", schema, err)
183+
}
174184
}
175185

176186
// Create the migrations table if it doesn't exist

0 commit comments

Comments
 (0)