fix(deploy): gate Stage v4 rollout on schema-first maintenance and acceptance

This commit is contained in:
devops committed 2026-10-22 19:18:50 +00:00
1 parent ac065fa911
commit 795bbb33cf
7 files changed
+179 -43

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
import subprocess as sp, tempfile, pathlib, os, time, json
ROOT=pathlib.Path('/opt/services/canina/worktrees/orchestration-v4-rollout')
name='canina-v4-rollout-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;")
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')
+11
View File
@@ -0,0 +1,11 @@
const {test}=require('node:test');
const assert=require('node:assert/strict');
const http=require('node:http');
const {accept}=require('../rollout-health.cjs');
for(const [name,status,body,pass] of [['bad status',503,'{}',false],['fake html',200,'<html>ok</html>',false],['wrong envelope',200,'{}',false],['valid empty catalog',200,'{"data":[],"meta":{"total":0}}',true]]) {
test(name,async()=>{
const s=http.createServer((q,r)=>{r.writeHead(status,{'content-type':'application/json'});r.end(body)});
await new Promise(r=>s.listen(0,'127.0.0.1',r));
try { const p=accept(`http://127.0.0.1:${s.address().port}`); if(pass) await p; else await assert.rejects(p); } finally {await new Promise(r=>s.close(r));}
});
}
+37
View File
@@ -0,0 +1,37 @@
import os, pathlib, subprocess, tempfile, unittest
ROOT = pathlib.Path(__file__).resolve().parents[2]
class RolloutTests(unittest.TestCase):
def test_non_ci_and_production_refused_before_docker(self):
script=(ROOT/'scripts/deploy.sh').read_text()
self.assertIn('CI/CD-only Stage rollout',script)
guard=script.split('# END ROLLOUT GUARD')[0]
for args,env in [(['develop'],{}),(['develop','stage-backend-only'],{'GITHUB_ACTIONS':'true'}),(['main'],{'GITHUB_ACTIONS':'true'})]:
r=subprocess.run(['bash','-c',guard,'deploy.sh',*args],cwd=ROOT,env=dict(os.environ,**env),capture_output=True,text=True)
self.assertNotEqual(r.returncode,0)
self.assertIn('refusing',r.stderr)
def test_migration_failure_never_starts_apps(self):
with tempfile.TemporaryDirectory() as d:
docker = pathlib.Path(d)/'docker'
docker.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$TRACE"\nif [[ "$*" == *"run --rm"* ]]; then exit 42; fi\n')
docker.chmod(0o755)
env = dict(os.environ, PATH=d+':'+os.environ['PATH'], TRACE=d+'/trace')
result = subprocess.run(['bash','-c', 'set -e; source scripts/rollout-stage.sh; stage_rollout; echo SUCCESS'], cwd=ROOT,env=env,capture_output=True,text=True)
self.assertNotEqual(result.returncode,0)
trace=pathlib.Path(d+'/trace').read_text() if pathlib.Path(d+'/trace').exists() else ''
self.assertIn('stop frontend_stage backend_stage',trace)
self.assertIn('run --rm --no-deps --entrypoint',trace)
self.assertNotIn('up -d',trace)
self.assertNotIn('SUCCESS',result.stdout)
def test_health_failure_stops_backend_and_does_not_publish_frontend(self):
with tempfile.TemporaryDirectory() as d:
docker = pathlib.Path(d)/'docker'
docker.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$TRACE"\nif [[ "$*" == *"exec -T backend_stage node"* ]]; then exit 43; fi\n')
docker.chmod(0o755)
result = subprocess.run(['bash','-c','set -e; source scripts/rollout-stage.sh; stage_rollout; echo SUCCESS'],cwd=ROOT,env=dict(os.environ,PATH=d+':'+os.environ['PATH'],TRACE=d+'/trace'),capture_output=True,text=True)
self.assertNotEqual(result.returncode,0)
trace=pathlib.Path(d+'/trace').read_text()
self.assertIn('stop frontend_stage backend_stage',trace)
self.assertIn('exec -T backend_stage node',trace)
self.assertNotIn('up -d --no-deps frontend_stage',trace)
self.assertNotIn('SUCCESS',result.stdout)
if __name__=='__main__': unittest.main()