- 更新前后端 sign_contract_sxsy80.pdf - 增加 com-sxsy80 说明、数据迁移与 SQL/执行脚本 - 增加 com-xsj33 数据迁移说明与 docs 下合同源文件 Made-with: Cursor
89 lines
2.5 KiB
Python
89 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
||
"""执行 com-sxsy80-data-cleanup.sql。
|
||
|
||
需安装: pip install pymysql
|
||
连接信息见 docs/com-sxsy80-data-imgration.md,通过环境变量传入,避免把密码写进仓库:
|
||
|
||
export YTYP_DB_HOST=106.14.132.80
|
||
export YTYP_DB_USER=yangtangyoupin
|
||
export YTYP_DB_PASSWORD='...'
|
||
export YTYP_DB_NAME=yangtangyoupin
|
||
python3 docs/sql/run_com_sxsy80_cleanup.py
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import pathlib
|
||
import sys
|
||
|
||
import pymysql
|
||
|
||
ROOT = pathlib.Path(__file__).resolve().parents[2]
|
||
SQL_FILE = ROOT / "docs" / "sql" / "com-sxsy80-data-cleanup.sql"
|
||
|
||
|
||
def load_statements(path: pathlib.Path) -> list[str]:
|
||
text = path.read_text(encoding="utf-8")
|
||
stmts: list[str] = []
|
||
buf: list[str] = []
|
||
for line in text.splitlines():
|
||
s = line.strip()
|
||
if s.startswith("--") or not line.strip():
|
||
continue
|
||
buf.append(line)
|
||
if ";" in line:
|
||
chunk = "\n".join(buf).strip()
|
||
if chunk:
|
||
stmts.append(chunk)
|
||
buf = []
|
||
if buf:
|
||
chunk = "\n".join(buf).strip()
|
||
if chunk:
|
||
stmts.append(chunk)
|
||
return stmts
|
||
|
||
|
||
def main() -> int:
|
||
host = os.environ.get("YTYP_DB_HOST", "106.14.132.80")
|
||
user = os.environ.get("YTYP_DB_USER", "yangtangyoupin")
|
||
password = os.environ.get("YTYP_DB_PASSWORD")
|
||
database = os.environ.get("YTYP_DB_NAME", "yangtangyoupin")
|
||
if not password:
|
||
print("请设置环境变量 YTYP_DB_PASSWORD(见脚本头部说明)", file=sys.stderr)
|
||
return 1
|
||
|
||
if not SQL_FILE.is_file():
|
||
print("missing", SQL_FILE, file=sys.stderr)
|
||
return 1
|
||
statements = load_statements(SQL_FILE)
|
||
conn = pymysql.connect(
|
||
host=host,
|
||
user=user,
|
||
password=password,
|
||
database=database,
|
||
charset="utf8mb4",
|
||
autocommit=False,
|
||
)
|
||
try:
|
||
with conn.cursor() as cur:
|
||
for i, sql in enumerate(statements, 1):
|
||
cur.execute(sql)
|
||
head = sql.split()[0].upper()
|
||
if head == "DELETE" and cur.rowcount >= 0:
|
||
print(f"[{i}] DELETE … rowcount={cur.rowcount}")
|
||
else:
|
||
print(f"[{i}] ok")
|
||
conn.commit()
|
||
print("COMMIT ok")
|
||
except Exception as e:
|
||
conn.rollback()
|
||
print("ROLLBACK:", e, file=sys.stderr)
|
||
raise
|
||
finally:
|
||
conn.close()
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|