Skip to content

A local-first personal finance app

Fix migration ID for “remove account type” migration (#1109)

Jed Fox6e6d76569943
diff --git a/.github/actions/check-migrations.js b/.github/actions/check-migrations.js
new file mode 100644
index 0000000000..8fc2fae46a
--- /dev/null
+++ b/.github/actions/check-migrations.js
@@ -0,0 +1,69 @@
+#!/usr/bin/env node
+
+// overview:
+// 1. List all migrations in packages/loot-core/migrations/*
+// 2. Check the commit that created the migration file
+// 3. Make sure that newer migrations were committed after older migrations (by date in the migration name)
+
+const fs = require('fs');
+const path = require('path');
+const { spawnSync } = require('child_process');
+
+// Please don’t add to this list, we just can’t change this migration ID since it already happened
+const exceptions = ['1679728867040_rules_conditions.sql'];
+
+const migrationsDir = path.join(
+ __dirname,
+ '..',
+ '..',
+ 'packages',
+ 'loot-core',
+ 'migrations',
+);
+
+const migrations = fs
+ .readdirSync(migrationsDir)
+ .filter(file => !file.startsWith('.'))
+ .map(file => {
+ const [_, date] = file.match(/^(\d+)_/) || [];
+ const { stdout } = spawnSync('git', [
+ 'log',
+ '--format=%ct',
+ '-n',
+ '1',
+ path.join(migrationsDir, file),
+ ]);
+ return {
+ migrationDate: parseInt(date),
+ commitDate: parseInt(stdout) * 1000,
+ file,
+ };
+ });
+
+const sortedMigrations = migrations.sort(
+ (a, b) => a.migrationDate - b.migrationDate,
+);
+
+let ok = true;
+for (let i = sortedMigrations.length - 1; i > 0; i--) {
+ const migration = sortedMigrations[i];
+ const prevMigration = sortedMigrations[i - 1];
+ if (migration.commitDate < prevMigration.commitDate) {
+ if (exceptions.includes(migration.file)) {
+ continue;
+ }
+ console.error(
+ `error: migration ${migration.file} was committed before ${prevMigration.file}, but it has a later date in the filename`,
+ );
+ ok = false;
+ }
+}
+
+if (ok) {
+ console.log('All migration IDs are in order');
+} else {
+ console.error(
+ '\nMigrations must be ordered by date or they will fail to apply properly',
+ );
+ process.exit(1);
+}
diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml
index 20af0f52d5..a840807b27 100644
--- a/.github/workflows/check.yml
+++ b/.github/workflows/check.yml
@@ -32,3 +32,13 @@ jobs:
uses: ./.github/actions/setup
- name: Test
run: yarn test
+
+ migrations:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v3
+ - uses: actions/setup-node@v2
+ with:
+ node-version: '19'
+ - name: Check migrations
+ run: node ./.github/actions/check-migrations.js
diff --git a/packages/loot-core/migrations/1682265217543_remove_account_type.sql b/packages/loot-core/migrations/1686139660866_remove_account_type.sql
similarity index 100%
rename from packages/loot-core/migrations/1682265217543_remove_account_type.sql
rename to packages/loot-core/migrations/1686139660866_remove_account_type.sql
diff --git a/upcoming-release-notes/1109.md b/upcoming-release-notes/1109.md
new file mode 100644
index 0000000000..7fc00e37ac
--- /dev/null
+++ b/upcoming-release-notes/1109.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [j-f1]
+---
+
+Fix ID for newly added migration