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:
-
6 PostgreSQL containers consume ~600MB of idle RAM and occupy ports 5432-5437.
-
Startup time is longer — 6 health checks instead of 1.
-
Developers working on a single program still pay the cost of all 5 program databases.
-
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.ymloverride file that disables per-program postgres containers and remaps DATABASE_URL env vars -
Additional
CREATE DATABASEstatements in the shared postgres init script -
--shared-dbflag oncargo xtask dev start,stop, andclean -
compose_cmdandcompose_cmd_with_filesinxtask/src/docker.rsto support additional-fcompose 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:
-
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]
# ...
-
Override the
DATABASE_URLenv vars on program services to point at the sharedpostgres:5432:
services:
canopy-snap:
environment:
CANOPY_SNAP__DATABASE_URL: "postgres://canopy:canopy@postgres:5432/canopy_snap"
depends_on:
postgres:
condition: service_healthy
-
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:
-
Move per-program postgres containers behind an
isolated-dbprofile so they don’t start -
Override the 5 program service
DATABASE_URLenv vars to point atpostgres:5432 -
Override the 5 program service
depends_onto referencepostgresinstead ofpostgres-{program} -
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
-
cargo clippy --workspace --all-targets — -D warnings -
cargo nextest run --workspace— all tests pass -
cargo xtask dev start— default behavior unchanged (6 postgres containers) -
cargo xtask dev start --shared-db— only 1 postgres container, all 17 databases present -
Verify services connect and run migrations on shared postgres
-
cargo xtask dev stop --shared-db— clean shutdown -
cargo xtask dev clean --shared-db --confirm— removes volumes -
Update
.claude/docs/local-dev.mdwith--shared-dbusage -
Update
CHANGELOG.adoc
Files Touched
| File | Change |
|---|---|
|
New: compose override disabling per-program postgres, remapping DATABASE_URLs |
|
New: CREATE DATABASE for the 5 program databases |
|
Add |
|
Add |
|
Document |
|
Entry under |
Verification
-
cargo nextest run --workspace --lib— unit tests pass -
cargo xtask dev start— default isolated mode works (6 postgres containers) -
cargo xtask dev start --shared-db— shared mode works (1 postgres container, 17 databases) -
cargo xtask dev status— all services healthy in both modes -
cargo xtask dev stop/cargo xtask dev stop --shared-db— clean shutdown -
cargo xtask dev clean --confirm/cargo xtask dev clean --shared-db --confirm— volumes removed -
cargo xtask validate— full pre-push validation passes
Documentation Updates
-
.claude/docs/local-dev.md— document--shared-dbflag 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).