_apply_1_0_259.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Apply UpdateScripts/1.0.259.sql to aidopdev (idempotent)."""
  2. from __future__ import annotations
  3. import os
  4. import pathlib
  5. import pymysql
  6. ROOT = pathlib.Path(__file__).resolve().parents[3]
  7. SQL_PATH = ROOT / "server" / "Admin.NET.Web.Entry" / "UpdateScripts" / "1.0.259.sql"
  8. def main() -> None:
  9. sql = SQL_PATH.read_text(encoding="utf-8")
  10. lines = []
  11. for line in sql.splitlines():
  12. if line.strip().startswith("--"):
  13. continue
  14. lines.append(line)
  15. body = "\n".join(lines)
  16. stmts: list[str] = []
  17. buf: list[str] = []
  18. for line in body.splitlines():
  19. buf.append(line)
  20. if line.rstrip().endswith(";"):
  21. stmts.append("\n".join(buf))
  22. buf = []
  23. if buf:
  24. stmts.append("\n".join(buf))
  25. conn = pymysql.connect(
  26. host=os.environ.get("MDP_DB_HOST", "127.0.0.1"),
  27. user=os.environ.get("MDP_DB_USER", "root"),
  28. password=os.environ.get("MDP_DB_PASSWORD", ""),
  29. database=os.environ.get("MDP_DB_NAME", "aidopdev"),
  30. charset="utf8mb4",
  31. autocommit=True,
  32. )
  33. cur = conn.cursor()
  34. ok = 0
  35. for i, st in enumerate(stmts, 1):
  36. st = st.strip()
  37. if not st:
  38. continue
  39. try:
  40. cur.execute(st)
  41. ok += 1
  42. except Exception as e:
  43. print(f"FAIL#{i}: {e}")
  44. print(st[:300])
  45. raise
  46. print(f"OK statements={ok}")
  47. cur.execute(
  48. "SELECT COUNT(*) FROM information_schema.COLUMNS "
  49. "WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_entity' AND COLUMN_NAME='biz_key_expr'"
  50. )
  51. print("biz_key_expr", cur.fetchone())
  52. cur.execute(
  53. "SELECT COUNT(*) FROM information_schema.STATISTICS "
  54. "WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_purchase_receipt' AND INDEX_NAME='uk_source_key'"
  55. )
  56. print("uk_purchase", cur.fetchone())
  57. cur.execute(
  58. "SELECT entity_code, biz_key_expr FROM mdp_entity "
  59. "WHERE entity_code IN ("
  60. "'S5_PURCHASE_RECEIPT_DETAIL','S5_PURCHASE_RECEIPT_MASTER',"
  61. "'S7_NBR_DETAIL','S6_IPQC_INSPECTION_HEAD','S7_NBR_MASTER')"
  62. )
  63. for row in cur.fetchall():
  64. print(row)
  65. cur.close()
  66. conn.close()
  67. if __name__ == "__main__":
  68. main()