#!/usr/bin/env python3 """Fix the completed column type bug in listening_history table.""" import asyncio import sys from pathlib import Path # Add backend to path sys.path.insert(0, str(Path(__file__).parent)) from app.core.database import get_db from sqlalchemy import text async def fix_completed_column(): """Fix the completed column type from INTEGER to BOOLEAN.""" print("🔧 Fixing completed column type...") async for db in get_db(): try: # Check current type result = await db.execute(text(""" SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'listening_history' AND column_name = 'completed' """)) for row in result: print(f" Current type: {row[1]}") # Fix the column type await db.execute(text(""" ALTER TABLE listening_history ALTER COLUMN completed TYPE BOOLEAN USING CASE WHEN completed = 1 THEN TRUE ELSE FALSE END """)) await db.commit() print(" ✅ Column type fixed: INTEGER → BOOLEAN") # Verify the fix result = await db.execute(text(""" SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'listening_history' AND column_name = 'completed' """)) for row in result: print(f" ✅ New type: {row[1]}") except Exception as e: print(f" ❌ Error: {e}") await db.rollback() raise finally: await db.close() print("🎉 Bug fixed successfully!") if __name__ == "__main__": asyncio.run(fix_completed_column())