#!/usr/bin/env python3 """Generate the compiled field contract from the verified specialist schema inventory. First run the marked synthetic database --schema-coverage-only gate, then this script, review its diff, build, and run --restore-field-omission-only. No database access here. """ import csv import json from collections import defaultdict from pathlib import Path root = Path(__file__).resolve().parents[2] source = root / "Docs/Sprints/2026-11-03-phase2-field-coverage.csv" output = root / "PlotLine/Services/ProjectBackupStoredFieldContract.Schema.cs" excluded = {"GenreMetricPresets", "GenreMetricPresetMetrics", "ProjectAccess", "UserFiles"} groups = defaultdict(list) with source.open() as stream: for row in csv.DictReader(stream): if row["Collection"] in excluded or row["Computed"] == "True": continue if row["Collection"] == "Project" and row["Column"] == "UpdatedDate": continue groups[row["Collection"]].append(row) text = """namespace PlotLine.Services; internal static partial class ProjectBackupStoredFieldContract { // Generated from the independently audited stored schema inventory. // Update alongside stored columns/backup DTOs; specialist field-omission checks use live synthetic metadata. private static readonly IReadOnlyDictionary StoredColumns= new Dictionary(StringComparer.OrdinalIgnoreCase) { """ for collection, rows in sorted(groups.items()): columns = ",".join(row["Column"] for row in rows) nullable = ",".join(row["Column"] for row in rows if row["Nullable"] == "True") text += f" [{json.dumps(collection)}]=({json.dumps(columns)},{json.dumps(nullable)}),\n" output.write_text(text + " };\n}\n") print(f"Generated {len(groups)} stored entity contracts; review the diff and run the field-omission specialist gate.")