refactor: 引入 SQLAlchemy 多数据库抽象(PostgreSQL + SQL Server)

- 新增 orm.py:按 db_type 构建引擎(postgresql+psycopg2 / mssql+pyodbc),
  声明式 Attachment 模型,init_schema 幂等建表
- 重写 db.py 为 SQLAlchemy Core 实现(动态 Table + quote 跨库正确引用、
  id/sn 双键回查、跨库分页、先删后插幂等),对外签名不变
- 配置:config.yaml 默认 PostgreSQL,新增 config.mssql.yaml 保留 SQL Server,
  config_loader 支持可选 driver 与 db_type
- write_attachments.py 新增 --init-db
- 依赖 requirements.txt 增 sqlalchemy / psycopg2-binary(保留 pyodbc)
- README 对齐:修正目标表字段描述并新增数据库抽象层小节
This commit is contained in:
pengq
2026-07-26 11:59:55 +08:00
parent 70b6fb3767
commit a014a945af
7 changed files with 420 additions and 155 deletions

View File

@@ -49,6 +49,8 @@ from db import ( # noqa: E402
SENTINEL_MINOR,
NO_MINOR,
fetch_all_ids,
get_engine,
init_schema,
upsert_attachments,
)
@@ -154,6 +156,10 @@ def main() -> None:
"--dry-run", action="store_true",
help="只打印将要写入/跳过的统计,不连接目标表写入",
)
parser.add_argument(
"--init-db", action="store_true",
help="仅初始化目标库:幂等创建 Common.Attachment 表(含 Common schema不进行分类/写入",
)
args = parser.parse_args()
try:
@@ -169,6 +175,16 @@ def main() -> None:
)
logger = logging.getLogger("write_attachments")
# 仅初始化目标表(幂等建 Common schema + Attachment 表),然后退出
if args.init_db:
try:
init_schema(get_engine(cfg["database"]))
except DatabaseError as e:
print(f"[init-db 失败] {e}", file=sys.stderr)
sys.exit(1)
print(f"[init-db] 已完成:目标表 {ATTACHMENT_SCHEMA}.{ATTACHMENT_TABLE} 已就绪(幂等)")
return
mode = args.mode or cfg["business"]["default_mode"]
log_dir = args.log_dir or cfg["business"]["log_dir"]
# enable_other_category 是可选配置项,缺省按"关闭"--enable-other 只能从关到开