{"record":{"id":"16fe48caedaec046","repo":"ory/hydra","slug":"migrator-problem-creating-schema-migrations","errorCode":null,"errorMessage":"migrator: problem creating schema migrations","messagePattern":"migrator: problem creating schema migrations","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/popx/migrator.go","lineNumber":684,"sourceCode":"\tf, err := os.Create(schema) //#nosec:G304\n\tif err != nil {\n\t\treturn errors.WithStack(err)\n\t}\n\terr = c.Dialect.DumpSchema(f)\n\tif err != nil {\n\t\t_ = os.RemoveAll(schema)\n\t\treturn errors.WithStack(err)\n\t}\n\treturn nil\n}\n\nfunc (mb *MigrationBox) exec(ctx context.Context, fn func() error) error {\n\tnow := time.Now()\n\tdefer mb.printTimer(now)\n\n\terr := mb.CreateSchemaMigrations(ctx)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"migrator: problem creating schema migrations\")\n\t}\n\n\tif mb.c.Dialect.Name() == \"sqlite3\" {\n\t\tif err := mb.c.RawQuery(\"PRAGMA foreign_keys=OFF\").Exec(); err != nil {\n\t\t\treturn sqlcon.HandleError(err)\n\t\t}\n\t}\n\n\tswitch mb.c.Dialect.Name() {\n\tcase dbal.DriverCockroachDB, dbal.DriverYugabyteDB:\n\t\touter := fn\n\t\tfn = func() error {\n\t\t\t// CreateSchemaMigrations runs before this wrapper. YugabyteDB uses\n\t\t\t// autocommit DDL, but its pgwire errors expose the same retryable\n\t\t\t// SQLSTATEs that crdb.Execute classifies for whole-run retries.\n\t\t\treturn errors.WithStack(crdb.Execute(outer))\n\t\t}\n\t}","sourceCodeStart":666,"sourceCodeEnd":702,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/popx/migrator.go#L666-L702","documentation":"MigrationBox.exec first ensures the `schema_migrations` tracking table exists via CreateSchemaMigrations before running any migration. If that call fails (DB unreachable, insufficient privileges, dialect issues), the underlying error is wrapped with this message and returned. Callers UpTo and Down abort the whole migration run, so no migrations are applied or rolled back.","triggerScenarios":"Calling UpTo(ctx, mb, ...) or Down(ctx, mb, ...) when mb.CreateSchemaMigrations returns an error: database is down/unreachable, the DB user lacks CREATE TABLE privileges, the schema_migrations table exists in a corrupt/incompatible form, or the driver/dialect fails on the CREATE TABLE IF NOT EXISTS statement.","commonSituations":"Database container not started or wrong DSN/host in config before running `pop migrate`; CI pipelines pointing at a migrations-restricted user; production DBs where the app role lacks DDL rights; stale/corrupt schema_migrations left by an interrupted earlier migration with a different pop version.","solutions":["Verify the database is reachable and the DSN in the connection details/environment is correct (try a plain query with the same credentials).","Grant the migration user CREATE privileges (e.g. GRANT CREATE ON DATABASE ...) or run migrations as a DDL-capable role.","Inspect the wrapped cause: drop/recreate a corrupt schema_migrations table only if its contents can be reconstructed from applied migration timestamps.","Confirm driver/dialect match (e.g. using the sqlite3 dialect on a file path that exists and is writable)."],"exampleFix":"// before (app role without DDL rights)\nDATABASE_URL=postgres://app_reader:...@db:5432/mydb?sslmode=disable\n\n// after\nDATABASE_URL=postgres://app_migrator:...@db:5432/mydb?sslmode=disable\n-- with: GRANT CREATE ON DATABASE mydb TO app_migrator;","handlingStrategy":"try-catch","validationCode":"// before running migrations\nif err := db.RawQuery(\"SELECT 1\").Exec(); err != nil {\n    return fmt.Errorf(\"database unreachable, aborting migrations: %w\", err)\n}\n// check DDL rights\ncanCreate, err := canCreateTables(db) // e.g. SELECT has_database_privilege(current_user, current_database(), 'CREATE')","typeGuard":null,"tryCatchPattern":"if err := migrator.UpTo(ctx, steps...); err != nil {\n    if strings.Contains(err.Error(), \"problem creating schema migrations\") {\n        // inspect cause: connectivity / privileges / corrupt schema_migrations\n        log.Printf(\"schema_migrations setup failed: %+v\", err)\n        return fmt.Errorf(\"migration bootstrap failed: %w\", errors.Unwrap(err))\n    }\n    return err\n}","preventionTips":["Run migrations as a dedicated role with CREATE privileges, separate from the runtime app role.","Add a startup health check (SELECT 1) before invoking migrations in CI and deploy scripts.","Keep pop versions consistent between environments to avoid schema_migrations format drift.","Monitor DB availability (container status, connection limits) before deploy pipelines trigger migrations."],"tags":["database","migration","schema"],"backgroundTag":"migration-schema-setup-failed","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}