Files
actual/packages/ci-actions/bin/check-migrations.ts
Julian Dominguez-Schatz 448da13cf5 Move migrations script to typescript (#7075)
* Move migrations script to typescript

* Add release notes

* Setup

* Fix

* PR feedback

* Make imports work as expected

* Rabbit
2026-03-09 07:58:03 +00:00

61 lines
1.7 KiB
TypeScript

// overview:
// 1. Identify the migrations in packages/loot-core/migrations/* on `master` and HEAD
// 2. Make sure that any new migrations on HEAD are dated after the latest migration on `master`.
import { spawnSync } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
const migrationsDir = path.join(
path.dirname(fileURLToPath(import.meta.url)),
'..',
'..',
'..',
'packages',
'loot-core',
'migrations',
);
function readMigrations(ref: string) {
const { stdout } = spawnSync('git', [
'ls-tree',
'--name-only',
ref,
migrationsDir + '/',
]);
const files = stdout.toString().split('\n').filter(Boolean);
console.log(`Found ${files.length} migrations on ${ref}.`);
return files
.map(file => path.basename(file))
.filter(file => !file.startsWith('.'))
.map(name => ({
date: parseInt(name.split('_')[0]),
name: name.match(/^\d+_(.+?)(\.sql)?$/)?.[1] ?? '***' + name,
}));
}
spawnSync('git', ['fetch', 'origin', 'master']);
const masterMigrations = readMigrations('origin/master');
const headMigrations = readMigrations('HEAD');
const latestMasterMigration =
masterMigrations[masterMigrations.length - 1].date;
const newMigrations = headMigrations.filter(
migration => !masterMigrations.find(m => m.name === migration.name),
);
const badMigrations = newMigrations.filter(
migration => migration.date <= latestMasterMigration,
);
if (badMigrations.length) {
console.error(
`The following migrations are dated before the latest migration on master:`,
);
badMigrations.forEach(migration => {
console.error(` ${migration.name}`);
});
process.exit(1);
} else {
console.log(`All migrations are dated after the latest migration on master.`);
}