diff --git a/scripts/production-release.py b/scripts/production-release.py index 169e4405..e56aa9ac 100644 --- a/scripts/production-release.py +++ b/scripts/production-release.py @@ -12,6 +12,11 @@ ALLOWED = {'nginx.conf', 'Dockerfile', 'backend/Dockerfile', '.dockerignore', '.gitea/workflows/deploy.yml', 'scripts/deploy.sh', 'scripts/ci-host-deploy.sh', 'scripts/rollout-production.sh', 'scripts/production-release.py', 'scripts/test_production_release.py', 'scripts/tests/test_release_schema_guard.py', + # Exact CEO-approved schema CI repair; no migration/workflow wildcard. + '.gitea/workflows/auth-schema-reconcile.yml', + 'docs/auth-schema-reconciliation.md', 'docs/auth-schema-rehearsal.json', + 'scripts/auth-schema-reconcile.sql', 'scripts/auth-schema-verify.sql', + 'scripts/ci-auth-schema-reconcile.sh', 'scripts/test_auth_schema_reconcile.py', 'scripts/compose.stage.yml', 'scripts/rollout-stage.sh', 'scripts/revalidation-runtime.py', 'scripts/provision-revalidation-env.py', 'scripts/tests/test_revalidation_runtime.py', 'scripts/tests/test_ci_host_boundary.py', 'scripts/tests/test_stage_provision_wiring.py', @@ -45,6 +50,16 @@ def validate_receipt(r, sha, baseline, paths): and sorted(r.get('paths',[])) == sorted(paths) and paths and set(paths) <= ALLOWED): raise ValueError('Exact SHA, audited Stage acceptance and narrow reviewed diff required') + # The runtime probe below checks missing columns only. Independent reviewed + # catalog evidence must cover native types and constraints; it is not inferred + # from Prisma DMMF scalar names or from this probe passing. + typed = r.get('typed_catalog_acceptance') + if not (isinstance(typed, dict) and typed.get('accepted') is True + and typed.get('sha') == sha + and typed.get('scope') == 'native-types-and-constraints' + and isinstance(typed.get('reviewer'), str) and typed['reviewer'].strip() + and re.fullmatch(r'[0-9a-f]{64}', typed.get('evidence_sha256', ''))): + raise ValueError('Independent reviewed typed catalog acceptance required') def private_json(path): p=Path(path); s=p.lstat() @@ -77,7 +92,11 @@ def snapshot(items): return c def validate_catalog(models, catalog): - """Check every persisted scalar required by the generated target client.""" + """Missing-column guard only; does not validate native types or constraints. + + A String field backed by int4 still passes here. Independent typed catalog + acceptance is mandatory in validate_receipt before production replacement. + """ if not models or not catalog: raise ValueError('Empty generated model or production catalog evidence') actual = {(r['table'], r['column']): r['type'] for r in catalog} @@ -91,11 +110,11 @@ def validate_catalog(models, catalog): if (table, column) not in actual: missing.append(table + '.' + column) if missing: - raise ValueError('Incompatible production catalog: ' + ', '.join(missing)) + raise ValueError('Missing production catalog columns: ' + ', '.join(missing)) def schema_gate(target): - """Read only: inspect immutable target client and the actual running DB catalog. + """Read-only missing-column guard, not a full schema compatibility check. No checkout/schema diff can substitute for this check. A newly generated client may require columns which were already in both source revisions. @@ -129,9 +148,11 @@ WHERE n.nspname='public' AND c.relkind IN ('r','p','v','m') AND a.attnum>0 AND N validate_catalog(models, catalog) after = read(['docker', 'inspect', 'canino_backend_prod'])[0] if after['Id'] != before['Id'] or after['Image'] != before['Image'] or not after['State']['Running']: - raise ValueError('Production runtime changed during compatibility check') + raise ValueError('Production runtime changed during missing-column check') return {'target_image': image, 'runtime_image': before['Image'], - 'runtime_container': before['Id'], 'catalog_columns': len(catalog)} + 'runtime_container': before['Id'], 'catalog_columns': len(catalog), + 'scope': 'missing-columns-only', 'native_types_checked': False, + 'constraints_checked': False} def main(): diff --git a/scripts/test_production_release.py b/scripts/test_production_release.py index 8f8ed4ac..4cbf7f60 100644 --- a/scripts/test_production_release.py +++ b/scripts/test_production_release.py @@ -9,7 +9,26 @@ SPEC.loader.exec_module(m) class ProductionGuards(unittest.TestCase): def receipt(self): - return dict(sha='a'*40, baseline='b'*40, stage_run_url='https://git.parsaaghayi.ir/parsa/canina/actions/runs/123', reviewer='CEO delegated acceptance', accepted=True, scope='reliability-only', paths=['nginx.conf']) + return dict(sha='a'*40, baseline='b'*40, stage_run_url='https://git.parsaaghayi.ir/parsa/canina/actions/runs/123', reviewer='CEO delegated acceptance', accepted=True, scope='reliability-only', paths=['nginx.conf'], typed_catalog_acceptance=dict(accepted=True, sha='a'*40, scope='native-types-and-constraints', reviewer='Independent schema reviewer', evidence_sha256='c'*64)) + + def test_exact_auth_schema_paths_from_real_git_diff_allowed_only(self): + import subprocess + paths = subprocess.check_output([ + 'git', 'diff', '--name-only', '688a0e4^', '688a0e4', '--', + '.gitea/workflows/auth-schema-reconcile.yml', + 'docs/auth-schema-reconciliation.md', 'docs/auth-schema-rehearsal.json', + 'scripts/auth-schema-reconcile.sql', 'scripts/auth-schema-verify.sql', + 'scripts/ci-auth-schema-reconcile.sh', 'scripts/test_auth_schema_reconcile.py', + ], cwd=Path(__file__).parents[1], text=True).splitlines() + self.assertEqual(len(paths), 7, paths) + r = self.receipt(); r['paths'] = paths + m.validate_receipt(r, 'a'*40, 'b'*40, paths) + for path in ['scripts/auth-schema-other.sql', 'docs/auth-schema-other.md', + '.gitea/workflows/other.yml', 'scripts/test_auth_schema_other.py']: + r['paths'] = [path] + with self.assertRaises(ValueError): + m.validate_receipt(r, 'a'*40, 'b'*40, [path]) + self.assertFalse(any('*' in path for path in m.ALLOWED)) def test_exact_six_baseline_gate_repairs_allowed_not_frontend_wildcard(self): files = { @@ -45,6 +64,20 @@ class ProductionGuards(unittest.TestCase): with self.assertRaises(ValueError): m.validate_receipt(r, 'a'*40, 'b'*40, [path]) + def test_independent_typed_catalog_acceptance_required(self): + r = self.receipt() + r.pop('typed_catalog_acceptance', None) + with self.assertRaisesRegex(ValueError, 'typed catalog'): + m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf']) + for acceptance in [ + {'accepted': False, 'sha': 'a'*40, 'evidence_sha256': 'c'*64}, + {'accepted': True, 'sha': 'd'*40, 'evidence_sha256': 'c'*64}, + {'accepted': True, 'sha': 'a'*40, 'evidence_sha256': ''}, + ]: + r['typed_catalog_acceptance'] = acceptance + with self.assertRaisesRegex(ValueError, 'typed catalog'): + m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf']) + def test_exact_stage_sha(self): r=self.receipt() m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf']) diff --git a/scripts/tests/test_release_schema_guard.py b/scripts/tests/test_release_schema_guard.py index 9dd1ebde..9ca61d5b 100644 --- a/scripts/tests/test_release_schema_guard.py +++ b/scripts/tests/test_release_schema_guard.py @@ -32,6 +32,9 @@ class SchemaGate(unittest.TestCase): with patch.object(m.subprocess,'check_output',side_effect=responses) as run: result = m.schema_gate('canina-production-backend_prod:'+'a'*40) self.assertEqual(result['target_image'], image) + self.assertEqual(result['scope'], 'missing-columns-only') + self.assertFalse(result['native_types_checked']) + self.assertFalse(result['constraints_checked']) calls = [c.args[0] for c in run.call_args_list] self.assertIn(image, calls[2]) self.assertIn('--network', calls[2]) @@ -40,6 +43,17 @@ class SchemaGate(unittest.TestCase): script = Path(__file__).parents[1].joinpath('rollout-production.sh').read_text() self.assertLess(script.index('production-release.py schema-check'), script.index('started=true')) + def test_wrong_native_type_is_outside_missing_column_guard(self): + # Deliberately document the boundary: this must not imply compatibility. + models = [{'name': 'User', 'dbName': 'users', 'fields': [ + {'name': 'id', 'kind': 'scalar', 'type': 'String', 'isList': False}]}] + m.validate_catalog(models, [{'table': 'users', 'column': 'id', 'type': 'int4'}]) + with self.assertRaisesRegex(ValueError, 'typed catalog'): + m.validate_receipt(dict(accepted=True, scope='reliability-only', + sha='a'*40, baseline='b'*40, reviewer='reviewer', + stage_run_url='https://git.parsaaghayi.ir/parsa/canina/actions/runs/123', + paths=['nginx.conf']), 'a'*40, 'b'*40, ['nginx.conf']) + def test_catalog_failure_never_falls_back_to_source_diff(self): from unittest.mock import patch import subprocess