init_db.py 999 B

123456789101112131415161718192021222324252627282930
  1. # -*- coding: utf-8 -*-
  2. # Author : Charley
  3. # Python : 3.12.10
  4. # Date : 2026/08/02
  5. """执行 schema.sql 建表(幂等,CREATE TABLE IF NOT EXISTS)。"""
  6. from loguru import logger
  7. from mysql_pool import MySQLConnectionPool
  8. def main():
  9. """读取 schema.sql 并逐条执行建表语句,最后打印已建表清单。"""
  10. with open("schema.sql", "r", encoding="utf-8") as f:
  11. raw = f.read()
  12. # 去掉整行注释,再按分号切分为独立语句
  13. lines = [ln for ln in raw.splitlines() if not ln.strip().startswith("--")]
  14. stmts = [s.strip() for s in "\n".join(lines).split(";") if s.strip()]
  15. pool = MySQLConnectionPool(log=logger)
  16. for stmt in stmts:
  17. pool._execute(stmt, commit=True)
  18. head = stmt.split("(")[0].strip().replace("\n", " ")
  19. logger.info(f"执行成功: {head}")
  20. rows = pool.select_all("SHOW TABLES LIKE 'deca\\_%'")
  21. logger.info(f"当前 deca 表: {[r[0] for r in rows]}")
  22. if __name__ == "__main__":
  23. main()