# Upgrade

Move a self-hosted Cohorly to a new version: back up first, change the image tag, recreate the containers, check the result, and what going back to the previous version means.

An upgrade changes the version of the server and dashboard images. Your data
stays in the `selfhost-pgdata` volume and the database is migrated by the new
server when it starts.

Install from the first version delivered to you under your contract. Versions
before it were never released to customers and are not supported.

## Before you start

- **Check that your license covers the new version.** The key covers images
  released up to its `updatesUntil` date, and an image released later refuses
  to start (see [License key](/self-hosting/images#license-key)). Look at
  `GET /api/usage` on the running instance to see your date, and renew before
  you upgrade if the new image was released after it.
- **Read the [changelog](/changelog)** for the versions between yours and the
  new one.
- **Take a backup, and keep it.** Going back means restoring it, so it must be
  taken before the new version starts. Follow
  [Backup and restore](/self-hosting/backup) and put the files somewhere other
  than the host.
- **Check the length of your signing secrets.** The API refuses to start when
  `COHORLY_MCP_JWT_SECRET`, `COHORLY_MCP_JWT_SECRET_PREVIOUS` or
  `COHORLY_EXPORT_LINK_SECRET` is set to fewer than 32 bytes. Replace a
  shorter one with the output of `openssl rand -base64 32` before you upgrade.

## Upgrade

1. Get the new images the way you got the first ones
   ([Getting the images](/self-hosting/images#getting-the-images)):
   - From the Cohorly registry or your own: nothing to do yet.
   - From the offline package: check and load the new package, then note the
     repository names `docker load` prints, as they can change.

     ```bash
     sha256sum -c --ignore-missing SHA256SUMS
     docker load < cohorly-1.1.0-linux-amd64.tar.gz
     ```

2. Set the new version in `.env`. If you copied the images to your own
   registry, copy the new tags there first.

   ```
   COHORLY_IMAGE_TAG=1.1.0
   ```

3. Pull (skip this step with the offline package) and recreate the containers:

   ```bash
   docker compose pull server error-worker dashboard
   docker compose up -d --no-build
   ```

   Compose recreates `server`, `error-worker` and `dashboard` because their
   image changed, and leaves `postgres` and `caddy` running.

4. Watch the API start:

   ```bash
   docker compose logs -f server
   ```

   The server applies the migrations of the new version before it opens its
   port. They are idempotent and recorded in the `schema_migrations` table, so
   a start that was interrupted resumes where it stopped. The error worker
   never migrates: it starts once the API reports healthy. Some index changes
   on a large events table run in the background after the API is up; a
   failure is logged and the next start resumes it.

5. Check the result:

   ```bash
   docker compose ps
   curl https://cohorly.example.com/health
   ```

   Every service should be `healthy`, and `/health` answers
   `{"ok":true,"db":true}`. Then sign in and open a few reports.

The upgrade has no separate migration command and no separate downtime window
beyond the time the API takes to restart and migrate. Events that devices send
meanwhile are retried by the SDKs.

## Going back

**Migrations only go forward.** A new version can change the database in ways
the previous version does not understand, and there is no command that undoes
them. Starting the previous image on a migrated database is not supported.

To go back, restore the backup you took before the upgrade, and run the
previous version:

1. Stop the stack: `docker compose down` (without `-v`, which would delete
   your volumes).
2. Set `COHORLY_IMAGE_TAG` in `.env` to the previous version.
3. Restore the database, and the blobs if you back them up, from the backup
   taken before the upgrade. The steps are in
   [Restore](/self-hosting/backup#restore).
4. Start the stack: `docker compose up -d --no-build`.

Anything written after that backup, including events sent after the upgrade,
is lost. For that reason, check the new version right after the upgrade, while
it is cheap to go back.

If the new image refuses to start with a license message, nothing was
migrated: the license is checked before the database is touched. The instance
is down, though. `server` keeps restarting, and the dashboard and the error
worker do not start because they wait for the API. Set `COHORLY_IMAGE_TAG` back
to the previous version and run `docker compose up -d --no-build` to bring it
back, with its data as it was, or renew the key and run the same command.
Checking the license before you upgrade avoids the outage.
