From 666c26bb57c146d3041780756373585d08638176 Mon Sep 17 00:00:00 2001 From: souplearn Date: Sat, 25 Jul 2026 05:53:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9B=9E=E5=A1=AB=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E6=8A=A5=E5=B7=A5=E5=BD=92=E5=B1=9E=E6=98=8E=E7=BB=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/migrate_report_allocations.py | 43 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/scripts/migrate_report_allocations.py b/scripts/migrate_report_allocations.py index 74e20fc..d872f3a 100644 --- a/scripts/migrate_report_allocations.py +++ b/scripts/migrate_report_allocations.py @@ -2,12 +2,16 @@ from pathlib import Path import re import sys -from sqlalchemy import text +from sqlalchemy import select, text +from sqlalchemy.orm import selectinload ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) -from app.database import engine # noqa: E402 +from app.database import SessionLocal, engine # noqa: E402 +from app.models import ProductionReport, WorkSession # noqa: E402 +from app.services.report_allocations import refresh_report_allocations # noqa: E402 +from app.services.work_schedule import get_work_schedule_config # noqa: E402 TABLE_NAME = "production_report_allocations" @@ -374,12 +378,45 @@ def verify_schema(conn) -> None: raise RuntimeError(f"{TABLE_NAME} missing foreign keys: {', '.join(missing_foreign_keys)}") +def _backfill() -> tuple[int, int]: + processed = 0 + skipped = 0 + + query = ( + select(ProductionReport) + .options( + selectinload(ProductionReport.items), + selectinload(ProductionReport.session).selectinload(WorkSession.devices), + ) + .order_by(ProductionReport.id.asc()) + ) + + with SessionLocal() as db: + reports = db.scalars(query).all() + for report in reports: + if report is None: + skipped += 1 + continue + + schedule = get_work_schedule_config(db, report.attendance_point_name) + refresh_report_allocations(db, report, schedule=schedule, commit=False) + processed += 1 + + if processed % 200 == 0: + db.commit() + + db.commit() + + return processed, skipped + + def main() -> None: with engine.begin() as conn: ensure_schema(conn) verify_schema(conn) - print("report allocations schema migrated") + processed, skipped = _backfill() + print(f"report allocations migrated, processed={processed}, skipped={skipped}") if __name__ == "__main__":