# N-1/N upgrade test

**Last updated:** 2026-11-17T17:53:36-03:00 (UTC-3)

During a rolling upgrade the previous release (N-1) and the new one (N) run against the same PostgreSQL database for a while. The N-1/N upgrade test proves what happens then, with the real binaries: it builds N-1 from a git ref and N from the current tree, and runs both against one database.

## Run it

```sh
npm --prefix web ci && npm --prefix web run build   # once: the binary embeds the console
scripts/upgrade-n1-n.sh --previous v1.4.0           # a release tag, or any accepted commit
```

| Input | Meaning | Default |
| --- | --- | --- |
| `--previous <ref>` or `GATEWAY_UPGRADE_PREVIOUS_REF` | The N-1 git ref: a release tag or an accepted commit | The newest released version in `CHANGELOG.md` (tag `v<version>`); the script stops and asks for a ref when there is none |
| `GATEWAY_UPGRADE_DATABASE_URL` | A PostgreSQL server where the test may create and drop its own database | An owned `postgres:16.15-alpine` container (pinned by digest) on a random local port, removed with its volumes at the end (needs Docker) |
| `--keep` | Keep the temporary directory with both binaries | off |

The script exports the N-1 tree with `git archive` into a temporary directory and builds it there; it never checks anything out in your working tree. When the old tree has no console build, the current one is embedded: the test exercises the API, not the console. It then runs `go test -run '^TestUpgradeN1N$' ./internal/deploytest/`, which skips unless both binaries and the database are given.

## What it checks

1. **N-1 alone** creates the schema, saves a model override through the console API and serves inference with usage recorded.
2. **N joins** the same database while N-1 keeps serving, and applies its new migrations (the test fails if N applies none).
3. **N-1 keeps working** during the rollout: inference, reads of its configuration, and a console write (accepted, or refused cleanly with a status, never a crash).
4. **N reads** what N-1 wrote and writes itself. A write right after N-1's answers `409` until N's control refresh sees the newer configuration version, as for any two replicas.
5. **A restarted N-1 refuses** the newer schema at boot: it exits with an error that says the database has a migration newer than it supports, instead of serving.
6. **Nothing is lost:** one usage event per provider call from both binaries, the latest model override, and the audit rows of both writes.

The test owns its database (`upgrade_<random>`) and drops it at the end; the mock provider runs inside the test process. No provider is called.

## What it means for your rollout

- Keep N-1 replicas running while N migrates: they keep serving with the schema they know.
- Do not restart an N-1 replica after N migrated: it refuses to start. Roll back by restoring the database backup taken before the upgrade, or by running N again.
- Per-release rollback steps (flags and fields an older binary refuses) are in `RELEASED.md` in the source tree; turn off the new flags and remove the new fields before rolling back.

## Limits

- The test covers the default PostgreSQL profile and the API, not optional engines, the console or the Helm chart.
- An N-1 build needs the Go toolchain of that release and network access for its modules when they are not in the local module cache.
