Skip to content

Commit 71fa004

Browse files
committed
- Deploy script and migration optimization to avoid too much clutter.
1 parent 9283acf commit 71fa004

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

deploy.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
# Deploy script - push to GitHub and update remote server
4+
# Usage: ./deploy.sh [commit message]
5+
6+
set -e
7+
8+
REMOTE_HOST="10.0.0.181"
9+
REMOTE_USER="pdesjardins"
10+
REMOTE_DIR="/home/pdesjardins/code/audio-stream-server"
11+
12+
# Push to GitHub
13+
echo "Pushing to GitHub..."
14+
git push origin main
15+
16+
# SSH into remote and run update script
17+
echo "Deploying to $REMOTE_HOST..."
18+
ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_DIR && ./update.sh"
19+
20+
echo ""
21+
echo "Deploy complete!"

migrate_add_audio_duration.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ def migrate_add_audio_duration():
2020
print(f"❌ Database not found at {db_path}. Nothing to migrate.")
2121
return
2222

23-
# Create backup
24-
backup_path = db_path.with_suffix(
25-
f"{db_path.suffix}.backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
26-
)
27-
shutil.copy2(db_path, backup_path)
28-
print(f"✅ Created backup: {backup_path}")
29-
3023
conn = sqlite3.connect(db_path)
3124
cursor = conn.cursor()
3225

@@ -41,6 +34,13 @@ def migrate_add_audio_duration():
4134
)
4235
return
4336

37+
# Create backup only when migration is actually needed
38+
backup_path = db_path.with_suffix(
39+
f"{db_path.suffix}.backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
40+
)
41+
shutil.copy2(db_path, backup_path)
42+
print(f"✅ Created backup: {backup_path}")
43+
4444
# Add the new column (nullable, since existing records won't have this data)
4545
print("Adding 'audio_duration_seconds' column...")
4646
cursor.execute(

migrate_add_metadata.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,27 @@ def column_exists(cursor, table_name, column_name):
3939

4040
def migrate():
4141
"""Add channel and thumbnail_url columns to tables."""
42-
backup_path = backup_database()
43-
4442
if not os.path.exists(DB_PATH):
4543
logger.info("No database to migrate. New schema will be created on first run.")
4644
return
4745

4846
conn = sqlite3.connect(DB_PATH)
4947
cursor = conn.cursor()
5048

49+
# Check if any columns are missing before creating a backup
50+
needs_migration = (
51+
not column_exists(cursor, "play_history", "channel")
52+
or not column_exists(cursor, "play_history", "thumbnail_url")
53+
or not column_exists(cursor, "queue", "channel")
54+
or not column_exists(cursor, "queue", "thumbnail_url")
55+
)
56+
if not needs_migration:
57+
logger.info("All columns already exist. No migration needed.")
58+
conn.close()
59+
return
60+
61+
backup_path = backup_database()
62+
5163
try:
5264
# Migrate play_history table
5365
if not column_exists(cursor, "play_history", "channel"):

migrate_add_weekly_summary.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ def migrate_database():
5656
print(f"✗ Database not found at {DB_PATH}")
5757
return False
5858

59-
# Create backup
60-
backup_path = backup_database()
61-
6259
try:
6360
conn = sqlite3.connect(DB_PATH)
6461

65-
# Check if migration needed
62+
# Check if migration needed before creating a backup
6663
if not check_if_migration_needed(conn):
6764
conn.close()
6865
return True
6966

67+
# Create backup only when migration is actually needed
68+
backup_path = backup_database()
69+
7070
cursor = conn.cursor()
7171

7272
# Create weekly_summaries table

update.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,21 @@ uv run python migrate_add_weekly_summary.py
7474
uv run python -c "from services.database import init_database; init_database(); print('Database schema updated successfully')"
7575

7676
echo ""
77-
echo "Step 6: Restarting service..."
77+
echo "Step 6: Cleaning up old database backups..."
78+
echo "----------------------------------------"
79+
KEEP_BACKUPS=5
80+
BACKUPS=$(ls -t audio_history.db.backup_* 2>/dev/null)
81+
BACKUP_COUNT=$(echo "$BACKUPS" | grep -c . 2>/dev/null || echo 0)
82+
if [ "$BACKUP_COUNT" -gt "$KEEP_BACKUPS" ]; then
83+
TO_DELETE=$((BACKUP_COUNT - KEEP_BACKUPS))
84+
echo "$BACKUPS" | tail -n +"$((KEEP_BACKUPS + 1))" | xargs rm -f
85+
echo "✓ Removed $TO_DELETE old backup(s), kept $KEEP_BACKUPS most recent"
86+
else
87+
echo "$BACKUP_COUNT backup(s) found, no cleanup needed"
88+
fi
89+
90+
echo ""
91+
echo "Step 7: Restarting service..."
7892
echo "----------------------------------------"
7993
if [ "$SERVICE_RUNNING" = true ]; then
8094
echo "Restarting audio-stream service..."

0 commit comments

Comments
 (0)