ifixkart-backend/app/migrations/sync_schema.py

70 lines
2.9 KiB
Python

import sqlalchemy
from app.core.database.db_session import engine_commerce, engine_crm
def sync():
with engine_commerce.connect() as conn:
print("Syncing ifixkart_commerce database tables...")
# Alter service_id to be nullable
try:
conn.execute(sqlalchemy.text("ALTER TABLE service_jobs MODIFY COLUMN service_id VARCHAR(26) NULL;"))
print("Modified service_id to NULLABLE.")
except Exception as e:
print("service_id modify note:", e)
# Add missing columns to service_jobs
cols = [
("device_type", "VARCHAR(50) NULL"),
("brand_id", "VARCHAR(26) NULL"),
("series_id", "VARCHAR(26) NULL"),
("model_id", "VARCHAR(26) NULL"),
("service_type_id", "VARCHAR(26) NULL"),
("repair_service_id", "VARCHAR(26) NULL"),
("repair_variant_id", "VARCHAR(26) NULL"),
("currency", "VARCHAR(3) NOT NULL DEFAULT 'INR'"),
("service_name_snapshot", "VARCHAR(255) NULL"),
("variant_name_snapshot", "VARCHAR(255) NULL"),
("base_price_snapshot", "DECIMAL(10, 2) NULL"),
("duration_snapshot", "INT NULL"),
("warranty_snapshot", "INT NULL"),
("inspection_fee_snapshot", "DECIMAL(10, 2) NULL"),
("queue_number", "VARCHAR(50) NULL"),
("queue_date", "DATE NULL"),
("priority", "VARCHAR(20) NOT NULL DEFAULT 'NORMAL'")
]
for col_name, col_def in cols:
try:
conn.execute(sqlalchemy.text(f"ALTER TABLE service_jobs ADD COLUMN {col_name} {col_def};"))
print(f"Added column {col_name} to service_jobs.")
except Exception as e:
print(f"Column {col_name} note:", e)
# Update service_payments in ifixkart_commerce if present
pmt_cols = [
("provider_refund_id", "VARCHAR(100) NULL"),
("refund_reason", "VARCHAR(255) NULL"),
("refunded_at", "DATETIME NULL")
]
for col_name, col_def in pmt_cols:
try:
conn.execute(sqlalchemy.text(f"ALTER TABLE service_payments ADD COLUMN {col_name} {col_def};"))
print(f"Added column {col_name} to service_payments.")
except Exception as e:
print(f"Payment column {col_name} note:", e)
conn.commit()
with engine_crm.connect() as conn:
print("Syncing ifixkart_crm database tables...")
# Update technician_skills column name
try:
conn.execute(sqlalchemy.text("ALTER TABLE technician_skills CHANGE COLUMN service_id service_type_id VARCHAR(26) NOT NULL;"))
print("Updated technician_skills table column service_id -> service_type_id.")
except Exception as e:
print("TechnicianSkill column note:", e)
conn.commit()
print("Database schema sync complete!")
if __name__ == "__main__":
sync()