42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
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_reports", "is_system_auto_submitted"):
|
|
conn.execute(text("""
|
|
ALTER TABLE production_reports
|
|
ADD COLUMN is_system_auto_submitted TINYINT(1) NOT NULL DEFAULT 0 AFTER submitted_at
|
|
"""))
|
|
if not _column_exists(conn, "production_reports", "auto_submit_reason"):
|
|
conn.execute(text("""
|
|
ALTER TABLE production_reports
|
|
ADD COLUMN auto_submit_reason VARCHAR(120) NULL AFTER is_system_auto_submitted
|
|
"""))
|
|
print("auto submitted report columns migrated")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|