#!/usr/bin/env python3 """Fix the source 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_source_column(): """Fix the source column type from INTEGER to VARCHAR.""" print("🔧 Fixing source 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 = 'source' """)) for row in result: print(f" Current type: {row[1]}") # Fix the column type await db.execute(text(""" ALTER TABLE listening_history ALTER COLUMN source TYPE VARCHAR(50) USING CASE WHEN source IS NOT NULL THEN 'library' ELSE NULL END """)) await db.commit() print(" ✅ Column type fixed: INTEGER → VARCHAR(50)") # 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 = 'source' """)) 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("🎉 Source column fixed successfully!") if __name__ == "__main__": asyncio.run(fix_source_column())