from pathlib import Path import sys from sqlalchemy import select, text ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from app.database import SessionLocal, engine # noqa: E402 from app.models import AttendancePoint # noqa: E402 from app.services.misc_work import ensure_misc_work_product # noqa: E402 def _column_exists(conn, table_name: str, column_name: str) -> bool: dialect = conn.dialect.name if dialect == "mysql": return bool(conn.execute( text(""" SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = :table_name AND column_name = :column_name """), {"table_name": table_name, "column_name": column_name}, ).scalar_one()) rows = conn.execute(text(f"PRAGMA table_info({table_name})")).all() return any(row[1] == column_name for row in rows) def main() -> None: with engine.begin() as conn: if not _column_exists(conn, "production_report_items", "remark"): conn.execute(text(""" ALTER TABLE production_report_items ADD COLUMN remark VARCHAR(500) NULL """)) with SessionLocal() as db: points = db.scalars( select(AttendancePoint).where(AttendancePoint.is_active.is_(True)) ).all() for point in points: ensure_misc_work_product(db, point.name) db.commit() print(f"misc work migrated, attendance_points={len(points)}") if __name__ == "__main__": main()