Skip to content

Database Layout and Migrations Runbook ​

How Cadence services store data in PostgreSQL, how schema changes reach an environment, and what to do when they fail. The decisions behind it are ADR-2247 (one database, schema per module) and ADR-2447 (how the schemas and migrations are applied).

Layout ​

Every service connects to the same database, cadence, through ConnectionStrings__cadence (or the legacy POSTGRES_CONNECTION_STRING; see ENVIRONMENT_VARIABLES.md). Each service owns exactly one schema, including its own __EFMigrationsHistory table.

ServiceSchemaDbContext
ctmsctmsCtmsDbContext
econsenteconsentEconsentDbContext
eisfeisfEisfDbContext
etmfetmfEtmfDbContext
executionedcExecutionDbContext
filesharefileshareFileshareDbContext
imagingimagingImagingDbContext
interopinteropInteropDbContext
knowledgeknowledgeKnowledgeDbContext
notificationsnotificationsNotificationsDbContext
orgorgOrgDbContext
paymentspaymentsPaymentsDbContext
qualityqualityQualityDbContext
safetysafetySafetyDbContext
ticketsticketsTicketsDbContext

The kernel packages qualify their own tables with the kernel schema. public holds only extensions.

The schema is applied by setting the connection's search path to <schema>,public (CadenceDatabaseExtensions.WithModuleSchema), so services and dotnet ef never need schema-qualified SQL. When you query by hand, set it yourself: SET search_path TO ctms, public;.

Applying migrations ​

EnvironmentSettingWhat happens
Local compose, single instanceDatabase__MigrateOnStartup=trueThe service migrates before its web server starts; if migration fails, it does not start.
Multi-replica deploymentDatabase__MigrateOnly=true on a one-off job or init container, before the rollout; unset on the replicasThe job migrates and exits 0 (non-zero on failure). Replicas never change the schema.
Regulated releaseneither; a DBA applies a reviewed scriptSee "Idempotent scripts" below.

A migrate-only job runs the normal service image with the same connection string and secrets as the service. Services refuse to start without their required settings (GATEWAY_SECRET, AUDIT_LOG_SECRET_KEY and, for execution, DemographicsEncryption__KeyBase64; see ENVIRONMENT_VARIABLES.md):

bash
docker run --rm \
  -e ConnectionStrings__cadence="Host=db;Database=cadence;Username=...;Password=..." \
  -e GATEWAY_SECRET=... \
  -e AUDIT_LOG_SECRET_KEY=... \
  -e Database__MigrateOnly=true \
  cadence/ctms:<version>

Readiness (/healthz/ready) fails while the database is unreachable, so orchestrators hold traffic until the database is back.

Idempotent scripts (DBA review, GxP qualification) ​

bash
scripts/manage_migrations.sh script <service>     # or: pwsh scripts/manage_migrations.ps1 script <service>

The script is unqualified; run it with SET search_path TO <schema>, public; after CREATE SCHEMA IF NOT EXISTS <schema>;.

Adding a migration ​

bash
scripts/manage_migrations.sh add <service> <MigrationName>

Then run dotnet test tests/Cadence.UnitTests/Cadence.UnitTests.csproj --filter FullyQualifiedName~MigrationUnitTests. It fails when a DbContext model has changes without a migration, which would otherwise break the next deployment with PendingModelChangesWarning. Never hand-edit a generated migration to create another module's tables; each module owns only its schema.

Then run pnpm cadence generate-schema-doc and commit the updated docs/schema pages, which describe every table, column, key and index per module. The schema-doc gate fails while they differ from the models.

Troubleshooting ​

SymptomCauseFix
PendingModelChangesWarning at startupModel changed without a migrationAdd a migration for that service
relation ... already existsA local database created before ADR-2447 has tables in publicDrop and recreate the local database (docker compose down -v)
Service exits with No PostgreSQL connection string configuredNeither connection key set outside DevelopmentSet ConnectionStrings__cadence
/healthz/ready returns 503Database unreachable or credentials wrongCheck connectivity from the service's network