Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions dbos/system_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,21 @@ func runMigrations(pool *pgxpool.Pool, schema string) error {
}
defer tx.Rollback(ctx)

// Create the schema if it doesn't exist
createSchemaQuery := fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s", pgx.Identifier{schema}.Sanitize())
_, err = tx.Exec(ctx, createSchemaQuery)
// Check if the schema exists
var schemaExists bool
checkSchemaQuery := fmt.Sprintf("SELECT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s')", schema)
err = tx.QueryRow(ctx, checkSchemaQuery).Scan(&schemaExists)
if err != nil {
return fmt.Errorf("failed to create schema %s: %v", schema, err)
return fmt.Errorf("failed to check if schema %s exists: %v", schema, err)
}

// Create the schema if it doesn't exist
if !schemaExists {
createSchemaQuery := fmt.Sprintf("CREATE SCHEMA %s", pgx.Identifier{schema}.Sanitize())
_, err = tx.Exec(ctx, createSchemaQuery)
if err != nil {
return fmt.Errorf("failed to create schema %s: %v", schema, err)
}
}

// Create the migrations table if it doesn't exist
Expand Down
Loading