| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- # -*- coding: utf-8 -*-
- # Author : Charley
- # Python : 3.12.10
- # Date : 2026/08/18
- """读取 schema.sql 幂等建表(集物星球爬虫库)。"""
- import os
- import sys
- from loguru import logger
- from mysql_pool import MySQLConnectionPool
- def init_db(log) -> None:
- """读取同目录 schema.sql,逐条执行建表语句。
- Args:
- log: 日志对象。
- Raises:
- FileNotFoundError: schema.sql 不存在时抛出。
- """
- sql_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "schema.sql")
- if not os.path.isfile(sql_path):
- raise FileNotFoundError(f"schema.sql 不存在: {sql_path}")
- with open(sql_path, "r", encoding="utf-8") as f:
- content = f.read()
- pool = MySQLConnectionPool(log=log)
- if not pool.check_pool_health():
- log.error("数据库连接池异常")
- return
- # 按分号切分逐条执行,跳过空行与注释行
- stmts = [s.strip() for s in content.split(";") if s.strip()]
- for stmt in stmts:
- lines = [ln for ln in stmt.splitlines() if not ln.strip().startswith("--")]
- clean = "\n".join(lines).strip()
- if not clean:
- continue
- try:
- pool._execute(clean, commit=True)
- head = clean.splitlines()[0][:60]
- log.success(f"执行成功: {head}")
- except Exception as e:
- log.warning(f"执行失败(可能已存在): {e}")
- if __name__ == "__main__":
- logger.remove()
- logger.add(sys.stderr, level="INFO", format="[{time:HH:mm:ss}] {level} {message}")
- init_db(logger)
|