Skip to content

24 — V1 database baseline and cutover

Purpose: Consolidate pre-v1 migration history (V001–V114) into a single baseline schema, preserve all production data, archive old migrations, and cut over with a reversible maintenance window.

Audience: Operator and engineering (agent-run scripts; product owner reviews before live cutover).

Version: 1.0
Last Updated: 2026-06-04

Status: Phase 2 complete — pre-v1 migrations archived, greenfield init_db path active; live VPS v1 cutover completed 2026-06-04.

Related: 11-DATABASE-SAFETY-AND-MIGRATIONS.md · 20-PRODUCTION-DEPLOYMENT-GUIDE.md · 23-BACKUP-RESTORE-DRILL.md


TermMeaning
Baseline schemaOne SQL file with complete DDL (all tables, indexes, RLS, functions) — the foundation
DataAll existing rows copied from production (tenants, users, PHI, clinical data)
schema_migrationsOne row after cutover: V001__lyncareos_v1_baseline
Archived migrationsV001–V114 moved to migrations/archive/pre-v1/ — not run on new DBs
Forward migrationsNew changes only: V002__…, V003__… after v1

This is not replaying 114 migrations on an empty database. It is squash + data load + single marker row.


ArtifactPath
Baseline DDL (generated)backend/schema/lyncareos_v1_baseline.sql
Export scriptscripts/v1-baseline/Export-V1BaselineFromDump.ps1
Rehearsal (isolated)scripts/v1-baseline/Invoke-V1Rehearsal.ps1
Undo rehearsalscripts/v1-baseline/Undo-V1Rehearsal.ps1
Post-load (sequences + migration row)backend/tools/v1_baseline_post_load.py
Rehearsal logsbackups/v1-rehearsal/rehearsal_*.log
Dev cutoverscripts/v1-baseline/Invoke-V1Cutover-Dev.ps1
VPS cutoverscripts/v1-baseline/Invoke-V1Cutover-Vps.ps1
VPS rollbackscripts/v1-baseline/Undo-V1Cutover-Vps.ps1
VPS cutover logsbackups/v1-vps-cutover/cutover_vps_*.log

Phase 2 (repo — 2026-06-04):

  • Archive V001–V114 → backend/migrations/archive/pre-v1/ (110 files)
  • Active marker backend/migrations/V001__lyncareos_v1_baseline.sql
  • init_db() greenfield path + migrations/runner.py (active dir only)
  • conftest.py baseline seed; migration-audit.ps1 archived-row handling
  • lyncareos_dev: full v1 cutover with data restore (2026-06-04)
  • VPS production: full v1 cutover with data restore (2026-06-04) — see §6.5

Completed (live cutover):

  • Live VPS cutover — rename strategy; lyncareos_legacy retained for rollback

TargetResultFile
VPS productionOK (2026-06-04)backups/lyncareos_vps_20260604_075811.dump (+ copy on VPS /tmp/)
Dev lyncareos_devFailed (RLS)See §3.1

Pre-deploy production dump (still valid rollback reference):
backups/pre-deploy-deploy-20260603-1937.dump

3.1 Dev backup — RLS fix (one-time per machine)

Section titled “3.1 Dev backup — RLS fix (one-time per machine)”

Native dev Postgres uses FORCE ROW LEVEL SECURITY (V034). pg_dump as lyncare fails on audit_log and other tables until BYPASSRLS is granted:

Terminal window
# Add to backend/.env (gitignored):
# LYNCARE_PG_SUPER_PASSWORD=<your Windows postgres superuser password>
.\scripts\grant-dev-backup-role.ps1
.\db-backup.ps1 -Target Dev

Until then, use the latest dev custom dump if present:
backups/lyncareos_dev_20260602_173528.dump


4. Regenerate baseline DDL from production

Section titled “4. Regenerate baseline DDL from production”

Whenever production schema changes (after new migrations on VPS), refresh the baseline from a fresh VPS dump:

Terminal window
cd D:\Projects\LynCareOS
# 1. Full VPS backup (tier 1 + workstation copy)
.\scripts\vps-production-backup.ps1
# 2. Export schema-only into repo
.\scripts\v1-baseline\Export-V1BaselineFromDump.ps1 `
-DumpPath backups\lyncareos_vps_<timestamp>.dump

Rule: Baseline is generated from production (pg_restore --schema-only), never hand-merged from schema.sql + docs.


5. Rehearsal (safe — no production impact)

Section titled “5. Rehearsal (safe — no production impact)”

Rehearsal uses an isolated database lyncareos_v1_rehearsal on local Docker Postgres (:5433). It does not modify:

  • VPS lyncareos
  • Native lyncareos_dev
  • Docker lyncareos (main UAT DB)
Terminal window
cd D:\Projects\LynCareOS
# Docker postgres must be running
docker compose up -d postgres
.\scripts\v1-baseline\Invoke-V1Rehearsal.ps1 `
-DataDumpPath backups\lyncareos_vps_20260604_075811.dump

Steps performed:

  1. Drop/create lyncareos_v1_rehearsal
  2. Apply backend/schema/lyncareos_v1_baseline.sql
  3. pg_restore --data-only --disable-triggers from VPS dump
  4. python -m tools.v1_baseline_post_load (sequences + single schema_migrations row)
  5. Refresh materialized views
  6. Sanity checks (users ≥ 1, schema_migrations = 1 row)

2026-06-04 result: Rehearsal passed sanity checks with VPS dump lyncareos_vps_20260604_075811.dump
(users=9, patient_scripts=17, dispensings=2933, schema_migrations=1)

Only use when Docker lyncareos was restored from the same dump as -DataDumpPath:

Terminal window
.\scripts\v1-baseline\Invoke-V1Rehearsal.ps1 `
-DataDumpPath backups\lyncareos_vps_20260604_075811.dump `
-CompareRowCountsToSourceDb

If the main Docker DB is an older snapshot, counts will differ (expected).

5.4 Dev cutover (lyncareos_dev — native Windows Postgres)

Section titled “5.4 Dev cutover (lyncareos_dev — native Windows Postgres)”

Full v1 cutover on native dev (not isolated rehearsal). Replaces brownfield schema with lyncareos_v1_baseline.sql, reloads your dev data from a pre-cutover backup, sets schema_migrations → one row.

Terminal window
cd D:\Projects\LynCareOS
# Creates backups/v1-dev-cutover/lyncareos_dev_pre_v1_<timestamp>.dump then cutover
.\scripts\v1-baseline\Invoke-V1Cutover-Dev.ps1
# Rollback to brownfield (full restore from pre-cutover dump)
.\scripts\v1-baseline\Undo-V1Cutover-Dev.ps1 `
-DumpPath backups\v1-dev-cutover\lyncareos_dev_pre_v1_<timestamp>.dump

2026-06-04 result: PASSED — users=9, tenants=5, patients=4, dispensings=412; schema_migrations = V001__lyncareos_v1_baseline only; audit PENDING/ORPHAN: none.

Dev PHI note: Rows with enc_version=0 and missing shadow columns are repaired with tools/v1_dev_phi_repair.py (synthetic username@dev.lyncareos.local emails). Use username login for accounts that previously relied on lost email plaintext.


Terminal window
.\scripts\v1-baseline\Undo-V1Rehearsal.ps1

Drops lyncareos_v1_rehearsal only. Production and dev unchanged.


6. Live production cutover — COMPLETED 2026-06-04

Section titled “6. Live production cutover — COMPLETED 2026-06-04”

Executed via .\scripts\v1-baseline\Invoke-V1Cutover-Vps.ps1 (deploy tag deploy-20260604-0901).

MetricBefore → After
users9 → 9
tenants4 → 4
patients3 → 3
dispensings2933 → 2933
schema_migrations112 rows → 2 rows (V001__lyncareos_v1_baseline, V002__care_alert_auto_resolved)
Rollback safetylyncareos_legacy (brownfield DB unchanged on server)
Pre-cutover dump (container)/tmp/v1_pre_cutover_20260604_090318.dump
Logbackups/v1-vps-cutover/cutover_vps_20260604_090318.log
Smoke testhttps://app.lyncareos.com/api/billing/tiers → 200

Post-cutover forward migrations (2026-06): VPS cutover left V001–V002 applied on 2026-06-04 morning. V003–V005 (log-dose quick picks, first-dose checks, palliative specialist contact role) applied same day — verified in prod backup lyncareos_prod_20260604_154728.sql. Dev and VPS expected at V005.

Operator command:

.\scripts\v1-baseline\Undo-V1Cutover-Vps.ps1
.\scripts\v1-baseline\Invoke-V1Cutover-Vps.ps1

6 (reference). Live production cutover procedure

Section titled “6 (reference). Live production cutover procedure”

Do not run this section until:

  • Rehearsal passed twice on two different VPS dump dates
  • Phase 2 code merged (init_db greenfield path + archived migrations)
  • Maintenance window agreed
  • Fresh VPS backup immediately before cutover
StepCommand / action
1.\scripts\vps-production-backup.ps1
2.\migration-audit.ps1 -Target Vps -NonInteractive → PENDING: none
3Note current Docker tag (rollback): e.g. deploy-20260603-1937
4Export latest baseline from that dump (§4)
5Enable maintenance / stop app + scan_worker on VPS
app containerlyncareos_v1lyncareos (legacy)VPS Postgresapp containerlyncareos_v1lyncareos (legacy)VPS Postgrespg_dump -Fc full backupStop app / scan_workerCREATE DATABASE lyncareos_v1Apply lyncareos_v1_baseline.sqlpg_restore --data-only (final dump)python -m tools.v1_baseline_post_loadREFRESH MATERIALIZED VIEW ...DATABASE_URL → lyncareos_v1Deploy v1-aware image + smoke test

SSH outline (operator — adapt paths):

Terminal window
# Final data-only dump while app stopped
ssh -i $env:USERPROFILE\.ssh\lyncareos_vps root@134.199.173.206 `
'cd /opt/lyncareos && docker compose exec -T postgres pg_dump -U lyncare -Fc --data-only -d lyncareos -f /tmp/v1_cutover_data.dump'
# Create new DB, apply baseline (baseline file scp'd to VPS or baked in image)
# pg_restore --data-only into lyncareos_v1
# docker compose run --rm app python -m tools.v1_baseline_post_load
# Update .env.production DATABASE_URL to lyncareos_v1 (or rename DB — see §6.3)
# docker compose restart app scan_worker
OptionProsCons
New DB lyncareos_v1 + URL changeOld DB untouched for rollbackTwo DBs until old dropped
Rename lyncareos → lyncareos_legacy, lyncareos_v1 → lyncareosApp URL unchangedSlightly more complex rename steps

Rollback (within 7–14 days):

  1. Point DATABASE_URL back to lyncareos (legacy DB unchanged)
  2. .\scripts\deploy.ps1 -SkipBuild -Tag deploy-20260603-1937 (or last known-good tag)
  3. Verify https://app.lyncareos.com/api/billing/tiers → 200
CheckExpected
migration-audit.ps1 -Target VpsPENDING: none; one baseline row in schema_migrations
Login / medications / care alertsReal data visible
PHINo re-backfill; same PHI_* keys on VPS
Uploads volumeUnchanged path (lyncare_uploads)
Manual QA22-PRODUCTION-MANUAL-QA.md

7. Phase 2 — repo/code changes (complete 2026-06-04)

Section titled “7. Phase 2 — repo/code changes (complete 2026-06-04)”
TaskStatus
Archive migrationsbackend/migrations/archive/pre-v1/ — 110 pre-v1 SQL files
Active migrationV001__lyncareos_v1_baseline.sql — no-op marker
init_db()Greenfield → lyncareos_v1_baseline.sql; brownfield unchanged
migrations/runner.pyEnsures schema_migrations; globs active dir only
conftest.pyBaseline on empty test DB; seeds active migrations only
migration-audit.ps1ARCHIVED rows OK; PENDING = active files only

  • No VPS DATABASE_URL change
  • No migration archive in repo (would break current dev startup until Phase 2) — done; brownfield dev/VPS apply only V001__lyncareos_v1_baseline on next startup
  • No public “v1.0” product tag at cutover (optional after live verification)

Terminal window
# Backup VPS + dev (after grant-dev-backup-role for dev)
.\scripts\vps-production-backup.ps1
.\db-backup.ps1 -Target Dev
# Regenerate baseline
.\scripts\v1-baseline\Export-V1BaselineFromDump.ps1 -DumpPath backups\lyncareos_vps_*.dump
# Rehearse (safe)
.\scripts\v1-baseline\Invoke-V1Rehearsal.ps1 -DataDumpPath backups\lyncareos_vps_*.dump
# Undo rehearsal
.\scripts\v1-baseline\Undo-V1Rehearsal.ps1

I need to…Read
Normal deploy (no cutover)20-PRODUCTION-DEPLOYMENT-GUIDE.md
Migration authoring after v111 §3
Backup / restore drill23-BACKUP-RESTORE-DRILL.md