import subprocess as sp, tempfile, pathlib, os, time, json ROOT=pathlib.Path(__file__).resolve().parents[2] name='canina-v4-completion-rehearsal' def run(args,**kw): return sp.run(args,check=True,**kw) def sql(q): return sp.check_output(['docker','exec','-i',name,'psql','-U','rehearsal','-d','rehearsal','-v','ON_ERROR_STOP=1','-At'],input=q.encode()).decode() with tempfile.TemporaryDirectory(prefix='canina-rollout-') as d: os.chmod(d,0o700) schema=pathlib.Path(d)/'schema.sql'; history=pathlib.Path(d)/'history.sql' for file,args in [(schema,['--schema-only','--no-owner','--no-privileges']),(history,['--data-only','--table=public._prisma_migrations','--column-inserts'])]: with file.open('wb') as f: run(['docker','exec','canino_db_stage','sh','-c','exec pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" "$@"','sh',*args],stdout=f) print('Captured schema-only Stage dump plus _prisma_migrations history only') run(['docker','run','-d','--name',name,'--network','none','-e','POSTGRES_USER=rehearsal','-e','POSTGRES_DB=rehearsal','-e','POSTGRES_HOST_AUTH_METHOD=trust','postgres:16-alpine'],stdout=sp.DEVNULL) try: for _ in range(30): if sp.run(['docker','exec',name,'pg_isready','-U','rehearsal'],stdout=sp.DEVNULL,stderr=sp.DEVNULL).returncode==0: break time.sleep(1) for file in (schema,history): with file.open('rb') as f: run(['docker','exec','-i',name,'psql','-U','rehearsal','-d','rehearsal','-v','ON_ERROR_STOP=1'],stdin=f,stdout=sp.DEVNULL) before=sql("SELECT table_name,column_name,data_type,is_nullable,column_default FROM information_schema.columns WHERE table_schema='public' AND table_name <> '_prisma_migrations' ORDER BY 1,2;") tables=sql("SELECT tablename FROM pg_tables WHERE schemaname='public' AND tablename <> '_prisma_migrations' ORDER BY 1;").splitlines() for table in tables: assert sql(f'SELECT count(*) FROM "{table}";').strip()=='0',table print(f'Restored {len(tables)} legacy tables; all business tables empty') cmd=['docker','run','--rm','--network','container:'+name,'-v',str(ROOT/'backend/prisma')+':/app/prisma:ro','-v','/opt/services/canina/worktrees/orchestration-v4-core/backend/node_modules:/app/node_modules:ro','-w','/app','-e','DATABASE_URL=postgresql://rehearsal@127.0.0.1:5432/rehearsal?schema=public','node:22-bookworm','./node_modules/.bin/prisma','migrate','deploy'] run(cmd); run(cmd) query = "const {PrismaClient}=require('@prisma/client');const p=new PrismaClient();(async()=>{await p.product.findMany({take:1,include:{ingredientList:true,symptoms:true}});await p.supportConversation.findMany({take:1});await p.orchestrationTask.findMany({take:1,include:{runs:true,events:true,budgetLedger:true}});console.log('PASS: actual Prisma legacy catalog/support and v4 relation queries');})().catch(()=>{console.error('Prisma compatibility failed');process.exitCode=1}).finally(()=>p.$disconnect());" run(cmd[:-3]+['node','-e',query]) after=sql("SELECT table_name,column_name,data_type,is_nullable,column_default FROM information_schema.columns WHERE table_schema='public' AND table_name <> '_prisma_migrations' AND table_name NOT LIKE 'orchestration_%' ORDER BY 1,2;") # Compare only the exact pre-existing table set; new additive tables are not legacy. after='\n'.join(line for line in after.splitlines() if line.split('|',1)[0] in tables)+'\n' assert before==after,'Legacy column definitions changed' for table in tables: sql(f'SELECT * FROM "{table}" LIMIT 0;') v4=sql("SELECT tablename FROM pg_tables WHERE schemaname='public' AND tablename LIKE 'orchestration_%' ORDER BY 1;").splitlines() assert len(v4)==9,v4 for table in v4: sql(f'SELECT * FROM "{table}" LIMIT 0;') print(f'PASS: unchanged legacy column definitions; {len(tables)} legacy queries; {len(v4)} v4 tables; migrate deploy repeatable') finally: run(['docker','rm','-fv',name],stdout=sp.DEVNULL) print('Disposable database/container removed; no live DDL performed')