import sqlite3
import os

def apply_migration():
    db_path = os.getenv('DATABASE_URL', 'instance/database.db')
    
    os.makedirs(os.path.dirname(db_path), exist_ok=True)
    
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    try:
        with open('src/config/create_messages_table.sql', 'r') as f:
            sql = f.read()
            cursor.executescript(sql)
        
        conn.commit()
        print("Migration applied successfully!")
        
    except Exception as e:
        print(f"Error applying migration: {str(e)}")
        conn.rollback()
        
    finally:
        conn.close()

if __name__ == '__main__':
    apply_migration() 