
import os
from sqlalchemy import create_engine, text
from app.config import DATABASE_URL, TABLE_PREFIX

def add_order_date_column():
    engine = create_engine(DATABASE_URL)
    table_name = f"{TABLE_PREFIX}Order_Records"
    column_name = "date"
    
    with engine.connect() as conn:
        try:
            # Check if column exists
            result = conn.execute(text(f"SHOW COLUMNS FROM {table_name} LIKE '{column_name}'"))
            if result.fetchone():
                print(f"Column {column_name} already exists in {table_name}.")
            else:
                print(f"Adding column {column_name} to {table_name}...")
                conn.execute(text(f"ALTER TABLE {table_name} ADD COLUMN {column_name} VARCHAR(20) NULL AFTER amount"))
                conn.commit()
                print("Column added successfully.")
        except Exception as e:
            print(f"Error: {e}")

if __name__ == "__main__":
    add_order_date_column()
