#!/usr/bin/env python3 """Generate the fresh MySQL 8 schema and safe seed from the verified migration catalog.""" import importlib.util import json from pathlib import Path import re from dameng_sql import mask_literals, restore_literals ROOT = Path(__file__).resolve().parents[2] spec = importlib.util.spec_from_file_location('baseline', Path(__file__).with_name('build-dameng-baseline.py')) baseline = importlib.util.module_from_spec(spec) spec.loader.exec_module(baseline) def literal(value): if value is None: return 'NULL' if isinstance(value, (int, float)): return str(value) return "'" + str(value).replace("'", "''") + "'" def main(): tables, _ = baseline.collect() target = ROOT / 'db/mysql' target.mkdir(exist_ok=True) header = "-- Fresh dedicated database only; not an upgrade or data migration.\nSET NAMES utf8mb4;\nSET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_BACKSLASH_ESCAPES';\n" ddl = [] for name, nodes in tables.items(): columns = [] for key, node in nodes.items(): if key.startswith('column:') and not re.match(r'(UNIQUE|CHECK|FOREIGN)\b', node, re.I): node = re.sub(r'^(\w+)', r'`\1`', node) masked, literals = mask_literals(node) masked = re.sub(r'\bCLOB\b', 'LONGTEXT', masked, flags=re.I) masked = re.sub(r'(?