from pathlib import Path import sys from sqlalchemy import text ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from app.database import engine # noqa: E402 def _column_exists(conn, table_name: str, column_name: str) -> bool: 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()) def main() -> None: with engine.begin() as conn: if not _column_exists(conn, "production_report_items", "operator_count"): conn.execute(text(""" ALTER TABLE production_report_items ADD COLUMN operator_count DECIMAL(10, 2) NOT NULL DEFAULT 1 AFTER stamping_method """)) if not _column_exists(conn, "production_reports", "is_multi_person_assistant"): conn.execute(text(""" ALTER TABLE production_reports ADD COLUMN is_multi_person_assistant TINYINT(1) NOT NULL DEFAULT 0 AFTER auto_submit_reason """)) if not _column_exists(conn, "production_reports", "multi_person_source_report_id"): conn.execute(text(""" ALTER TABLE production_reports ADD COLUMN multi_person_source_report_id BIGINT NULL AFTER is_multi_person_assistant """)) conn.execute(text(""" UPDATE production_report_items item JOIN products product ON product.attendance_point_name = item.attendance_point_name AND product.project_no = item.project_no AND product.product_name = item.product_name AND product.process_name = IFNULL(item.process_name, '') AND product.device_no = '' SET item.operator_count = product.operator_count WHERE item.operator_count IS NULL OR item.operator_count <= 1 """)) print("multi person report columns migrated") if __name__ == "__main__": main()