{"record":{"id":"b300d5530fb311ed","repo":"ory/hydra","slug":"migration-version-s-does-not-exist","errorCode":null,"errorMessage":"migration version %s does not exist","messagePattern":"migration version (.+?) does not exist","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/popx/migrator.go","lineNumber":386,"sourceCode":"\t\t\tl := mb.l.WithField(\"version\", mi.Version).WithField(\"migration_name\", mi.Name).WithField(\"migration_file\", mi.Path)\n\t\t\tl.Debugf(\"handling migration %s\", mi.Name)\n\t\t\texists, err := c.Where(\"version = ?\", mi.Version).Exists(mtn)\n\t\t\tif err != nil {\n\t\t\t\treturn errors.Wrapf(err, \"problem checking for migration version %s\", mi.Version)\n\t\t\t}\n\n\t\t\tif !exists && len(mi.Version) > 14 {\n\t\t\t\tlegacyVersion := mi.Version[:14]\n\t\t\t\tlegacyVersionExists, err := c.Where(\"version = ?\", legacyVersion).Exists(mtn)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn errors.Wrapf(err, \"problem checking for legacy migration version %s\", legacyVersion)\n\t\t\t\t}\n\n\t\t\t\tif !legacyVersionExists {\n\t\t\t\t\treturn errors.Errorf(\"neither normal (%s) nor legacy migration (%s) exist\", mi.Version, legacyVersion)\n\t\t\t\t}\n\t\t\t} else if !exists {\n\t\t\t\treturn errors.Errorf(\"migration version %s does not exist\", mi.Version)\n\t\t\t}\n\n\t\t\tif err := mi.Valid(); err != nil {\n\t\t\t\treturn errors.WithStack(err)\n\t\t\t}\n\n\t\t\tif mb.shouldNotUseTransaction(mi) {\n\t\t\t\terr := mi.Runner(mi, c)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn errors.WithStack(err)\n\t\t\t\t}\n\n\t\t\t\t// #nosec G201 - mtn is a system-wide const\n\t\t\t\tif err := c.RawQuery(fmt.Sprintf(\"DELETE FROM %s WHERE version = ?\", mtn), mi.Version).Exec(); err != nil {\n\t\t\t\t\treturn errors.Wrapf(err, \"problem deleting migration version %s. YOUR DATABASE MAY BE IN AN INCONSISTENT STATE! MANUAL INTERVENTION REQUIRED!\", mi.Version)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif err := mb.isolatedTransaction(ctx, \"down\", func(conn *pop.Connection) error {","sourceCodeStart":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/popx/migrator.go#L368-L404","documentation":"During Down, when a migration version is shorter than or equal to 14 characters (so no legacy variant applies) and its row is not present in schema_migration, the migrator aborts with this error. It refuses to roll back a migration that is not recorded as applied.","triggerScenarios":"Calling Down for a candidate migration whose version row is absent from schema_migration: count-based selection picked it because other rows exist, but this specific version was never recorded or was deleted.","commonSituations":"Manually deleted schema_migration rows; database restored from a partial backup; running Down on a database migrated with a different tool; legacy-version rows present but count-based selection referencing a short version migration not in the table.","solutions":["Compare the migrations listed in your schema_migration table with your migration files","Re-insert the missing version row if the migration was actually applied, then re-run Down","Confirm the correct database/DSN; the row may exist in another database","Skip/patch the migration set so Down only targets recorded versions"],"exampleFix":"// before\n// DELETE FROM schema_migration WHERE version='20221012'; -- accidental\n// after: re-insert then Down\n// INSERT INTO schema_migration (version) VALUES ('20221012');","handlingStrategy":"validation","validationCode":"// confirm the exact version exists in the tracking table before Down\nvar ok bool\nif err := db.QueryRow(\"SELECT EXISTS(SELECT 1 FROM schema_migration WHERE version=?)\", version).Scan(&ok); err != nil || !ok {\n    return fmt.Errorf(\"version %s not recorded; refusing rollback\", version)\n}","typeGuard":null,"tryCatchPattern":"if err := box.Down(ctx, 1); err != nil {\n    if strings.Contains(err.Error(), \"does not exist\") {\n        // reconcile schema_migration with migration files before retrying\n    }\n    return err\n}","preventionTips":["Keep schema_migration rows in sync with applied migrations","Avoid mixing migration tools on the same database","Snapshot the tracking table before maintenance"],"tags":["database","migration","go"],"backgroundTag":"migration-version-not-found","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"}