Skip to content

Commit 30f8b62

Browse files
committed
Migration script
1 parent 5692c38 commit 30f8b62

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

CLAUDE.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ CREATE TABLE queue (
286286
channel TEXT, -- Channel/uploader name
287287
thumbnail_url TEXT, -- Video thumbnail URL (best quality)
288288
position INTEGER NOT NULL, -- Order in queue
289-
created_at TEXT NOT NULL
289+
created_at TEXT NOT NULL,
290+
type TEXT DEFAULT 'youtube', -- Type of item (youtube or summary)
291+
week_year TEXT -- Week identifier for summary items
290292
);
291293
```
292294

@@ -583,7 +585,7 @@ This script will:
583585
2. Pull latest changes from main branch
584586
3. Update Python dependencies via `uv sync`
585587
4. Check and install missing system dependencies
586-
5. Run database migrations (migrate_database.py)
588+
5. Run database migrations (migrate_database.py, migrate_add_metadata.py, migrate_add_queue_columns.py)
587589
6. Update database schema (`services.database.init_database`)
588590
7. Restart the systemd service if it was running
589591

@@ -607,13 +609,20 @@ The project includes multiple migration scripts for schema updates:
607609
- Creates backup before migration
608610
- Required for MediaSession API integration (car displays, lock screens)
609611

612+
**migrate_add_queue_columns.py** - Queue enhancement migration:
613+
- Adds `type` column to queue table (default: 'youtube')
614+
- Adds `week_year` column to queue table (for weekly summary items)
615+
- Creates backup before migration
616+
- Required for queuing weekly summaries alongside YouTube videos
617+
610618
**Manual migration**:
611619
```sh
612620
uv run python migrate_database.py
613621
uv run python migrate_add_metadata.py
622+
uv run python migrate_add_queue_columns.py
614623
```
615624

616-
Both migrations run automatically during `./update.sh`.
625+
All migrations run automatically during `./update.sh`.
617626

618627
### Trilium Notes Title Migration
619628

migrate_add_queue_columns.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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()

update.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ echo "----------------------------------------"
6060
# Run migrations (handles schema changes for existing databases)
6161
uv run python migrate_database.py
6262
uv run python migrate_add_metadata.py
63+
uv run python migrate_add_queue_columns.py
6364

6465
# Then initialize/update schema (creates tables if they don't exist)
6566
uv run python -c "from services.database import init_database; init_database(); print('Database schema updated successfully')"

0 commit comments

Comments
 (0)