Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(db): a table the DDL later drops is not a schema expectation (#161)

yetone8258514b429e
diff --git a/server/src/__tests__/db-schema-sentinels.test.ts b/server/src/__tests__/db-schema-sentinels.test.ts
index 6103345..a1b0d9c 100644
--- a/server/src/__tests__/db-schema-sentinels.test.ts
+++ b/server/src/__tests__/db-schema-sentinels.test.ts
@@ -49,3 +49,18 @@ test('commented-out DDL does not become a phantom expectation', () => {
assert.match(c.column, /^[a-z_][a-z0-9_]*$/)
}
})
+
+test('a table the DDL creates and later drops is not an expectation', () => {
+ const { tables, columns } = ddlExpectations()
+ // The DDL still carries `CREATE TABLE IF NOT EXISTS email_verification_tokens`
+ // followed by the migration that dropped it when password auth was removed.
+ // Deriving expectations from the CREATEs alone made it a phantom: production
+ // could never be "current", the 40P01 shortcut never fired, and the v0.13.1
+ // rollout on 2026-09-02 crash-looped its migrate init container under load.
+ for (const t of ['email_verification_tokens', 'password_reset_tokens', 'notion_pages', 'github_branches']) {
+ assert.ok(!tables.includes(t), `${t} is dropped by the DDL and must not be expected`)
+ assert.ok(!columns.some((c) => c.table === t), `no column of dropped table ${t} may be expected`)
+ }
+ // The live tables next to them are still promised.
+ for (const t of ['users', 'sessions', 'user_identities']) assert.ok(tables.includes(t), `${t} must be expected`)
+})
diff --git a/server/src/db/migrate.ts b/server/src/db/migrate.ts
index aae4223..cd8ac23 100644
--- a/server/src/db/migrate.ts
+++ b/server/src/db/migrate.ts
@@ -2203,10 +2203,30 @@ export async function releaseMigrationClient(client: MigrationClient): Promise<v
* cannot become a phantom expectation. */
export function ddlExpectations(): { tables: string[]; columns: Array<{ table: string; column: string }> } {
const sql = DDL.replace(/--[^\n]*/g, '')
+ // A table the DDL creates and LATER drops (the retired auth-token tables) is
+ // not a promise — it is history. Left in, it is a phantom expectation that can
+ // never be satisfied: `schemaAlreadyCurrent` answers "not current" on every
+ // boot, the lock-contention shortcut below never fires, and a deploy under
+ // sustained traffic crash-loops on 40P01 with nothing to actually apply.
+ // That is exactly how the v0.13.1 rollout wedged on 2026-09-02.
+ // "Later" is by position in the DDL: a DROP that precedes a CREATE of the same
+ // name is a reset, and the re-created table is still promised.
+ const lastDropAt = new Map<string, number>()
+ for (const m of sql.matchAll(/DROP\s+TABLE\s+(?:IF\s+EXISTS\s+)?([a-z_][a-z0-9_]*)/gi)) {
+ lastDropAt.set((m[1] as string).toLowerCase(), m.index ?? 0)
+ }
+ for (const m of sql.matchAll(/ALTER\s+TABLE\s+([a-z_][a-z0-9_]*)\s+DROP\s+COLUMN\s+(?:IF\s+EXISTS\s+)?([a-z_][a-z0-9_]*)/gi)) {
+ lastDropAt.set(`${(m[1] as string).toLowerCase()}.${(m[2] as string).toLowerCase()}`, m.index ?? 0)
+ }
+ const droppedAfter = (key: string, at: number) => (lastDropAt.get(key) ?? -1) > at
const tables = [...sql.matchAll(/CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+([a-z_][a-z0-9_]*)/gi)]
- .map((m) => m[1] as string)
+ .map((m) => ({ name: (m[1] as string).toLowerCase(), at: m.index ?? 0 }))
+ .filter((t) => !droppedAfter(t.name, t.at))
+ .map((t) => t.name)
const columns = [...sql.matchAll(/ALTER\s+TABLE\s+([a-z_][a-z0-9_]*)\s+ADD\s+COLUMN\s+IF\s+NOT\s+EXISTS\s+([a-z_][a-z0-9_]*)/gi)]
- .map((m) => ({ table: m[1] as string, column: m[2] as string }))
+ .map((m) => ({ table: (m[1] as string).toLowerCase(), column: (m[2] as string).toLowerCase(), at: m.index ?? 0 }))
+ .filter((c) => !droppedAfter(c.table, c.at) && !droppedAfter(`${c.table}.${c.column}`, c.at))
+ .map((c) => ({ table: c.table, column: c.column }))
return { tables: [...new Set(tables)], columns }
}