From d972b3fb8a5996d1473c9cae6bca774410df3823 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 7 Oct 2025 22:04:00 +0800 Subject: [PATCH] refactor initialize_database to return MigrationResult and handle existing schema --- app/data/schema.py | 74 ++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/app/data/schema.py b/app/data/schema.py index a23af34..fe3b5f0 100644 --- a/app/data/schema.py +++ b/app/data/schema.py @@ -526,32 +526,48 @@ def _missing_tables() -> List[str]: return [name for name in REQUIRED_TABLES if name not in existing] -def initialize_database() -> None: - """Initialize the SQLite database with all required tables.""" - with db_session() as session: - cursor = session.cursor() - - # 创建表 - for statement in SCHEMA_STATEMENTS: - try: - cursor.execute(statement) - except Exception as e: # noqa: BLE001 - print(f"初始化数据库时出错: {e}") - raise - - # 添加触发器以自动更新 updated_at 字段 - try: - cursor.execute(""" - CREATE TRIGGER IF NOT EXISTS update_fetch_jobs_timestamp - AFTER UPDATE ON fetch_jobs - BEGIN - UPDATE fetch_jobs - SET updated_at = CURRENT_TIMESTAMP - WHERE id = NEW.id; - END; - """) - except Exception as e: # noqa: BLE001 - print(f"创建触发器时出错: {e}") - raise - - session.commit() +def initialize_database() -> MigrationResult: + """Initialize the SQLite database with all required tables. + + Returns a MigrationResult describing how many statements were executed + and whether the migration was skipped because the schema already exists. + """ + # 如果所有表已存在,则视为跳过 + missing = _missing_tables() + if not missing: + return MigrationResult(executed=0, skipped=True, missing_tables=list(missing)) + + executed = 0 + with db_session() as session: + cursor = session.cursor() + + # 创建表 + for statement in SCHEMA_STATEMENTS: + try: + cursor.execute(statement) + executed += 1 + except Exception as e: # noqa: BLE001 + print(f"初始化数据库时出错: {e}") + raise + + # 添加触发器以自动更新 updated_at 字段 + try: + cursor.execute(""" + CREATE TRIGGER IF NOT EXISTS update_fetch_jobs_timestamp + AFTER UPDATE ON fetch_jobs + BEGIN + UPDATE fetch_jobs + SET updated_at = CURRENT_TIMESTAMP + WHERE id = NEW.id; + END; + """) + executed += 1 + except Exception as e: # noqa: BLE001 + print(f"创建触发器时出错: {e}") + raise + + session.commit() + + # 返回执行摘要(创建后再次检查缺失表以报告) + remaining = _missing_tables() + return MigrationResult(executed=executed, skipped=False, missing_tables=remaining)