Upgrading an existing installation
This page describes how to move an instance that already holds data from one Payload Bay release (image version N) to a later one (N+1) without losing data, and what to do when something goes wrong.
How the schema changes between releases
Schema source of truth is packages/domain/src/schema.ts plus
packages/domain/sql/runtime.sql. After a source change merges, CI generates one
reviewed SQL migration (and its Drizzle journal entry) into
packages/domain/drizzle/. Feature branches never commit release migrations.
Every release image of the migrator contains the complete, ordered set of migrations up to that release. The migrator is the only supported way to change an operator database:
- the operator stack runs it as the one-shot
migratorservice, - the single-container stack runs the same migrator image as
the one-shot
schemaservice, - both start the application only after the migrator exits successfully.
bun run db:push and PAYLOADBAY_SCHEMA_MODE=initialize-only are development and
disposable-preview tools. initialize-only initializes an empty database and now
fails, rather than silently skipping, when the database already holds a schema.
What the migrator guarantees
Applied migrations are recorded in payloadbay.schema_migration (file name, SHA-256
digest of the SQL, timestamp). On every run, under a PostgreSQL advisory lock so
that concurrent runs serialize, the migrator checks the history before changing
anything and refuses with a specific error when:
| Error code | Situation | Meaning |
|---|---|---|
migration_database_newer | The database recorded a migration this image does not ship | The image is older than the database. Deploy the matching or a newer image. Never run an older image against a newer schema |
migration_history_gap | A migration that precedes a recorded one is not recorded | The history was edited or partially restored. Restore a consistent backup |
migration_digest_mismatch | A recorded migration's SQL differs from the image's copy | The reviewed SQL was changed after it was applied. Use the exact release image |
migration_missing | The image's journal lists a migration whose file is absent | Damaged or truncated image. Pull the image again |
migration_unlisted | A migration file is not in the journal | Unreviewed SQL in the image |
migration_untracked_database | A Payload Bay schema exists but no migration is recorded | Database initialized by the development push. See baseline |
migration_unknown, migration_baseline_rejected | Invalid --through or baseline request | Operator input error |
Otherwise it applies each pending migration in order, each in its own transaction. A failing migration rolls back completely, leaves nothing recorded, and the migrator exits non-zero with an error, so the application is never started on a half-migrated database. After the cause is fixed, re-running resumes at the failed migration. Running the migrator again at head applies nothing and exits zero.
Migrations are forward-only. There are no down migrations.
Upgrade procedure
- Read the release notes for the target version (required configuration, breaking changes, data mapping).
- Back up first. Follow backup: a complete
pg_dump --format=customarchive of the database, role definitions, the encryption key and the current image digests. Verify the archive is readable (pg_restore --list). Do not proceed without it. - Stop ingress and worker consumption (stop
app/payload-bayandworker), so nothing writes while the schema changes. Accepted requests are already durable. - Update the image references (operator stack: the coordinated app, worker, executor and migrator digests; single container: check out the target version and rebuild).
- Run the migrator:
docker compose ... run --rm migrator(operator) ordocker compose ... up schema(single container). The log ends withPayload Bay migrations complete: N applied, database at <migration>. - Start the services and check readiness. Verify a recent event, its attempts and a controlled delivery before reopening traffic.
Re-running step 5 is always safe.
Databases created before migration tracking
Earlier single-container builds initialized the schema with the development push and
recorded no migration history. The migrator refuses such a database with
migration_untracked_database instead of guessing. If you know which release the
schema corresponds to, take the backup, then record that state explicitly:
PAYLOADBAY_MIGRATION_BASELINE=<migration file name> docker compose -f deploy/compose.single.yml up schemaThe same is available as --baseline-through <migration file name>. Baselining
records every migration up to and including the named one as already applied
without running it, then applies the remaining ones. It only asserts what you
tell it. If you cannot tell which migration your schema matches, restore the data
into a fresh database instead: start a new stack, let the migrator initialize it,
and import the rows you need. Remove the variable after the first successful run.
Rollback
Because migrations are forward-only, rollback means restore, not downgrade:
- Stop the application, worker and migrator.
- Restore the pre-upgrade archive into an empty PostgreSQL 17 target as described in backup, keeping the failed database for diagnosis.
- Start the previous release's images (including its migrator) against the restored database. The restored history matches that release, so the migrator is a no-op.
Do not start an older image against an upgraded database: the migrator will refuse
with migration_database_newer, and running application code that predates the
schema is unsupported. Work accepted after the backup is not in the archive; the
restore loses it, and restored pending deliveries may repeat, so receivers must
deduplicate.
What the tests prove
tests/integration/upgrade.test.ts builds a database at the earliest shipped
release, inserts an installation, organization, projects, routes, binary events,
executions, managed operations, delivery attempts and dispatches, runs the migrator to
head, and asserts row counts, key rows (bodies and headers byte for byte), validated
foreign keys, no orphaned rows and that the application's own event and attempt
reads work. It also covers the no-op re-run, empty-database initialization,
newer-than-image refusal, missing/unlisted/gapped/changed migrations, failed
migration rollback and the baseline path. tests/deployment/migrator.test.ts
runs the packaged migrator image.