Reverting Database Migration for Ory Hydra
This guide outlines the steps to revert a database migration in Ory Hydra using the custom jobs functionality provided in the Ory Hydra Helm chart. Reverting a migration can be essential when a schema change needs to be undone due to errors or unexpected issues. This method leverages your existing production configuration, with only the migration arguments being customized.
All other parameters - such as pod labels, service accounts, and resource configurations—are inherited from your current Hydra deployment. This minimizes potential configuration discrepancies and reduces the risk of errors.
Prerequisites
Before you begin, ensure you have the following:
- Active Ory Hydra Deployment: A production deployment is required because the migration job uses its configuration.
- Kubernetes Permissions: Ensure you have sufficient privileges to create and apply Kubernetes jobs.
- Database Backup: Always backup your database before attempting to revert any migrations.
- No rows without an OAuth 2.0 client: Down migrations that restore the foreign keys between the OAuth 2.0 client table and its dependent tables fail while any token, consent session, or login flow references a client ID that has no client row. Remove those rows first. See Restoring foreign keys fails on orphaned rows.
- No tokens or codes referencing a deleted consent flow: Down migrations that restore the foreign keys from the token and code tables to the consent flow table fail while any row references a consent flow that no longer exists. Clear those references first. See Restoring foreign keys to consent flows fails on orphaned rows.
Deployment Process
1. Create a Job Values File
Create a file named migrate-down-values.yaml to define the job with the custom arguments for reverting the migration. Adjust the
parameters as needed:
hydra:
customMigrations:
jobs:
sql-migration-down:
enabled: true
customArgs: ["migrate", "sql", "down", "-e", "-y", "--steps", "1"]
If you need to revert all database changes introduced by the last migration, you must determine the number of applied statements from the migration job. Check the migration job logs for a line similar to the following:
time=2025-03-05T11:47:18Z level=info msg=Successfully applied 2 migrations. audience=application service_name=Ory Hydra service_version=master
2. Prepare Helm resources
Pull the newest version of the Ory Helm chart and update the repository:
helm repo add ory https://k8s.ory.com/helm/charts
helm repo update
Locate the current production values file used by your Ory Hydra deployment. This file is needed to ensure that we revert the correct migrations. Using your production values file ensures that the job is created with the same container image and configuration as your production deployment.
3. Generate and Apply the Job Definition
This command produces a Kubernetes job definition that reverts the database migration using your production configuration with the custom job arguments.
helm template hydra ory/hydra --namespace oasis --show-only templates/job-migrations-custom.yaml \
-f hydra-values-used-by-your-production-env.yaml \
-f hydra/oel/migrate-down-values.yaml > migrate-down-job.yaml
Verify the generated job definition in migrate-down-job.yaml to ensure that the custom arguments are correctly set. If
everything looks good, apply the job to your Kubernetes cluster:
kubectl apply -f migrate-down-job.yaml
Possible issues
Incorrect Migration Arguments
If the job fails, check that the custom arguments in migrate-down-values.yaml accurately reflect the intended migration reversal. The default configuration reverts the last migration step, but you may need to adjust the --steps parameter if reverting more than one migration is required.
Restoring foreign keys fails on orphaned rows
Some down migrations restore foreign key constraints between the OAuth 2.0 client table and the tables holding its tokens, consent
sessions, and login flows. Restoring a constraint validates every existing row. It fails if a row has non-null client_id and
nid values but no matching (id, nid) in hydra_client. Rows with a null client reference are valid and must be preserved.
-
Process pending client deletions:
hydra janitor --clients {database-url} -
Delete the remaining rows that reference no client. Run the following statement against each table in the order listed, replacing every occurrence of
hydra_oauth2_accesswith the table name:DELETE FROM hydra_oauth2_accessWHERE client_id IS NOT NULLAND nid IS NOT NULLAND NOT EXISTS (SELECT 1 FROM hydra_client AS cWHERE c.id = hydra_oauth2_access.client_idAND c.nid = hydra_oauth2_access.nid);hydra_oauth2_obfuscated_authentication_sessionhydra_oauth2_logout_requesthydra_oauth2_accesshydra_oauth2_refreshhydra_oauth2_codehydra_oauth2_oidchydra_oauth2_pkcehydra_oauth2_device_auth_codeshydra_oauth2_flow
-
Rerun the migration job.
Restoring foreign keys to consent flows fails on orphaned rows
Some down migrations restore the foreign key constraints from the tables holding tokens and codes to the consent flow table.
Restoring a constraint validates every existing row. It fails if a row has a non-null challenge_id with no matching
consent_challenge_id in hydra_oauth2_flow. Those rows are tokens and codes that outlived their consent flow. Keep them and
clear the reference instead of deleting them.
-
Clear the references to consent flows that no longer exist. Run the following statement against each table listed, replacing every occurrence of
hydra_oauth2_accesswith the table name:UPDATE hydra_oauth2_accessSET challenge_id = NULLWHERE challenge_id IS NOT NULLAND NOT EXISTS (SELECT 1 FROM hydra_oauth2_flow AS fWHERE f.consent_challenge_id = hydra_oauth2_access.challenge_id);hydra_oauth2_accesshydra_oauth2_refreshhydra_oauth2_codehydra_oauth2_oidchydra_oauth2_pkcehydra_oauth2_device_auth_codes
-
Rerun the migration job.
