const test = require('node:test'); const assert = require('node:assert/strict'); require('../src/common'); const { getPendingMigrations } = require('../src/db/migrations'); function createPool(responses) { const queries = []; return { queries, async query(sql, params) { queries.push([sql, params]); const text = String(sql); for (const response of responses) { if (response.match(text, params)) { return response.result; } } return [[]]; } }; } test('pending migrations are empty when the schema already matches the app version', async () => { const pool = createPool([ { match(sql) { return sql.includes('FROM information_schema.COLUMNS') && sql.includes('TABLE_NAME = ?') && sql.includes('COLUMN_NAME = ?'); }, result: [[{ column_count: 1 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_schedule_groups'; }, result: [[{ table_count: 0 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_schedule_entries'; }, result: [[{ table_count: 0 }]] } ]); const pendingMigrations = await getPendingMigrations(pool, { currentVersion: require('../package.json').version }); assert.equal(pendingMigrations.length, 0); }); test('playlist shuffle migration upgrades an existing 2.15.0 installation', async () => { const pool = createPool([]); const pending = await getPendingMigrations(pool, { currentVersion: '2.15.0' }); const shuffleMigration = pending.find(migration => migration.version === '2.15.1'); assert.ok(shuffleMigration); await shuffleMigration.run(pool); assert.ok(pool.queries.some(([sql]) => sql.includes('ADD COLUMN group_shuffle'))); }); test('playlist repeat migration upgrades an existing 2.15.1 installation', async () => { const pool = createPool([]); const pending = await getPendingMigrations(pool, { currentVersion: '2.15.1' }); const repeatMigration = pending.find(migration => migration.version === '2.15.2'); assert.ok(repeatMigration); await repeatMigration.run(pool); assert.ok(pool.queries.some(([sql]) => sql.includes('ADD COLUMN group_mode'))); assert.ok(pool.queries.some(([sql]) => sql.includes('ADD COLUMN group_repeat_count'))); }); test('canvas size enabled migration upgrades an existing 2.15.2 installation', async () => { const pool = createPool([]); const pending = await getPendingMigrations(pool, { currentVersion: '2.15.2' }); assert.equal(pending.length, 1); assert.equal(pending[0].version, '2.15.3'); await pending[0].run(pool); assert.ok(pool.queries.some(([sql]) => sql.includes('ALTER TABLE c_canvas_sizes ADD COLUMN enabled'))); }); test('playlist group schema migration is pending for version 2.14.3', async () => { const pool = createPool([ { match(sql) { return sql.includes('FROM information_schema.COLUMNS') && sql.includes('TABLE_NAME = ?') && sql.includes('COLUMN_NAME = ?'); }, result: [[{ column_count: 1 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_schedule_groups'; }, result: [[{ table_count: 0 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_schedule_entries'; }, result: [[{ table_count: 0 }]] } ]); const pendingMigrations = await getPendingMigrations(pool, { currentVersion: '2.14.3' }); assert.ok(pendingMigrations.some(function (migration) { return migration.version === '2.15.0'; })); }); test('pending migrations are reported when an older schema still needs scripts', async () => { const pool = createPool([ { match(sql) { return sql.includes('FROM information_schema.COLUMNS') && sql.includes('TABLE_NAME = ?') && sql.includes('COLUMN_NAME = ?'); }, result: [[{ column_count: 0 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_schedule_groups'; }, result: [[{ table_count: 1 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_schedule_entries'; }, result: [[{ table_count: 1 }]] } ]); const pendingMigrations = await getPendingMigrations(pool, { currentVersion: '2.6.17' }); assert.ok(pendingMigrations.length > 0); assert.equal(pendingMigrations.filter(function (migration) { return migration.version.indexOf('2.8.') === 0; }).length, 4); assert.equal(pendingMigrations.find(function (migration) { return migration.version.indexOf('2.8.') === 0; }).version, '2.8.0'); }); test('a partial timetable schema does not fast-forward migration detection', async () => { const pool = createPool([ { match(sql) { return sql.includes('FROM information_schema.COLUMNS') && sql.includes('TABLE_NAME = ?') && sql.includes('COLUMN_NAME = ?'); }, result: [[{ column_count: 0 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_timetable_groups'; }, result: [[{ table_count: 1 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_timetable_entries'; }, result: [[{ table_count: 0 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_schedule_groups'; }, result: [[{ table_count: 0 }]] }, { match(_sql, params) { return Array.isArray(params) && params[0] === 'i_schedule_entries'; }, result: [[{ table_count: 0 }]] } ]); const pendingMigrations = await getPendingMigrations(pool, { currentVersion: '0.0.0' }); assert.ok(pendingMigrations.some(function (migration) { return migration.version === '2.6.16'; })); assert.ok(pendingMigrations.some(function (migration) { return migration.version === '2.6.17'; })); assert.ok(pendingMigrations.some(function (migration) { return migration.version === '2.6.18'; })); });