Updates and the database
This page is for owners who want to know why updates are safe to let run. After reading it you can explain how the database gets its shape and keeps it through updates, what a release is, and how your server knows a release is genuine.
The steps an update takes, the commands and what rollback does are on Updates and rollback. Backups and restores are on Backups and restore.
Migrations
The platform's database changes shape over time: a new feature needs a new table or column. Each change is one numbered file, a migration, applied in number order. Each one makes its change and then records itself in a table called public.schema_migrations (its version number and a short note). That table is the record of what has been applied, so nothing is ever guessed.
The rules every migration follows:
- It runs in one transaction. If any statement fails, none of the migration is applied and the run stops. A half-applied change is the worst state a database can be in, so it is never allowed.
- It records itself. A migration that runs but writes no record counts as a failure.
- It works on every install. A migration never adds rows that only one install should have, such as a person, a channel or a price.
Who runs them: the migrate job
Every time the install starts, before stream-api, stream-ingest or the website, a one-shot container called migrate runs and then exits. The other programs wait for it to succeed, so they never run against a database older than their code. It decides what to do from what it finds:
| The database is | It does |
|---|---|
| Empty (a new install) | Builds it from the baseline, then applies every migration newer than the baseline |
| One it built before | Applies only the migrations not yet recorded, or says it is up to date |
Not empty, but with no schema_migrations table |
Refuses and stops. A database it did not make is not its to change |
You can also run it by hand:
sudo blackbranch migrate
The fresh-install baseline
The full list of migrations is long, and much of it is history: tables made and later dropped, columns renamed, data moved. Replaying all of that on every new install would be slow and would carry every old mistake along. So a new install starts from a baseline instead:
- the schema: the exact shape a full replay of every migration leaves, with no data, plus one
schema_migrationsrecord for each version it includes. A tool generates it; nobody edits it by hand; - the seed: the few rows the product cannot run without, each one neutral (for example the single row that holds the slow-mode setting).
Both are applied together in one transaction, then any newer migrations. The seed runs again, harmlessly, on every start, so a row that went missing for any reason comes back.
The project checks the baseline on every change: in a throwaway database it builds the database both ways (every migration from the start, and baseline plus seed plus newer migrations) and compares every table, column, index, constraint, function and seed row. Any difference fails the change. So a new install and a years-old install end up with the same database.
The pgvector add-on needs a database superuser to install it. The baseline is the only step that needs one: for that step alone the job raises the platform's own database user to superuser and lowers it again afterwards. Every table ends up owned by the platform's user, and nothing else runs with more rights than it needs.
Your .env has migrations too
The settings file .env can also need changes when a release renames, adds or retires one of its keys. Each release carries small numbered scripts for that, and BB_ENV_VERSION in .env records the last one applied. They never print a value. An update runs them on a copy of .env first, and stops before changing anything if the result would be missing a key the release needs.
Releases
A release is one version of everything on the server, pinned exactly:
- the source of stream-api, stream-ingest and the backup tool, as one archive that your server checks by its size and SHA-256 fingerprint before building those images itself;
- every other image the install runs, by digest: the website (one prebuilt image for every install), the database, the homeserver and its sign-in service, the router, the tunnel and the build's base images. A digest is a fingerprint of the image's exact contents, so a name re-published later by someone else can never change what a release runs.
How your server knows a release is genuine
The newest release on each channel (stable or beta; every release so far is a preview on beta) is announced in a signed feed, a small file the project publishes at blackbranch.app/releases. Your server checks the signature with the one public key it was installed with (BB_RELEASE_KEY in .env). The signature uses Ed25519 in a DSSE envelope: the announcement travels as exact bytes and the signature covers those bytes, so no two programs can disagree about what was signed. An update never changes the key.
Your server refuses:
- a release older than the one it runs;
- a feed for another channel;
- a feed that has expired without being signed again, or one older than a feed it has already read (a replayed feed);
- any release that would move the database to another major version of Postgres. That is always a separate, announced step.
Why an update backs up before it switches
Migrations only move forward: there is no "undo" file for each one. Going back to an older release therefore means going back to the database as it was before the new release's migrations ran. That is why every update stops the site, makes a backup, and only then switches to the new release. If the new release fails its health check, the server restores the databases from that backup instead of trying to reverse the changes. The whole sequence is on Updates and rollback.
Next: Docs home