style: 统一 Ruff 代码格式

This commit is contained in:
yml2213
2026-08-30 21:04:52 +08:00
parent c891ac982e
commit 47e19ed7b2
90 changed files with 5574 additions and 2350 deletions
+32 -12
View File
@@ -73,10 +73,14 @@ def _normalize_value(value: object, target_column) -> object:
try:
return json.loads(value)
except json.JSONDecodeError as exc:
raise ValueError(f"{target_column.name} 存在非法 JSON{value[:120]!r}") from exc
raise ValueError(
f"{target_column.name} 存在非法 JSON{value[:120]!r}"
) from exc
def _source_rows(source_connection: Connection, source_table: Table, target_table: Table) -> Iterable[dict]:
def _source_rows(
source_connection: Connection, source_table: Table, target_table: Table
) -> Iterable[dict]:
columns = [column.name for column in target_table.columns]
missing_columns = [column for column in columns if column not in source_table.c]
if missing_columns:
@@ -88,7 +92,9 @@ def _source_rows(source_connection: Connection, source_table: Table, target_tabl
primary_key_columns = list(source_table.primary_key.columns)
statement = select(*(source_table.c[name] for name in columns))
if primary_key_columns:
statement = statement.order_by(*(column.asc() for column in primary_key_columns))
statement = statement.order_by(
*(column.asc() for column in primary_key_columns)
)
for row in source_connection.execute(statement).mappings():
yield {
@@ -101,12 +107,12 @@ def _ensure_target_is_empty(target_engine: Engine, target_tables: list[Table]) -
"""拒绝向已有业务数据的 MySQL 写入,防止误覆盖。"""
with target_engine.connect() as connection:
occupied = [
table.name
for table in target_tables
if _count_rows(connection, table) > 0
table.name for table in target_tables if _count_rows(connection, table) > 0
]
if occupied:
raise RuntimeError(f"目标 MySQL 已存在业务数据,拒绝迁移:{', '.join(occupied)}")
raise RuntimeError(
f"目标 MySQL 已存在业务数据,拒绝迁移:{', '.join(occupied)}"
)
def _upgrade_source_sqlite(source_path: Path) -> None:
@@ -115,7 +121,11 @@ def _upgrade_source_sqlite(source_path: Path) -> None:
source_env = os.environ.copy()
source_env["DATABASE_URL"] = f"sqlite:///{source_path}"
subprocess.run(
[sys.executable, "-c", "from web.backend.database import run_migrations; run_migrations()"],
[
sys.executable,
"-c",
"from web.backend.database import run_migrations; run_migrations()",
],
cwd=PROJECT_ROOT,
env=source_env,
check=True,
@@ -141,7 +151,9 @@ def main() -> int:
target_url = target_url or DATABASE_URL
if not target_url.startswith("mysql+"):
raise ValueError("目标库必须是 MySQL;可传 --target-url,或设置 DB_HOST/DB_* 环境变量")
raise ValueError(
"目标库必须是 MySQL;可传 --target-url,或设置 DB_HOST/DB_* 环境变量"
)
print("正在初始化目标 MySQL 表结构...")
run_migrations()
@@ -153,7 +165,9 @@ def main() -> int:
try:
source_table_names = set(inspect(source_engine).get_table_names())
unexpected_tables = source_table_names - target_table_names - IGNORED_SOURCE_TABLES
unexpected_tables = (
source_table_names - target_table_names - IGNORED_SOURCE_TABLES
)
if unexpected_tables:
raise RuntimeError(
"源 SQLite 存在当前程序无法识别的表:"
@@ -171,7 +185,10 @@ def main() -> int:
_ensure_target_is_empty(target_engine, tables_to_copy)
with source_engine.connect() as source_connection, target_engine.begin() as target_connection:
with (
source_engine.connect() as source_connection,
target_engine.begin() as target_connection,
):
for target_table in tables_to_copy:
source_table = source_metadata.tables[target_table.name]
source_count = _count_rows(source_connection, source_table)
@@ -184,7 +201,10 @@ def main() -> int:
for batch in _chunks(rows, args.batch_size):
target_connection.execute(target_table.insert(), batch)
with source_engine.connect() as source_connection, target_engine.connect() as target_connection:
with (
source_engine.connect() as source_connection,
target_engine.connect() as target_connection,
):
for target_table in tables_to_copy:
source_table = source_metadata.tables[target_table.name]
source_count = _count_rows(source_connection, source_table)