Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(switchdash): fail loud when a migration is not registered in the journal (CHOO-1809)

Louis Amaudruz67023754b7fe
diff --git a/dash/apps/switchdash-desktop/drizzle/0046_military_whirlwind.sql b/dash/apps/switchdash-desktop/drizzle/0046_military_whirlwind.sql
index 3498d04bf..42d54d0e3 100644
--- a/dash/apps/switchdash-desktop/drizzle/0046_military_whirlwind.sql
+++ b/dash/apps/switchdash-desktop/drizzle/0046_military_whirlwind.sql
@@ -1,3 +1,20 @@
+/*
+ Remote-host setup plans (CHOO-1809).
+
+ Onboarding a remote host is a sequence — check and install each prerequisite in
+ turn — and until now that sequence lived only in renderer state. Nothing
+ survived a reload, so a failure partway through left no record of what had
+ already succeeded and no way to resume.
+
+ One row per onboarded host. `steps` holds the JSON-serialised step list; it is
+ read and written whole, and validated on read rather than trusted. Purely
+ additive: no existing table is touched and nothing is backfilled, because a
+ host with no row simply has no plan yet.
+
+ Numbered 0046 after main claimed 0045; regenerating it dropped this header,
+ which is restored here. A dev machine that applied the original
+ 0045_remote_host_setup_plans already has this table.
+*/
CREATE TABLE `remote_host_setup_plans` (
`ssh_host` text PRIMARY KEY NOT NULL,
`status` text DEFAULT 'idle' NOT NULL,
diff --git a/dash/apps/switchdash-desktop/src/main/db/initialize.ts b/dash/apps/switchdash-desktop/src/main/db/initialize.ts
index 77402f752..f07ddd641 100644
--- a/dash/apps/switchdash-desktop/src/main/db/initialize.ts
+++ b/dash/apps/switchdash-desktop/src/main/db/initialize.ts
@@ -13,6 +13,32 @@ const sqlFiles = import.meta.glob('@root/drizzle/*.sql', {
type JournalEntry = { idx: number; when: number; tag: string; breakpoints: boolean };
+/**
+ * Bundled migration SQL that the journal does not list.
+ *
+ * The runner iterates the journal, so a `.sql` file missing from it is not
+ * "pending" — it is invisible, and can never be applied on any machine while the
+ * app boots reporting success with the table absent. Migration 0046 shipped in
+ * exactly that state (CHOO-1809): the SQL and its snapshot were committed and
+ * the journal entry was not, which is only detectable by looking for the
+ * mismatch, never by running the migrations.
+ *
+ * Exported for the test that locks the invariant in; the runner asserts it at
+ * boot because it is a packaging mistake, not a recoverable condition.
+ */
+export function orphanedMigrationTags(sqlKeys: string[], journalTags: string[]): string[] {
+ const known = new Set(journalTags);
+ return sqlKeys
+ .map((key) =>
+ key
+ .split('/')
+ .at(-1)
+ ?.replace(/\.sql$/, '')
+ )
+ .filter((tag): tag is string => !!tag && !known.has(tag))
+ .sort();
+}
+
function runBundledMigrations(connection: BetterSqlite3.Database): void {
const migrationLog = log.child({ component: 'db-migration' });
const startedAt = Date.now();
@@ -26,6 +52,20 @@ function runBundledMigrations(connection: BetterSqlite3.Database): void {
)
`);
+ const entries = (journal as { entries: JournalEntry[] }).entries;
+
+ const orphans = orphanedMigrationTags(
+ Object.keys(sqlFiles),
+ entries.map((entry) => entry.tag)
+ );
+ if (orphans.length) {
+ throw new Error(
+ `Migration SQL is not registered in the journal: ${orphans.join(', ')}. ` +
+ `The journal drives which migrations run, so these would never be applied. ` +
+ `Add the entry to drizzle/meta/_journal.json (or regenerate with db:generate).`
+ );
+ }
+
const lastRow = connection
.prepare('SELECT created_at FROM __drizzle_migrations ORDER BY created_at DESC LIMIT 1')
.get() as { created_at: number } | undefined;
@@ -41,7 +81,7 @@ function runBundledMigrations(connection: BetterSqlite3.Database): void {
// A failed migration is unrecoverable and cannot be reconstructed after the
// fact, so which one was being applied is recorded as it happens.
connection.transaction(() => {
- for (const entry of (journal as { entries: JournalEntry[] }).entries) {
+ for (const entry of entries) {
if (entry.when <= lastTimestamp) continue;
const sqlKey = Object.keys(sqlFiles).find((k) => k.includes(entry.tag));
diff --git a/dash/apps/switchdash-desktop/src/main/db/tests/migrations/journal-registration.test.ts b/dash/apps/switchdash-desktop/src/main/db/tests/migrations/journal-registration.test.ts
new file mode 100644
index 000000000..72550d6e8
--- /dev/null
+++ b/dash/apps/switchdash-desktop/src/main/db/tests/migrations/journal-registration.test.ts
@@ -0,0 +1,70 @@
+/**
+ * Every bundled migration must be listed in the journal (CHOO-1809).
+ *
+ * The runner iterates `_journal.json`, so a `.sql` file the journal does not
+ * list is not "pending" — it is invisible. It can never be applied on any
+ * machine, and the app boots reporting migration success with the table absent.
+ * Migration 0046 shipped in exactly that state: the SQL and its snapshot were
+ * committed, the journal entry was not, and the only symptom was `no such
+ * table: remote_host_setup_plans` at the point something tried to use it.
+ *
+ * Running the migrations cannot detect this, because the missing entry is the
+ * thing that would have driven the run. It is only visible by comparing the two
+ * sets, which is what these tests do.
+ */
+
+import { readdirSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import { describe, expect, it } from 'vitest';
+import { orphanedMigrationTags } from '@main/db/initialize';
+import journal from '@root/drizzle/meta/_journal.json';
+
+const drizzleDir = fileURLToPath(new URL('../../../../../drizzle', import.meta.url));
+
+const journalTags = journal.entries.map((entry) => entry.tag);
+const sqlTags = readdirSync(drizzleDir)
+ .filter((name) => name.endsWith('.sql'))
+ .map((name) => name.replace(/\.sql$/, ''));
+
+describe('migration journal registration', () => {
+ it('finds the migration files at all, so the checks below are not vacuous', () => {
+ expect(sqlTags.length).toBeGreaterThan(0);
+ });
+
+ it('lists every bundled .sql file in the journal', () => {
+ expect(orphanedMigrationTags(sqlTags, journalTags)).toEqual([]);
+ });
+
+ it('has a bundled .sql file for every journal entry', () => {
+ // The runner already throws on this one; asserting it here reports the whole
+ // set at once rather than whichever entry the loop reached first.
+ const missing = journalTags.filter((tag) => !sqlTags.includes(tag));
+ expect(missing).toEqual([]);
+ });
+
+ it('numbers journal entries contiguously from zero', () => {
+ // A gap means an entry was hand-edited out; a duplicate means two branches
+ // claimed the same number and the merge kept both.
+ expect(journal.entries.map((entry) => entry.idx)).toEqual(
+ journal.entries.map((_, index) => index)
+ );
+ });
+
+ it('has no duplicate tags', () => {
+ expect(new Set(journalTags).size).toBe(journalTags.length);
+ });
+});
+
+describe('orphanedMigrationTags', () => {
+ it('reports a bundled file the journal does not list', () => {
+ expect(orphanedMigrationTags(['0001_a', '0002_b'], ['0001_a'])).toEqual(['0002_b']);
+ });
+
+ it('accepts full paths, as the bundler produces', () => {
+ expect(orphanedMigrationTags(['/build/drizzle/0002_b.sql'], ['0001_a'])).toEqual(['0002_b']);
+ });
+
+ it('reports nothing when every file is registered', () => {
+ expect(orphanedMigrationTags(['/x/0001_a.sql'], ['0001_a', '0002_unbundled'])).toEqual([]);
+ });
+});