import psycopg2
import os

# Database connection parameters
DB_PARAMS = {
    'dbname': 'bookbeach',
    'user': 'postgres',
    'password': 'F@f@k0s!!',
    'host': 'localhost'
}

def execute_sql_file(conn, file_path):
    """Execute SQL commands from a file as a single transaction"""
    try:
        with open(file_path, 'r') as f:
            sql_content = f.read()
        
        cursor = conn.cursor()
        cursor.execute(sql_content)
        conn.commit()
        cursor.close()
        
        print("Migration executed successfully!")
        return True
    except Exception as e:
        print(f"Error executing migration: {e}")
        # Rollback the transaction
        conn.rollback()
        return False

def main():
    """Test the UUID migration"""
    print("Testing UUID migration...")
    
    # Get the directory of this script
    script_dir = os.path.dirname(os.path.abspath(__file__))
    
    # Path to the migration script
    migration_file = os.path.join(script_dir, 'database', 'scripts', '05_migrate_key_tables_to_uuid_corrected.sql')
    
    print(f"Executing migration script: {migration_file}")
    
    # Connect to the database
    try:
        conn = psycopg2.connect(
            host="localhost",
            database="bookbeach",
            user="postgres",
            password="F@f@k0s!!"
        )
    except Exception as e:
        print(f"Error connecting to database: {e}")
        return
    
    # Execute the migration
    success = execute_sql_file(conn, migration_file)
    
    # Close the connection
    conn.close()
    
    if success:
        print("Migration completed successfully!")
    else:
        print("Migration failed!")

if __name__ == "__main__":
    main()