47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
from sqlalchemy import inspect, text
|
|
|
|
from app.database import engine
|
|
|
|
|
|
def column_exists(inspector, table_name: str, column_name: str) -> bool:
|
|
return any(column["name"] == column_name for column in inspector.get_columns(table_name))
|
|
|
|
|
|
def index_exists(inspector, table_name: str, index_name: str) -> bool:
|
|
return any(index["name"] == index_name for index in inspector.get_indexes(table_name))
|
|
|
|
|
|
def main() -> None:
|
|
with engine.begin() as conn:
|
|
inspector = inspect(conn)
|
|
if not column_exists(inspector, "production_reports", "is_voided"):
|
|
conn.execute(text("""
|
|
ALTER TABLE production_reports
|
|
ADD COLUMN is_voided TINYINT(1) NOT NULL DEFAULT 0 AFTER multi_person_source_report_id
|
|
"""))
|
|
if not column_exists(inspector, "production_reports", "voided_at"):
|
|
conn.execute(text("""
|
|
ALTER TABLE production_reports
|
|
ADD COLUMN voided_at DATETIME NULL AFTER is_voided
|
|
"""))
|
|
if not column_exists(inspector, "production_reports", "voided_by"):
|
|
conn.execute(text("""
|
|
ALTER TABLE production_reports
|
|
ADD COLUMN voided_by VARCHAR(20) NULL AFTER voided_at
|
|
"""))
|
|
if not column_exists(inspector, "production_reports", "unvoid_deadline_at"):
|
|
conn.execute(text("""
|
|
ALTER TABLE production_reports
|
|
ADD COLUMN unvoid_deadline_at DATETIME NULL AFTER voided_by
|
|
"""))
|
|
inspector = inspect(conn)
|
|
if not index_exists(inspector, "production_reports", "idx_reports_voided"):
|
|
conn.execute(text("""
|
|
CREATE INDEX idx_reports_voided ON production_reports (is_voided, voided_at)
|
|
"""))
|
|
print("voided report columns migrated")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|