how-to deployeradministrator

Upgrade contract — moving a deployment from one release to the next

In plain words. A release of OpenGov is a git tag. Upgrading a country’s deployment means taking a backup, checking out the newer tag, and bringing the stack back up — the schema migrates itself on the way. This page says what to read before you start, what to do, how to tell it worked, and how to go back.

It complements the pre-deploy checklist in release.md; that one covers what to verify about a build before it ships, this one covers what an operator does to a running deployment.


Before you start

  1. Read the changelog section for the version you are moving to (CHANGELOG.md). Every release states its own known limits there.

  2. Know which release you are on now. Ask the deployment:

    curl -s https://<your-host>/version
    # {"version": "0.1.0", "git_sha": "<twelve hex characters>"}
    

    version is the release. git_sha is the exact commit of it — on some hosts the SHA is legitimately unknown (the build could not see its own git history), which is not a fault. The release is the number that matters here.

  3. Check whether this instance has ever published a governance change — that is, whether anyone has approved a change to the country’s world model through the Studio. If so, read “The forked chain” at the bottom of this page before going any further. It is the one upgrade hazard that has actually bitten.


The upgrade

# 1. Back up. This is not optional — the restore is your only way back.
./scripts/backup.sh

# 2. Check that the release is really ours, and really the one you think.
#    Do this BEFORE the checkout, while you are still standing on the release
#    you trust — the keys must come from where you are, not from where you
#    are going.
./scripts/release/verify_checkout.sh v<version>

# 3. Move to the release you want.
git fetch --tags
git checkout v<version>

# 4. Rebuild and bring the stack up.
docker compose up -d --build

What step 2 is for. You build from source, so what this deployment ends up running is decided by the commit you check out. Step 2 is what turns git fetch from trusted into verified: it reads the signed announcement for your channel and checks four things — that a key you trust signed it, that v<version> resolves to the exact commit that announcement names, that upgrading from the version you are on has actually been tested, and how old the announcement is.

The first three stop the upgrade if they fail. The fourth only tells you: an old announcement cannot drive an automatic upgrade, but you are here by hand, which is the judgement that rule stands in for.

It needs nothing installed beyond what this deployment already has — no cosign, no network beyond the git remote you already use. If you have never subscribed to a channel, this step has nothing to read and you can skip it; see docs/runbooks/release-signing.md for what the announcement is and who signs it.

Nothing else is required. A one-shot bootstrap container (init) runs before the application and the worker start; it migrates the database (alembic upgrade heads) and re-seeds this deployment’s registry row from its committed artifact under deployments/<code>/. The application and worker wait for it, so only one process ever migrates and neither can start against an unmigrated schema.

Telling it worked

curl -s https://<your-host>/version   # version is the tag you checked out
curl -s https://<your-host>/health    # status is "ok"

Then read the startup log and confirm there are no WARNING lines from opengov.platform or opengov.platform.auth. Those warnings mark configuration left at a development default, and each one is a real exposure — the list of what they mean is in release.md. A clean log is the acceptance signal.


Going back

There is no “downgrade” command, and you should not reach for one. A release may have added a column that the previous release’s code does not know about; older code reading a newer schema is a class of failure with no clean recovery.

Roll back by restoring the backup you took in step 1 and checking out the tag you came from:

git checkout v<previous-version>
./scripts/restore.sh <the-bundle-you-made-before-upgrading>
docker compose up -d --build

The restore procedure, including the conditions under which it refuses to run, is documented in backup-restore.md. Read it before you need it.


Crossing the S1 privacy boundary — three keys an existing deployment may not have

In plain words. The first release that carries the private-envelope migration (M89_private_envelope_reseal) seals the private columns of the world model, and it needs three environment variables that only deployments provisioned after the keys were introduced have: the world-model blind-index key (OPENGOV_WM_BLIND_INDEX_KEY), the vault key-encryption key (OPENGOV_VAULT_KEK_V1), and the active vault key version (OPENGOV_VAULT_ACTIVE_KEK_VERSION, which must be 1). A deployment created before that does not have them, and the upgrade will not come up until it does. Set them BEFORE the deploy — when you cross this boundary, this section is step zero of the upgrade above.

What skipping this looks like. The migrations run before the application starts, and the migration refuses to run without its keys: it fails up front with a RuntimeError naming every missing key at once, before touching any data. The container exits, restarts, and fails again — a crash-loop at boot, with nothing serving. The sandbox hit exactly this on 2026-08-19 (the build it ran named one missing key per restart, so recovery took three rounds).

A country deployment

On a trusted machine, from the repository root, run the secrets generator (scripts/generate_secrets.py) in its --only-missing mode against the deployment’s decrypted secrets file:

sops -d deployments/<code>/secrets.env > /tmp/secrets.<code>.plain
python scripts/generate_secrets.py --only-missing /tmp/secrets.<code>.plain

The mode prints exactly the KEY=VALUE lines the file is missing and never rewrites the file. Append the printed lines to secrets.env (decrypt, append, re-encrypt — the file must never sit in the repository in plaintext), and make sure OPENGOV_VAULT_ACTIVE_KEK_VERSION=1 is set in the same place; it is a version number, not a generated secret. If the output contains more than the S1 keys — for example a Sentinel signing key, for a file that predates it — do NOT append the Sentinel private key on its own: its public half is not written in this mode; follow provision-deployment.md for that key instead. Then inject the two keys into the container environment out-of-band, the same way the other KEK-tier keys went in at provisioning (provision-deployment.md). Delete the temporary plaintext copy when you are done.

The sandbox

The sandbox has no secrets.env. Its environment lives in Coolify, so the keys go into Coolify. Do not use the Coolify API for that. No Coolify key has the power write, and the owner wants none made (decision of 2026-10-13). The deploy key of CI has the powers deploy and read only.

Generate the values on a trusted machine. To get the full set, point --only-missing at an empty file. The generator then prints its whole default set.

Use ONLY the three values above. The rest is for fresh deployments, not for this backfill. For example, the rest holds a fresh Sentinel signing key, and this mode never writes its public half. Set the three values in Coolify in one of two ways:

  • Way 1, the standard way. Write each value through the data model of Coolify, over root SSH on the host. Way 1 writes the normal row and the preview row, and reads each row back.
  • Way 3, the fallback. A person without root SSH types each value in the Coolify screen, in both rows.

docs/runbooks/replace-a-login-secret.md (step 2) gives the commands of Way 1. They read the value from a file with mode 0600 on the host. Each step on the host or in Coolify needs the approval of the owner.

Way 1 changes rows that exist. For a key that the application does not have yet, add the key in the Coolify screen first, with a temporary value. Then write the real value with Way 1. When the screen makes the normal row, Coolify makes the preview row too (the created hook of its EnvironmentVariable model, read on the sandbox host on 2026-10-13). With one row only, the Way 1 script changes nothing.

Then redeploy the application. The migration applies on the way up.

Confirming it worked

# The migration landed — alembic_version names it:
docker compose exec postgres psql -U opengov -d opengov -c \
  "SELECT version_num FROM alembic_version;"
# M89_private_envelope_reseal

curl -s https://<your-host>/health    # 200
curl -s https://<your-host>/version   # git_sha is the build you deployed

Nested tables — what a deployment sees after this release

A form may now ask the same questions once per item within a person: a table inside a row of another table (two levels, never deeper). What changes for a running deployment:

  • Older portal and approval builds are refused, in plain words, for any form that uses one. Such a form requires the nested_repeating_groups capability; a build that never declared it gets a 426 (“a table inside a table … needs an update”) on reads, and the same on any decision or document that addresses an inner row. Upgrade the SPAs together with the backend — nothing is shown broken meanwhile, and every form without a nested table is served exactly as before.
  • No data changes shape. The row_key columns were already widened by M94 (a release earlier); a flat table’s rows keep their plain row ids byte for byte. An inner row is addressed by its path (r_outer/inner_table/r_inner) only from the moment a nested table is published.
  • The register write path gains one idiom, not a migration: an inner row’s bond attaches to the outer row’s own record. An outer row that cannot name its record anchors nothing and is recorded in the audit log (worldmodel.inner_rows_unanchored).

The forked chain

This affects only instances where someone has approved a change to the country’s world model — the description of the things the country registers.

Such an approval writes a schema change of its own, recorded on that instance and nowhere else. A platform release also carries schema changes, written in this repository. The two are siblings rather than a sequence, so the instance’s schema history splits into two branches that both need applying.

The upgrade handles this correctly today: the bootstrap applies heads — every branch tip — rather than a single one. It deliberately does not rejoin the branches. Issue #1378 weighed a deploy-time merge revision and decided against it: the branches are independent by construction, so leaving them split costs an operator nothing but a second row in the schema-version table. Your instance is expected to show two branch tips, and that is not a fault to report.

What this means for you as an operator:

  • A normal upgrade works. Do not intervene.
  • If the bootstrap container exits non-zero with a message about multiple heads, stop and escalate rather than applying a revision by hand. The message names the branch it could not resolve.
  • Approving a further world-model change while the branches are split is refused in plain words, telling you to rejoin first. That refusal is deliberate; it is not a bug to work around.

Getting this wrong takes the whole deployment down — the bootstrap exits non-zero, the application and worker never start because they wait on it, and the proxy answers 503. That happened twice in two days on the sandbox, from two unrelated changes.


Why there is no image registry

OpenGov deployments build from source. Every service in docker-compose.yml uses build:, not a published image reference, and the hosted deployments build from a git checkout too. So a country consumes a release by checking out its tag.

The sandbox is the exception, and only the sandbox. Since divergence D47 the sandbox is moving to a Docker Swarm stack (docker-stack.yml), and Swarm cannot build — it pulls. .github/workflows/publish-images.yml therefore publishes three images, signed, into a registry that runs on the deployment host itself — not GitHub’s, whose free allowance filled in three pushes on 2026-08-11 (spec docs/specs/2026-08-11-self-hosted-registry.md). That registry has been installed and running on the sandbox host since 2026-08-12, and holds all three images, signed and verified. Publishing is still on request only — the trigger is manual by design, not because anything is unfinished. That changes nothing for a country: docker-compose.yml is still the shape a country deploys, still builds from source, and the tag is still what an operator checks out.

Extending the registry to countries is a reasonable future step, but it is a change to how deployments get their code, not a packaging detail, and it should be decided as one — the sandbox move was decided that way, as D47.

That decision is made — D48, ratified 2026-08-12 (docs/specs/2026-08-12-release-channels-and-signing-trust.md): a deployment subscribes to a channel, is told when a newer release exists on it, and either waits for a person or applies within a window — with waiting the default, because the backup above is still the only way back. Countries adopt in source mode: the signed manifest names the commit, the operator verifies the checked-out tag resolves to it, and the steps above stay exactly as written. Until D48 is built (docs/plans/2026-08-12-release-channels.md), this page describes the whole of how an upgrade happens.

Imported connections — what a deployment sees after this release (2026-08-28)

A deployment that ran a service import before this release carries the old proposal shape: an agency_api proof mode on the fields each automated step fills, and a studio draft bound as the practice system. That wiring was measured unable to ever fire (spec 2026-08-28-import-draft-confirmation.md, “The correction”) and nothing migrates it — re-run the import (python -m sandbox.importer.run --map <file>) to get the corrected proposal: event bindings held until go-live, the connection bound as the live system, seeded checks. Only the sandbox has such drafts today (no production country is committed), and the sandbox re-imports.

After this release an operator will also see event rows in a new held state: those are sends whose step passed before the connection went live — kept on purpose, released by going live, never an error (docs/runbooks/integration-studio.md §“Held vs paused vs failed”).

Upgrading across M101 — what an applicant qualifies for arrives, and filters leave (2026-09-10)

M101 renames the filter table to smartrules_qualifications and gives each row a typed rule. Before upgrading, list what the conversion will carry — every filter sentence and every fee-line condition:

SELECT p.name, f.name, f.condition FROM smartrules_filters f JOIN smartrules_procedures p ON p.id = f.procedure_id
UNION ALL
SELECT p.name, i.code, i.condition FROM smartrules_fee_items i JOIN smartrules_procedures p ON p.id = i.procedure_id WHERE i.condition IS NOT NULL;

Rows whose operator is IS_EMPTY, IS_NOT_EMPTY, or an AND/OR sentence convert to a qualification with no rule and a qualification_rule_missing readiness note; nothing seeds these, so query rather than assume.

One readiness tier changes: an unreadable qualification rule now blocks publish; before M101 the same filter only warned. After upgrading, republish one procedure carrying a converted fee line and confirm the canary still holds — test_refuses_a_fee_whose_qualification_is_unknown_to_the_snapshot.

Rollback: alembic downgrade M101_studio_json_order restores the sentence from legacy_condition and drops every rule authored after the upgrade; the real way back is restore-from-backup.

Upgrading across M137 — one operation for each code on a connection (2026-11-18)

A connection is the link from SmartLink to one outside register. An operation is one call that SmartLink sends through that connection. M137 (M137_smartlink_operation_code) adds a database rule: a connection has one operation for each code.

What the upgrade refuses. The upgrade stops while a connection holds two operations with one code. Its message names each such pair as connection code/operation code. It deletes nothing and it changes nothing.

The application runs its migrations when it starts. So the new release does not start while the upgrade refuses. The release that you run now continues to work until you replace it.

Before you upgrade. Run this query on the release that you run now. It lists each connection that holds a code more than one time:

SELECT c.code, o.code, count(*) FROM smartlink_operations o JOIN smartlink_connectors c ON c.id = o.connector_id GROUP BY c.code, o.code HAVING count(*) > 1;

No rows means that the upgrade continues.

What to do for each row. The two operations of a pair did not work. Each read of that pair of codes failed with a server error. Thus no check can depend on the difference between them.

  1. Open the connection in the SmartLink administration.
  2. Compare the operations that have the same code.
  3. Delete each operation that you do not want to keep.
  4. Run the query again, and continue only when it returns no rows.

If the new release already refused to start, go back to the previous release (section “Going back”) and do the four steps there. If you cannot go back, delete the extra row with SQL: DELETE FROM smartlink_operations WHERE id = '<id>';. Find the ids with SELECT id, name, created_at FROM smartlink_operations WHERE code = '<operation code>';. The database also deletes the saved examples of that operation. Its query logs stay, with no link to the operation.

Rollback: alembic downgrade M137_smartlink_operation_code-1 goes back one revision, to the revision before M137. It deletes the rule and keeps each row.