Plan: Shared Database Mode for Devstack

Status

Step Description Status

1

Create docker-compose.shared-db.yml override file

Done (2026-04-05)

2

Add program databases to shared postgres init.sql

Done (2026-04-05)

3

Add --shared-db flag to xtask dev start/stop/clean

Done (2026-04-05)

4

Update compose_cmd to support extra compose files

Done (2026-04-05)

5

Tests, documentation, validation

Done (2026-04-05)

Epic: &38
Issues: TBD
Branch: chore/shared-db-devstack
Labels: type::chore, priority::medium, program::infrastructure, service::devstack

Context

Canopy’s production architecture requires per-program database isolation (ADR-001, ADR-004). The devstack mirrors this by running 6 separate PostgreSQL containers: one shared instance for infrastructure services (12 databases) and 5 per-program instances (canopy_snap, canopy_tanf, canopy_medicaid, canopy_caps, canopy_wic).

This is correct for production and integration testing, but unwieldy for daily development:

  1. 6 PostgreSQL containers consume ~600MB of idle RAM and occupy ports 5432-5437.

  2. Startup time is longer — 6 health checks instead of 1.

  3. Developers working on a single program still pay the cost of all 5 program databases.

  4. Most development doesn’t exercise cross-program data isolation guarantees.

A shared-db mode consolidates all databases onto the single postgres container. Services don’t know the difference — they receive a DATABASE_URL pointing at the same host with a different database name. The isolation guarantees are maintained at the database level (separate databases, same instance), just not at the container level.

Scope

In scope:

  • docker-compose.shared-db.yml override file that disables per-program postgres containers and remaps DATABASE_URL env vars

  • Additional CREATE DATABASE statements in the shared postgres init script

  • --shared-db flag on cargo xtask dev start, stop, and clean

  • compose_cmd and compose_cmd_with_files in xtask/src/docker.rs to support additional -f compose files

Out of scope:

  • Changing the default behavior (default remains isolated, matching production)

  • Modifying any service code (services are DATABASE_URL-driven, no code changes needed)

  • Production deployment configuration

Design

Compose Override Pattern

Docker Compose supports layered files: docker compose -f docker-compose.yml -f docker-compose.shared-db.yml up. The override file uses YAML merge semantics:

  1. Disable the 5 per-program postgres containers by overriding them with empty profiles (they won’t start unless the profile is explicitly activated):

services:
  postgres-snap:
    profiles: [isolated-db]
  postgres-tanf:
    profiles: [isolated-db]
  # ...
  1. Override the DATABASE_URL env vars on program services to point at the shared postgres:5432:

services:
  canopy-snap:
    environment:
      CANOPY_SNAP__DATABASE_URL: "postgres://canopy:canopy@postgres:5432/canopy_snap"
    depends_on:
      postgres:
        condition: service_healthy
  1. Add the 5 program databases to the shared postgres init — via a separate init script mounted in the override.

xtask Integration

compose_cmd currently hardcodes a single compose file. Add a compose_cmd_with_files function that accepts &[&str] of compose file paths. dev start checks for --shared-db and passes the extra -f argument.

#[derive(clap::Subcommand)]
pub enum Action {
    Start {
        /// Use a single shared PostgreSQL instance for all databases
        #[arg(long)]
        shared_db: bool,
    },
    // ...
}

Steps

Step 1: Create docker-compose.shared-db.yml

Files: docker-compose.shared-db.yml (new)

Create the override file at the workspace root. It must:

  1. Move per-program postgres containers behind an isolated-db profile so they don’t start

  2. Override the 5 program service DATABASE_URL env vars to point at postgres:5432

  3. Override the 5 program service depends_on to reference postgres instead of postgres-{program}

  4. Mount an additional init script into the shared postgres container

Step 2: Create shared-db init script

Files: devstack/postgres/init-program-dbs.sql (new)

Create a SQL file that creates the 5 program databases:

-- Program service databases (shared-db mode only)
-- In isolated mode, these are created by their own postgres containers.
CREATE DATABASE canopy_snap;
CREATE DATABASE canopy_tanf;
CREATE DATABASE canopy_medicaid;
CREATE DATABASE canopy_caps;
CREATE DATABASE canopy_wic;

The override file mounts this into /docker-entrypoint-initdb.d/ on the shared postgres container.

Step 3: Add --shared-db flag to xtask dev

Files: xtask/src/cmd/dev.rs

Add shared_db: bool to Action::Start. Pass it through to the compose invocation. Also pass it to stop and clean so they target the same compose file set (otherwise orphan containers from the shared-db run won’t be cleaned up).

Step 4: Update compose_cmd to support extra files

Files: xtask/src/docker.rs

Add a new function:

pub fn compose_cmd_with_files(
    project: &str,
    compose_files: &[&str],
    args: &[&str],
) -> Result<()>

The existing compose_cmd delegates to this with &["docker-compose.yml"]. When --shared-db is active, the caller passes &["docker-compose.yml", "docker-compose.shared-db.yml"].

Each file becomes a -f argument to docker compose.

Step 5: Tests, documentation, validation

  1. cargo clippy --workspace --all-targets — -D warnings

  2. cargo nextest run --workspace — all tests pass

  3. cargo xtask dev start — default behavior unchanged (6 postgres containers)

  4. cargo xtask dev start --shared-db — only 1 postgres container, all 17 databases present

  5. Verify services connect and run migrations on shared postgres

  6. cargo xtask dev stop --shared-db — clean shutdown

  7. cargo xtask dev clean --shared-db --confirm — removes volumes

  8. Update .claude/docs/local-dev.md with --shared-db usage

  9. Update CHANGELOG.adoc

Files Touched

File Change

docker-compose.shared-db.yml

New: compose override disabling per-program postgres, remapping DATABASE_URLs

devstack/postgres/init-program-dbs.sql

New: CREATE DATABASE for the 5 program databases

xtask/src/cmd/dev.rs

Add --shared-db flag to Start, Stop, Clean actions

xtask/src/docker.rs

Add compose_cmd_with_files, update compose_cmd to delegate

.claude/docs/local-dev.md

Document --shared-db flag

CHANGELOG.adoc

Entry under == Unreleased

Verification

  1. cargo nextest run --workspace --lib — unit tests pass

  2. cargo xtask dev start — default isolated mode works (6 postgres containers)

  3. cargo xtask dev start --shared-db — shared mode works (1 postgres container, 17 databases)

  4. cargo xtask dev status — all services healthy in both modes

  5. cargo xtask dev stop / cargo xtask dev stop --shared-db — clean shutdown

  6. cargo xtask dev clean --confirm / cargo xtask dev clean --shared-db --confirm — volumes removed

  7. cargo xtask validate — full pre-push validation passes

Documentation Updates

  • .claude/docs/local-dev.md — document --shared-db flag and when to use it

  • CHANGELOG.adoc — entry under == Unreleased

Errata

The plan originally called for a separate docker-compose.shared-db.yml override file. During implementation, we consolidated into the single docker-compose.yml using Docker Compose profiles (isolated-db) and ${VAR:-default} env var interpolation. This is simpler — one file to maintain, no override file management in xtask.

stop and clean no longer need --shared-db since --profile isolated-db is always passed on stop/clean (it’s harmless if the profiled containers aren’t running).

Edit this page · default