| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #!/usr/bin/env python3
- """Normalize generated UAT KPI business facts to dashboard factory scope 1."""
- from __future__ import annotations
- import argparse
- import json
- from apply_sql_file import connect
- TENANTS = (838257186181189, 838257212780613, 838257237606469)
- TABLES = (
- ("crm_seorder", "bill_no"),
- ("crm_seorderentry", "bill_no"),
- )
- def apply() -> dict[str, int]:
- conn = connect()
- counts: dict[str, int] = {}
- try:
- conn.begin()
- with conn.cursor() as cursor:
- placeholders = ",".join(["%s"] * len(TENANTS))
- for table, biz_column in TABLES:
- cursor.execute(
- f"""
- UPDATE `{table}`
- SET factory_id=1
- WHERE tenant_id IN ({placeholders})
- AND (`{biz_column}` LIKE 'UAT%%' OR `{biz_column}` LIKE 'DEMO-SO-%%')
- AND COALESCE(NULLIF(factory_id,0),1)<>1
- """,
- TENANTS,
- )
- counts[table] = cursor.rowcount
- conn.commit()
- except Exception:
- conn.rollback()
- raise
- finally:
- conn.close()
- return counts
- def main() -> int:
- parser = argparse.ArgumentParser()
- parser.add_argument("--apply", action="store_true")
- args = parser.parse_args()
- result = {"mode": "apply", "updated": apply()} if args.apply else {"mode": "dry-run", "tenants": TENANTS}
- print(json.dumps(result, ensure_ascii=False, indent=2))
- return 0
- if __name__ == "__main__":
- raise SystemExit(main())
|