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): assert the migration runner's timestamp precondition (CHOO-1809)

Louis Amaudruza99096daf7b7
diff --git a/dash/apps/switchdash-desktop/src/main/db/initialize.ts b/dash/apps/switchdash-desktop/src/main/db/initialize.ts
index f07ddd641..75d6c9077 100644
--- a/dash/apps/switchdash-desktop/src/main/db/initialize.ts
+++ b/dash/apps/switchdash-desktop/src/main/db/initialize.ts
@@ -29,16 +29,17 @@ type JournalEntry = { idx: number; when: number; tag: string; breakpoints: boole
export function orphanedMigrationTags(sqlKeys: string[], journalTags: string[]): string[] {
const known = new Set(journalTags);
return sqlKeys
- .map((key) =>
- key
- .split('/')
- .at(-1)
- ?.replace(/\.sql$/, '')
- )
+ .map(migrationTagFromKey)
.filter((tag): tag is string => !!tag && !known.has(tag))
.sort();
}
+/** A bundled SQL file's migration tag — its basename without the extension. */
+function migrationTagFromKey(key: string): string | null {
+ const base = key.split('/').at(-1);
+ return base?.endsWith('.sql') ? base.slice(0, -'.sql'.length) : null;
+}
+
function runBundledMigrations(connection: BetterSqlite3.Database): void {
const migrationLog = log.child({ component: 'db-migration' });
const startedAt = Date.now();
@@ -66,6 +67,13 @@ function runBundledMigrations(connection: BetterSqlite3.Database): void {
);
}
+ // Resolved by exact tag rather than substring: `0012_foo` would otherwise also
+ // match a bundled `0012_foo_bar.sql`, and whichever the glob happened to list
+ // first would be applied under the other's name.
+ const sqlByTag = new Map(
+ Object.keys(sqlFiles).map((key) => [migrationTagFromKey(key), key] as const)
+ );
+
const lastRow = connection
.prepare('SELECT created_at FROM __drizzle_migrations ORDER BY created_at DESC LIMIT 1')
.get() as { created_at: number } | undefined;
@@ -84,7 +92,7 @@ function runBundledMigrations(connection: BetterSqlite3.Database): void {
for (const entry of entries) {
if (entry.when <= lastTimestamp) continue;
- const sqlKey = Object.keys(sqlFiles).find((k) => k.includes(entry.tag));
+ const sqlKey = sqlByTag.get(entry.tag);
if (!sqlKey) throw new Error(`Missing bundled SQL for migration: ${entry.tag}`);
const sql = sqlFiles[sqlKey];
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
index 72550d6e8..75ea22dd6 100644
--- 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
@@ -53,11 +53,32 @@ describe('migration journal registration', () => {
it('has no duplicate tags', () => {
expect(new Set(journalTags).size).toBe(journalTags.length);
});
+
+ /**
+ * The runner skips with `entry.when <= lastTimestamp`, where `lastTimestamp`
+ * is the highest `created_at` in the ledger — not a record of which
+ * migrations ran. So an entry whose `when` is below a preceding entry's can
+ * never be applied on any machine: it is skipped on every boot, silently,
+ * while migration reports success and the table it creates is simply absent.
+ *
+ * Two branches landing migrations out of order is enough to cause it, which
+ * is the same class of merge accident that produced the 0045/0046 renumber.
+ * Keying the ledger per migration would remove the dependency entirely; until
+ * then the precondition is at least asserted rather than assumed.
+ */
+ it('orders journal entries by strictly increasing timestamp', () => {
+ const whens = journal.entries.map((entry) => entry.when);
+
+ expect(whens).toEqual([...whens].sort((a, b) => a - b));
+ expect(new Set(whens).size).toBe(whens.length);
+ });
});
describe('orphanedMigrationTags', () => {
it('reports a bundled file the journal does not list', () => {
- expect(orphanedMigrationTags(['0001_a', '0002_b'], ['0001_a'])).toEqual(['0002_b']);
+ expect(orphanedMigrationTags(['/x/0001_a.sql', '/x/0002_b.sql'], ['0001_a'])).toEqual([
+ '0002_b',
+ ]);
});
it('accepts full paths, as the bundler produces', () => {
@@ -67,4 +88,11 @@ describe('orphanedMigrationTags', () => {
it('reports nothing when every file is registered', () => {
expect(orphanedMigrationTags(['/x/0001_a.sql'], ['0001_a', '0002_unbundled'])).toEqual([]);
});
+
+ it('matches tags exactly, not by prefix', () => {
+ // `0012_foo` must not vouch for a bundled `0012_foo_bar.sql`. The runner
+ // resolves SQL the same way, where a prefix match would apply one
+ // migration's file under another's name.
+ expect(orphanedMigrationTags(['/x/0012_foo_bar.sql'], ['0012_foo'])).toEqual(['0012_foo_bar']);
+ });
});