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(migrate): rebuild required indexes before the deploy gate (#323)

OKNOTOK032dde193e2b
diff --git a/server/src/__tests__/required-index-repair.test.ts b/server/src/__tests__/required-index-repair.test.ts
new file mode 100644
index 0000000..3a8e4c8
--- /dev/null
+++ b/server/src/__tests__/required-index-repair.test.ts
@@ -0,0 +1,13 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { readFile } from 'node:fs/promises'
+
+test('ensureSchema rebuilds required concurrent indexes before the promotion gate', async () => {
+ const source = await readFile(new URL('../db/migrate.ts', import.meta.url), 'utf8')
+ const ensure = source.slice(source.indexOf('export async function ensureSchema'))
+ const verify = ensure.indexOf('await verifyRequiredIndexes(client)')
+ const repair = ensure.indexOf('await buildConcurrentIndexes(client)')
+ const clientId = ensure.indexOf('await ensureMessageClientIdIndex(client)')
+ assert.ok(clientId >= 0 && clientId < verify, 'client-id index repair must run before the gate')
+ assert.ok(repair >= 0 && repair < verify, 'concurrent index repair must run before the gate')
+})
diff --git a/server/src/db/migrate.ts b/server/src/db/migrate.ts
index 81d6ed6..16dd5f9 100644
--- a/server/src/db/migrate.ts
+++ b/server/src/db/migrate.ts
@@ -2781,6 +2781,12 @@ export async function ensureSchema(): Promise<void> {
}
const finalHistory = validateMigrationHistory(await readHistory())
+ // The creators below used to live only inside migration 0001. After that
+ // version is in the ledger they never ran again, while this gate still
+ // failed the Job if a required index was missing or invalid. Both
+ // helpers are re-entrant: a valid index is a catalog read and a return.
+ await ensureMessageClientIdIndex(client)
+ await buildConcurrentIndexes(client)
await verifyRequiredIndexes(client)
console.log(`[db] schema is current at version ${finalHistory.currentVersion}`)
} finally {
@@ -2937,13 +2943,11 @@ async function buildConcurrentIndexes(client: import('pg').PoolClient): Promise<
const indexes: Array<{ name: string; create: string }> = [
{
name: 'idx_conversations_members_gin',
- // DO NOT DROP. This name is in BASELINE_REQUIRED_SCHEMA_INDEXES.
- // verifyRequiredIndexes fails the migration Job when the index is
- // missing or invalid, and this function is the only creator — it runs
- // from the legacy baseline, not on later deploys. Dropping it blocks
- // every subsequent deploy with no code path to rebuild it.
- // To retire it: remove the name from BASELINE_REQUIRED_SCHEMA_INDEXES
- // and drop the index in the same commit, in that order.
+ // This name is in BASELINE_REQUIRED_SCHEMA_INDEXES. ensureSchema calls
+ // this function before verifyRequiredIndexes on every migration Job, so
+ // a missing or invalid index is rebuilt here. To retire it: remove the
+ // name from BASELINE_REQUIRED_SCHEMA_INDEXES and drop the index in the
+ // same commit, in that order.
// `loadInbox` / `loadContext` / inbox-triage no longer read
// `members @> [agentId]`; they use `conversation_members`. That does
// not make the boot gate optional. This build's schema range also