File tree Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments