From b87904c3266c32e8a72b308ce494703ff37b22ba Mon Sep 17 00:00:00 2001 From: ScreenTinker Date: Sat, 11 Apr 2026 22:28:10 -0500 Subject: [PATCH] Add schema_migrations table for run-once migration tracking New schema_migrations table (id TEXT PK, ran_at INTEGER) tracks which one-time migrations have executed. The Phase 2 playlist migration now checks for 'phase2_playlist_migration' in this table instead of inferring state from devices.playlist_id. Records the migration ID after successful completion. Eliminates ffprobe overhead on subsequent startups. Co-Authored-By: Claude Opus 4.6 --- server/db/database.js | 11 ++++++++--- server/db/schema.sql | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/server/db/database.js b/server/db/database.js index 752c8af..f677cfe 100644 --- a/server/db/database.js +++ b/server/db/database.js @@ -96,10 +96,12 @@ try { } // Phase 2 migration: convert existing assignments into per-device playlists +const MIGRATION_ID = 'phase2_playlist_migration'; + async function migrateAssignmentsToPlaylists() { - // Skip if already migrated (any device has a playlist_id set) - const migrated = db.prepare('SELECT 1 FROM devices WHERE playlist_id IS NOT NULL LIMIT 1').get(); - if (migrated) return; + // Skip if already ran (tracked in schema_migrations table) + const already = db.prepare('SELECT 1 FROM schema_migrations WHERE id = ?').get(MIGRATION_ID); + if (already) return; const { v4: uuidv4 } = require('uuid'); const { execFile } = require('child_process'); @@ -185,6 +187,9 @@ async function migrateAssignmentsToPlaylists() { }); migrate(); + // Record that this migration has run + db.prepare('INSERT OR IGNORE INTO schema_migrations (id) VALUES (?)').run(MIGRATION_ID); + const scheduleCount = db.prepare('SELECT COUNT(*) as count FROM schedules').get().count; console.log(`Migration complete: ${devicesWithAssignments.length} device(s), ${totalItems} playlist item(s), ${videosProbed} video(s) probed, ${scheduleCount} schedule(s).`); } diff --git a/server/db/schema.sql b/server/db/schema.sql index 229a987..3e44f50 100644 --- a/server/db/schema.sql +++ b/server/db/schema.sql @@ -408,3 +408,10 @@ CREATE TABLE IF NOT EXISTS device_status_log ( status TEXT NOT NULL, timestamp INTEGER NOT NULL DEFAULT (strftime('%s','now')) ); + +-- ===================== SCHEMA MIGRATIONS ===================== + +CREATE TABLE IF NOT EXISTS schema_migrations ( + id TEXT PRIMARY KEY, + ran_at INTEGER NOT NULL DEFAULT (strftime('%s','now')) +);