From 6e6d765699430520c3adfb8f206eef1fc258aafd Mon Sep 17 00:00:00 2001
From: Jed Fox <git@jedfox.com>
Date: Thu, 8 Jun 2023 14:52:13 -0400
Subject: [PATCH] =?UTF-8?q?Fix=20migration=20ID=20for=20=E2=80=9Cremove=20?=
 =?UTF-8?q?account=20type=E2=80=9D=20migration=20(#1109)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Good catch @Jackenmen in
https://github.com/actualbudget/actual/pull/948#issuecomment-1580501909
— I’ve also added a CI check to ensure bad migrations aren’t introduced
in the future.

I think if you have a budget that has managed to have this migration
applied successfully, you’ll need to manually patch
`getAppliedMigrations` in
`packages/loot-core/src/server/migrate/migrations.ts` and inject a query
to remove the old migration ID and insert the new one.
---
 .github/actions/check-migrations.js           | 69 +++++++++++++++++++
 .github/workflows/check.yml                   | 10 +++
 ... => 1686139660866_remove_account_type.sql} |  0
 upcoming-release-notes/1109.md                |  6 ++
 4 files changed, 85 insertions(+)
 create mode 100644 .github/actions/check-migrations.js
 rename packages/loot-core/migrations/{1682265217543_remove_account_type.sql => 1686139660866_remove_account_type.sql} (100%)
 create mode 100644 upcoming-release-notes/1109.md

diff --git a/.github/actions/check-migrations.js b/.github/actions/check-migrations.js
new file mode 100644
index 000000000..8fc2fae46
--- /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 20af0f52d..a840807b2 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 000000000..7fc00e37a
--- /dev/null
+++ b/upcoming-release-notes/1109.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [j-f1]
+---
+
+Fix ID for newly added migration
-- 
GitLab