Skip to main content

Upgrading

Idenplane can upgrade a running instance for you: it validates first, takes a full database backup, applies pending migrations, health-checks the result, and restores the backup automatically if anything fails.

The endpoints that do this are disabled by default. Everything below is opt-in, because the data at risk is your user accounts and their credentials.

warning

Read Before you enable it first. The upgrade API refuses to run when it cannot guarantee a way back, and those refusals are the feature working — not an obstacle to route around.


What an upgrade does​

Seven stages, in order. A failure in DATABASE_MIGRATION or POST_HEALTH_CHECK triggers a rollback; an earlier failure stops the upgrade before anything has changed.

StageWhat happens
INITIALIZATIONRecords the attempt and locks out concurrent upgrades
PRE_VALIDATIONNine checks — see below. Stops here if any fail
BACKUPpg_dump -Fc of the whole database into BACKUP_DIR
CONFIG_CHECKVerifies configuration is compatible with the target version
DATABASE_MIGRATIONprisma migrate deploy
POST_HEALTH_CHECKConnection, schema, migrations, data integrity, critical tables
COMPLETIONMarks the upgrade successful

If DATABASE_MIGRATION or POST_HEALTH_CHECK fails, a ROLLBACK stage restores the backup taken in BACKUP and the upgrade is reported as failed.


Before you enable it​

1. pg_dump and pg_restore must be on the server​

The upgrade shells out to them. If they are missing, pre-validation fails with:

Backup tooling is missing — an upgrade cannot be rolled back

The official Docker image ships them for PostgreSQL 16, 17 and 18. On bare metal, install the PostgreSQL client package.

2. The client major version must equal the server major version​

A dump written by pg_dump 18 cannot be restored onto a PostgreSQL 16 server, and the reverse fails too. Pre-validation checks this and refuses on a mismatch:

pg client and server majors match (16)

Idenplane reads the server's major version and picks the matching client by itself, in this order:

  1. PG_BIN_DIR, if you set it (for example PG_BIN_DIR=/usr/lib/postgresql/17/bin);
  2. /usr/libexec/postgresqlN (Alpine, which the official image uses) or /usr/lib/postgresql/N/bin (Debian and Ubuntu), where N is the server major;
  3. pg_dump and pg_restore on PATH.

On bare metal with a single client installed, check that it matches:

pg_dump --version
psql -c 'SHOW server_version'

3. BACKUP_DIR must point at durable storage​

BACKUP_DIR=/var/lib/idenplane/backups

The default is ./backups, which is fine locally. In a container it is not: a backup on an ephemeral filesystem is not a backup. Mount a volume.

4. Scale to a single replica​

Rollback runs pg_restore --clean, which takes ACCESS EXCLUSIVE locks. Those conflict with any query another replica is running.


Enabling it​

Two variables, gated separately on purpose.

# Enables POST /admin/upgrade and POST /admin/upgrade/rollback.
# Both additionally require the super-admin role.
UPGRADE_API_ENABLED=true

# Allows a restore over the live database. Strictly harder than running
# migrations, which is why it is a second switch.
UPGRADE_ALLOW_LIVE_RESTORE=true

With UPGRADE_API_ENABLED unset, both endpoints return 503.

With UPGRADE_ALLOW_LIVE_RESTORE unset, an upgrade is refused before it touches anything:

Refusing to start: this upgrade could not be rolled back. Restoring a backup over the running database requires UPGRADE_ALLOW_LIVE_RESTORE=true, which is unset — so a failure after the migration would leave the database modified with no automatic recovery.

That refusal is deliberate. Without it you can reach a state where the migration ran, the health check failed, and nothing can put the database back.


Running an upgrade​

Check readiness​

curl -H "x-admin-api-key: $ADMIN_API_KEY" \
http://localhost:3000/admin/upgrade/health

curl -H "x-admin-api-key: $ADMIN_API_KEY" \
http://localhost:3000/admin/upgrade/pre-validation

curl -H "x-admin-api-key: $ADMIN_API_KEY" \
"http://localhost:3000/admin/upgrade/config-compatibility?version=0.4.0"

/health reports healthy, /pre-validation reports canProceed, and /config-compatibility checks your configuration against a target version. Both list every check with a pass, warn or fail status. Do not proceed while canProceed is false.

Dry run first​

curl -X POST -H "x-admin-api-key: $ADMIN_API_KEY" \
-H "Content-Type: application/json" \
-d '{"toVersion":"0.4.0","dryRun":true,"confirm":"0.4.0"}' \
http://localhost:3000/admin/upgrade

A dry run runs pre-validation, the configuration check and the health check, and skips the backup and the migration. It changes no data, but it is recorded in upgrade_audit_log.

confirm is only required for a real run — a dry run writes nothing, and demanding a confirmation there would just train you to type past the prompt that matters.

Run it​

curl -X POST -H "x-admin-api-key: $ADMIN_API_KEY" \
-H "Content-Type: application/json" \
-d '{"toVersion":"0.4.0","dryRun":false,"confirm":"0.4.0"}' \
http://localhost:3000/admin/upgrade
note

This call is synchronous — it does not return until the upgrade finishes or rolls back. On a large database that can take a while. Do not interrupt it.

confirm must equal toVersion here, so a mis-click or a replayed request cannot start a migration against a live database.

The response carries success, every stage with its outcome, and — if something failed — rollbackTriggered and rollbackSucceeded.

Request fields​

FieldRequiredMeaning
toVersionyesTarget version, e.g. "0.4.0"
confirmfor real runsMust equal toVersion
dryRunnoSimulate only. Defaults to false
forcenoSkip pre-validation. See below
notenoFree-text note recorded with the attempt

The identity recorded against the upgrade comes from the authenticated principal, never from the request body.

danger

force: true skips all of pre-validation — including the checks that confirm pg_dump exists and BACKUP_DIR is writable. An upgrade forced past those can run the migration with no usable backup behind it, which is precisely the state the rollback exists to prevent. It is logged at error level. Do not use it to get past a failing check; fix the check.

Or use the console​

/console/upgrade shows current status and health; /console/upgrade/new walks the same flow as a three-step wizard with the dry-run box ticked by default, and requires you to type the target version before a live run.


When something fails​

A failed upgrade looks like this:

{
"success": false,
"rollbackTriggered": true,
"rollbackSucceeded": true,
"stages": [
{ "stage": "BACKUP", "success": true },
{ "stage": "DATABASE_MIGRATION", "success": false },
{ "stage": "ROLLBACK", "success": true }
]
}

rollbackSucceeded: true means the database is back to its pre-upgrade state. The instance stays usable and a new upgrade can be started.

If rollbackSucceeded is false, restore by hand from the file named in the BACKUP stage:

pg_restore --clean --if-exists --no-owner --single-transaction --exit-on-error \
-d "$DATABASE_URL" /var/lib/idenplane/backups/idenplane-backup-....dump

Only one upgrade at a time​

A second concurrent upgrade is refused with 409:

An upgrade to X started at T is still in progress (id …). Refusing to start an upgrade to Y concurrently — two migrations against one database can corrupt it. If that upgrade is known to have died, mark its row FAILED.

The lock expires after two hours. If an upgrade died and left its row behind, mark that row FAILED in upgrade_audit_log.


Rehearse it first​

Do this once on a copy before you trust it with real data:

  1. Copy the database — pg_dump -Fc "$DATABASE_URL" -f copy.dump, then restore it into a scratch database
  2. Point a spare instance at the copy, with both variables set
  3. Dry run, and confirm nothing changed
  4. Real upgrade, and confirm your row counts still match
  5. Break it on purpose — add a migration that fails, run the upgrade, and confirm rollbackSucceeded is true and your data came back

Step 5 is the one that matters. Stages 1–4 tell you the happy path works; only step 5 tells you the safety net does.


Upgrading by replacing the image​

The upgrade API above is optional. The usual upgrade is to start the new image on the same database: the container's entrypoint runs prisma migrate deploy before the server starts, so the schema is migrated on start.

  1. Back up first: pg_dump --format=custom (see backups).
  2. Stop the old version. With more than one replica, stop all of them: an old process must not write while the schema changes.
  3. Start the new image with the same environment, in particular the same WEBHOOK_SECRET_KEY and WEBHOOK_ENCRYPTION_SALT. Client secrets, TOTP secrets and signing keys are stored encrypted with them, and a server started with different values refuses to start rather than misread them.
  4. Wait for /health/ready, then sign in.

How each release is tested​

The restore-drill.yml workflow runs every week, and on every pull request that touches migrations or the backup code. It does exactly the procedure above:

  • Upgrade test (scripts/ci/upgrade-test.sh):
    1. Runs the previous release's published image (the latest v* tag before the commit under test).
    2. Seeds it: a realm, a confidential client, a password user, a user with an authenticator app, and a signed-in session.
    3. Replaces it with the image built from the commit, on the same database and keys.
    4. Checks that every migration in prisma/migrations is applied and none failed, and that the server reports the new version.
    5. Checks that password and TOTP sign-in still work, the old client secret still authenticates, the old refresh token is accepted, and tokens are still signed with the same keys.
  • Restore drill (scripts/ci/restore-drill.sh): takes a pg_dump --format=custom of a seeded instance, restores it into a fresh PostgreSQL 16, and runs the same checks against it. It also starts the restore once with a different WEBHOOK_SECRET_KEY and expects a refusal to start.

You can run the same test against your own previous version. It needs only Docker:

git clone https://github.com/idenplane/idenplane.git && cd idenplane
git checkout v0.10.0 # the version you are moving to
PREV_IMAGE=islamawad/idenplane:0.9.1 IDENPLANE_IMAGE=islamawad/idenplane:0.10.0 IDENPLANE_IMAGE_IS_EXTERNAL=1 bash scripts/ci/upgrade-test.sh

Leave out IDENPLANE_IMAGE to build the image from the checkout instead. PREV_IMAGE defaults to the release before the checked-out commit. The script prints a PASS or FAIL line for each check. It uses throwaway containers on a private Docker network, and publishes no ports.


History​

curl -H "x-admin-api-key: $ADMIN_API_KEY" \
"http://localhost:3000/admin/upgrade/audit?limit=20"

Every attempt is recorded in upgrade_audit_log with its status, stages, and the backup it took. The rollback deliberately excludes this table from the restore, so a rollback does not erase the record of itself.


Next steps​

Docker Running Idenplane in containers

Kubernetes Deploying to a cluster

Environment Variables Full configuration reference