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 <noreply@anthropic.com>
This commit is contained in:
ScreenTinker 2026-04-11 22:28:10 -05:00
parent df7919f84f
commit b87904c326
2 changed files with 15 additions and 3 deletions

View file

@ -96,10 +96,12 @@ try {
} }
// Phase 2 migration: convert existing assignments into per-device playlists // Phase 2 migration: convert existing assignments into per-device playlists
const MIGRATION_ID = 'phase2_playlist_migration';
async function migrateAssignmentsToPlaylists() { async function migrateAssignmentsToPlaylists() {
// Skip if already migrated (any device has a playlist_id set) // Skip if already ran (tracked in schema_migrations table)
const migrated = db.prepare('SELECT 1 FROM devices WHERE playlist_id IS NOT NULL LIMIT 1').get(); const already = db.prepare('SELECT 1 FROM schema_migrations WHERE id = ?').get(MIGRATION_ID);
if (migrated) return; if (already) return;
const { v4: uuidv4 } = require('uuid'); const { v4: uuidv4 } = require('uuid');
const { execFile } = require('child_process'); const { execFile } = require('child_process');
@ -185,6 +187,9 @@ async function migrateAssignmentsToPlaylists() {
}); });
migrate(); 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; 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).`); console.log(`Migration complete: ${devicesWithAssignments.length} device(s), ${totalItems} playlist item(s), ${videosProbed} video(s) probed, ${scheduleCount} schedule(s).`);
} }

View file

@ -408,3 +408,10 @@ CREATE TABLE IF NOT EXISTS device_status_log (
status TEXT NOT NULL, status TEXT NOT NULL,
timestamp INTEGER NOT NULL DEFAULT (strftime('%s','now')) 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'))
);