Devstack Migration Snapshot & Rollback

Overview

For a contributor about to apply or review a migration on the devstack: cargo xtask migrate snapshot dumps every Canopy database into .devstack/snapshots/<timestamp>/ and cargo xtask migrate rollback restores from one (xtask/src/cmd/migrate.rs).

This is dev/CI tooling only. Migrations are forward-only (ADR-016); production rollback is pg_basebackup plus WAL point-in-time recovery — see Database Backup & Restore.

Concern Tool

Production point-in-time recovery

pg_basebackup + WAL archive (Backup & Restore)

Local dev — snapshot before applying a new migration

cargo xtask migrate snapshot

Local dev — undo a broken migration

cargo xtask migrate rollback

Deploy-time migration job (runtime runs with SKIP_MIGRATIONS)

canopy-manage migrate apply --service <svc> (Deployment Guide › Migrations)

Down migrations

None, by design (ADR-016). A wrong migration is fixed forward or, when data was destroyed, recovered with PITR — never a down file.

When to use

  • About to merge a migration MR that touches a table you don’t own — snapshot first, run cargo xtask dev refresh to apply, smoke-test, roll back if the migration breaks something unexpected.

  • Reviewing a migration MR locally — snapshot, check out the branch, apply migrations via cargo xtask dev refresh, then roll back to your clean state when done.

  • Reproducing a determined-to-be-broken state in CI for a bug — capture a snapshot from an affected developer’s box, ship it as an attachment, restore on your own devstack with cargo xtask migrate rollback --id <ts>.

Commands

Command Effect

cargo xtask migrate snapshot

Runs pg_dump --format=custom --no-owner --no-privileges against each of the eighteen databases in the migrator’s DATABASES list (the same inventory Deployment Guide › Data tier shows) via docker exec into the owning Postgres container — <project>-postgres-<program>-1 for an isolated program database, the shared <project>-postgres-1 otherwise (container_for_db in xtask/src/docker.rs). Writes archives + a manifest.json (snapshot id, git HEAD SHA, shared-db flag) to .devstack/snapshots/<timestamp>/. Skips databases that don’t exist.

cargo xtask migrate list

Prints all available snapshots in chronological order with their size, entry count, and git HEAD SHA at snapshot time.

cargo xtask migrate rollback

Restores from the most recent snapshot. Each archive is piped into pg_restore --clean --if-exists --no-owner --no-privileges --exit-on-error so existing tables are dropped and recreated. Aborts on first error rather than continuing through partial state.

cargo xtask migrate rollback --id 20260430T174056Z

Restore from a specific snapshot by id.

cargo xtask migrate rollback --db canopy_snap [--db canopy_tanf]

Restore only the named database(s). Pass --db multiple times to restore a subset. The filter is validated against the manifest BEFORE any restore runs — a typo aborts cleanly instead of partially restoring. Combine with --id <ts> to scope to an older snapshot.

canopy-manage migrate apply --service canopy-<svc>

Not a snapshot command, and not an xtask verb: applies the migrations of canopy-<svc> compiled into canopy-manage as the deploy-time job, reading the URL from CANOPY_MIGRATE__DATABASE_URL (required; there is no devstack fallback: Local Development › canopy-manage against the devstack; run with CANOPY_ENV=development against the non-TLS devstack). Same name guard and KEK preflight as a service boot.

Output paths

.devstack/snapshots/
  20260430T174056Z/
    manifest.json
    canopy_appeals.dump
    canopy_applications.dump
    ...
    canopy_wic.dump

The .devstack/ directory is gitignored — snapshots live on the developer’s machine only and never get checked in.

Manifest schema

{
  "snapshot_id": "20260430T174056Z",
  "git_sha": "ca0f836f44a773493a5fb6161f336724a612efdb",
  "shared_db": false,
  "entries": [
    {
      "database": "canopy_snap",
      "container": "canopy-postgres-snap-1",
      "archive": "20260430T174056Z/canopy_snap.dump",
      "size_bytes": 371320
    }
  ]
}

Limitations

  • Devstack only. Container names are derived from the compose project name (<project>-postgres-*-1). Production databases live behind a different naming scheme (cloud-managed Postgres, host:port endpoints with TLS) — pg_basebackup is the right tool there.

  • Logical dump, not physical. Restored databases get the same data but may differ in toast/WAL state. Fine for dev; not equivalent to a physical replica.

  • No incremental snapshots. Each snapshot is a full dump of every database.

  • Per-database rollback uses the --db flag; no need to drop into raw pg_restore for the common case.

Implementation notes

Source: xtask/src/cmd/migrate.rs. Container naming: container_for_db in xtask/src/docker.rs. Migration authoring rules and recovery paths: Migrations.

Edit this page · default