{"record":{"id":"feb942ec0a0be261","repo":"ory/hydra","slug":"error-executing-s-sql-s","errorCode":null,"errorMessage":"error executing %s, sql: %s","messagePattern":"error executing (.+?), sql: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"oryx/popx/migration_box.go","lineNumber":209,"sourceCode":"\n\ttxRunner := func(b []byte) func(Migration, *pop.Connection) error {\n\t\treturn func(mf Migration, c *pop.Connection) error {\n\t\t\tcontent, err := mb.migrationContent(mf, c, b, true)\n\t\t\tif err != nil {\n\t\t\t\treturn errors.Wrapf(err, \"error processing %s\", mf.Path)\n\t\t\t}\n\t\t\tif isMigrationEmpty(content) {\n\t\t\t\tl.WithField(\"migration\", mf.Path).Trace(\"This is usually ok - ignoring migration because content is empty. This is ok!\")\n\t\t\t\treturn nil\n\t\t\t}\n\n\t\t\tvar q queryExecutor = c.Store.SQLDB()\n\t\t\tif c.TX != nil {\n\t\t\t\tq = c.TX\n\t\t\t}\n\n\t\t\tif _, err = q.Exec(content); err != nil {\n\t\t\t\treturn errors.Wrapf(err, \"error executing %s, sql: %s\", mf.Path, content)\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\t}\n\n\terr := mb.findMigrations(dir, txRunner)\n\tif err != nil {\n\t\treturn mb, err\n\t}\n\n\tif err := mb.check(); err != nil {\n\t\treturn nil, err\n\t}\n\treturn mb, nil\n}\n\n// migrationFallbacks returns the ordered dialects migration selection falls\n// back to before the generic (\"all\") file (.yugabyte. → .postgres. → generic","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/popx/migration_box.go#L191-L227","documentation":"When executing a migration's SQL against the database (via queryExecutor Exec, using the transaction or the raw SQL DB), any driver error is wrapped as \"error executing <path>, sql: <content>\" (oryx/popx/migration_box.go:209). This exposes both the failing migration file and its SQL so the database-level error can be diagnosed.","triggerScenarios":"q.Exec(content) fails while running migrations — e.g. syntax error, permission denied, object already exists, or a DDL statement incompatible with the dialect/transaction mode.","commonSituations":"Re-running a partially applied migration (table already exists); insufficient database user privileges; SQL valid in Postgres but invalid in CockroachDB/MySQL; non-transactional DDL on dialects requiring autocommit.","solutions":["Read the wrapped driver error and the included SQL to find the failing statement; fix the migration SQL","Restore the database to a consistent state (drop partially created objects or restore a snapshot) before retrying","Run the migration with the .autocommit flag / correct dialect if it performs DDL that cannot run in a transaction","Check the database user has sufficient privileges"],"exampleFix":"-- before (fails on re-run)\nCREATE TABLE users (id serial primary key);\n-- after (idempotent)\nCREATE TABLE IF NOT EXISTS users (id serial primary key);","handlingStrategy":"try-catch","validationCode":"// dry-run the statement's shape on a dev database with the same engine and version\nif err := conn.RawQuery(\"SELECT 1\").Exec(); err != nil { return err } // connectivity/privilege sanity check","typeGuard":null,"tryCatchPattern":"err := mb.Exec(ctx, conn)\nvar path, sql string\nif err != nil {\n    fmt.Printf(\"%+v\\n\", err) // shows 'error executing <path>, sql: <content>' + driver cause\n    return err\n}","preventionTips":["Write idempotent SQL (IF NOT EXISTS / IF EXISTS) so retried migrations succeed","Run migrations in CI against the same database engine/version as production","Ensure the migration user has DDL privileges","Use .autocommit for DDL on dialects that cannot run DDL in transactions (CockroachDB, MySQL, YugabyteDB)","Back up the database before applying migrations"],"tags":["migrations","database","sql","execution"],"backgroundTag":"migration-execution-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"}