|
| 1 | +""" |
| 2 | +Migration script to add type and week_year columns to queue table. |
| 3 | +
|
| 4 | +This migration adds support for queuing weekly summaries alongside YouTube videos. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import sqlite3 |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +def migrate_queue_table() -> None: |
| 14 | + """Add type and week_year columns to queue table if they don't exist.""" |
| 15 | + db_path_str = os.getenv("DATABASE_PATH", "./audio_history.db") |
| 16 | + db_path = Path(db_path_str).expanduser().resolve() |
| 17 | + |
| 18 | + print(f"Migrating database: {db_path}") |
| 19 | + |
| 20 | + # Check if database exists |
| 21 | + if not db_path.exists(): |
| 22 | + print(f"Error: Database not found at {db_path}") |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | + # Create backup |
| 26 | + backup_path = db_path.with_suffix(f"{db_path.suffix}.backup-queue-columns") |
| 27 | + print(f"Creating backup: {backup_path}") |
| 28 | + import shutil |
| 29 | + |
| 30 | + shutil.copy2(db_path, backup_path) |
| 31 | + |
| 32 | + # Connect to database |
| 33 | + conn = sqlite3.connect(db_path) |
| 34 | + conn.row_factory = sqlite3.Row |
| 35 | + cursor = conn.cursor() |
| 36 | + |
| 37 | + try: |
| 38 | + # Check if columns already exist |
| 39 | + cursor.execute("PRAGMA table_info(queue)") |
| 40 | + columns = [row[1] for row in cursor.fetchall()] |
| 41 | + |
| 42 | + changes_made = False |
| 43 | + |
| 44 | + # Add type column if it doesn't exist |
| 45 | + if "type" not in columns: |
| 46 | + print("Adding 'type' column to queue table...") |
| 47 | + cursor.execute(""" |
| 48 | + ALTER TABLE queue ADD COLUMN type TEXT DEFAULT 'youtube' |
| 49 | + """) |
| 50 | + # Update existing rows to have 'youtube' type |
| 51 | + cursor.execute(""" |
| 52 | + UPDATE queue SET type = 'youtube' WHERE type IS NULL |
| 53 | + """) |
| 54 | + changes_made = True |
| 55 | + print("✓ Added 'type' column") |
| 56 | + else: |
| 57 | + print("✓ 'type' column already exists") |
| 58 | + |
| 59 | + # Add week_year column if it doesn't exist |
| 60 | + if "week_year" not in columns: |
| 61 | + print("Adding 'week_year' column to queue table...") |
| 62 | + cursor.execute(""" |
| 63 | + ALTER TABLE queue ADD COLUMN week_year TEXT |
| 64 | + """) |
| 65 | + changes_made = True |
| 66 | + print("✓ Added 'week_year' column") |
| 67 | + else: |
| 68 | + print("✓ 'week_year' column already exists") |
| 69 | + |
| 70 | + if changes_made: |
| 71 | + conn.commit() |
| 72 | + print("\n✅ Migration completed successfully!") |
| 73 | + print(f"Backup saved at: {backup_path}") |
| 74 | + else: |
| 75 | + print("\n✅ No migration needed - columns already exist") |
| 76 | + # Remove unnecessary backup |
| 77 | + backup_path.unlink() |
| 78 | + |
| 79 | + except Exception as e: |
| 80 | + print(f"\n❌ Migration failed: {e}") |
| 81 | + conn.rollback() |
| 82 | + print(f"Database has been rolled back. Backup available at: {backup_path}") |
| 83 | + sys.exit(1) |
| 84 | + finally: |
| 85 | + conn.close() |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + migrate_queue_table() |
0 commit comments