Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(schema): unblock pre-v121 schema replay (#2724) (#2735)

Time Attakc2fca12446889
diff --git a/src/core/pglite-engine.ts b/src/core/pglite-engine.ts
index 72b9e7412c..df686ac2fb 100644
--- a/src/core/pglite-engine.ts
+++ b/src/core/pglite-engine.ts
@@ -518,7 +518,11 @@ export class PGLiteEngine implements BrainEngine {
EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_schema='public' AND table_name='pages' AND column_name='embedding_signature') AS pages_embedding_signature_exists,
EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema='public' AND table_name='pages' AND column_name='links_extracted_at') AS pages_links_extracted_at_exists
+ WHERE table_schema='public' AND table_name='pages' AND column_name='links_extracted_at') AS pages_links_extracted_at_exists,
+ EXISTS (SELECT 1 FROM information_schema.tables
+ WHERE table_schema='public' AND table_name='timeline_entries') AS timeline_entries_exists,
+ EXISTS (SELECT 1 FROM information_schema.columns
+ WHERE table_schema='public' AND table_name='timeline_entries' AND column_name='event_page_id') AS timeline_event_page_id_exists
`);
const probe = rows[0] as {
pages_exists: boolean;
@@ -561,6 +565,8 @@ export class PGLiteEngine implements BrainEngine {
pages_generation_exists: boolean;
pages_embedding_signature_exists: boolean;
pages_links_extracted_at_exists: boolean;
+ timeline_entries_exists: boolean;
+ timeline_event_page_id_exists: boolean;
};
const needsPagesBootstrap = probe.pages_exists && !probe.source_id_exists;
@@ -637,6 +643,8 @@ export class PGLiteEngine implements BrainEngine {
// it; pre-v112 brains crash without the column, so bootstrap adds it before
// the CREATE INDEX runs. v112 runs later via runMigrations and is idempotent.
const needsPagesLinksExtractedAt = probe.pages_exists && !probe.pages_links_extracted_at_exists;
+ // v121: schema-blob indexes reference event_page_id before migrations run.
+ const needsTimelineEventPageId = probe.timeline_entries_exists && !probe.timeline_event_page_id_exists;
// Fresh installs (no tables yet) and modern brains both no-op.
if (!needsPagesBootstrap && !needsLinksBootstrap && !needsChunksBootstrap
@@ -648,7 +656,8 @@ export class PGLiteEngine implements BrainEngine {
&& !needsPagesProvenance
&& !needsContextualRetrievalColumns && !needsPagesGeneration
&& !needsPagesEmbeddingSignature
- && !needsPagesLinksExtractedAt) return;
+ && !needsPagesLinksExtractedAt
+ && !needsTimelineEventPageId) return;
process.stderr.write(' Pre-v0.21 brain detected, applying forward-reference bootstrap\n');
@@ -895,6 +904,14 @@ export class PGLiteEngine implements BrainEngine {
ALTER TABLE pages ADD COLUMN IF NOT EXISTS links_extracted_at TIMESTAMPTZ;
`);
}
+
+ if (needsTimelineEventPageId) {
+ // Add only the forward-referenced column. Migration v121 remains the
+ // source of truth for the FK and indexes and runs idempotently afterward.
+ await this.db.exec(`
+ ALTER TABLE timeline_entries ADD COLUMN IF NOT EXISTS event_page_id INTEGER;
+ `);
+ }
}
async withReservedConnection<T>(fn: (conn: ReservedConnection) => Promise<T>): Promise<T> {
diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.ts
index 19131c73ed..ce0d06e5bb 100644
--- a/src/core/postgres-engine.ts
+++ b/src/core/postgres-engine.ts
@@ -507,7 +507,11 @@ export class PostgresEngine implements BrainEngine {
EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_schema = current_schema() AND table_name = 'pages' AND column_name = 'embedding_signature') AS pages_embedding_signature_exists,
EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema = current_schema() AND table_name = 'pages' AND column_name = 'links_extracted_at') AS pages_links_extracted_at_exists
+ WHERE table_schema = current_schema() AND table_name = 'pages' AND column_name = 'links_extracted_at') AS pages_links_extracted_at_exists,
+ EXISTS (SELECT 1 FROM information_schema.tables
+ WHERE table_schema = current_schema() AND table_name = 'timeline_entries') AS timeline_entries_exists,
+ EXISTS (SELECT 1 FROM information_schema.columns
+ WHERE table_schema = current_schema() AND table_name = 'timeline_entries' AND column_name = 'event_page_id') AS timeline_event_page_id_exists
`;
const probe = probeRows[0]!;
@@ -584,6 +588,8 @@ export class PostgresEngine implements BrainEngine {
pages_generation_exists?: boolean;
pages_embedding_signature_exists?: boolean;
pages_links_extracted_at_exists?: boolean;
+ timeline_entries_exists?: boolean;
+ timeline_event_page_id_exists?: boolean;
};
const needsContextualRetrievalColumns = (probe.pages_exists
&& (!probeCr.pages_cr_mode_exists || !probeCr.pages_corpus_generation_exists))
@@ -603,6 +609,9 @@ export class PostgresEngine implements BrainEngine {
// SCHEMA_SQL replay creates the index. v112 runs later via runMigrations
// and is idempotent.
const needsPagesLinksExtractedAt = probe.pages_exists && !probeCr.pages_links_extracted_at_exists;
+ // v121: schema-blob indexes reference event_page_id before migrations run.
+ const needsTimelineEventPageId = probeCr.timeline_entries_exists === true
+ && !probeCr.timeline_event_page_id_exists;
if (!needsPagesBootstrap && !needsLinksBootstrap && !needsChunksBootstrap
&& !needsPagesDeletedAt && !needsMcpLogBootstrap && !needsSubagentProviderId
@@ -613,7 +622,8 @@ export class PostgresEngine implements BrainEngine {
&& !needsPagesProvenance
&& !needsContextualRetrievalColumns && !needsPagesGeneration
&& !needsPagesEmbeddingSignature
- && !needsPagesLinksExtractedAt) return;
+ && !needsPagesLinksExtractedAt
+ && !needsTimelineEventPageId) return;
process.stderr.write(' Pre-v0.21 brain detected, applying forward-reference bootstrap\n');
@@ -860,6 +870,14 @@ export class PostgresEngine implements BrainEngine {
ALTER TABLE pages ADD COLUMN IF NOT EXISTS links_extracted_at TIMESTAMPTZ;
`);
}
+
+ if (needsTimelineEventPageId) {
+ // Add only the forward-referenced column. Migration v121 remains the
+ // source of truth for the FK and indexes and runs idempotently afterward.
+ await conn.unsafe(`
+ ALTER TABLE timeline_entries ADD COLUMN IF NOT EXISTS event_page_id INTEGER;
+ `);
+ }
}
async transaction<T>(fn: (engine: BrainEngine) => Promise<T>): Promise<T> {
diff --git a/test/bootstrap.test.ts b/test/bootstrap.test.ts
index 11a4e2d362..0b7679fc95 100644
--- a/test/bootstrap.test.ts
+++ b/test/bootstrap.test.ts
@@ -196,4 +196,61 @@ describe('PGLiteEngine#applyForwardReferenceBootstrap', () => {
await engine.disconnect();
}
}, 30000);
+
+ test('pre-v121 timeline shape reaches LATEST through full initSchema', async () => {
+ const engine = new PGLiteEngine();
+ await engine.connect({});
+ try {
+ await engine.initSchema();
+ const db = (engine as any).db;
+ await db.exec(`
+ DROP INDEX IF EXISTS idx_timeline_event_dedup;
+ DROP INDEX IF EXISTS idx_timeline_event_page;
+ ALTER TABLE timeline_entries DROP CONSTRAINT IF EXISTS timeline_entries_event_page_id_fkey;
+ ALTER TABLE timeline_entries DROP COLUMN IF EXISTS event_page_id;
+ `);
+ await engine.setConfig('version', '119');
+
+ await engine.initSchema();
+ await engine.initSchema();
+
+ expect(await engine.getConfig('version')).toBe(String(LATEST_VERSION));
+ const { rows } = await db.query(`
+ SELECT column_name FROM information_schema.columns
+ WHERE table_name = 'timeline_entries' AND column_name = 'event_page_id'
+ `);
+ expect(rows).toHaveLength(1);
+ } finally {
+ await engine.disconnect();
+ }
+ }, 30000);
+
+ test('pre-v121 partial bootstrap resumes without skipping migration work', async () => {
+ const engine = new PGLiteEngine();
+ await engine.connect({});
+ try {
+ await engine.initSchema();
+ const db = (engine as any).db;
+ await db.exec(`
+ DROP INDEX IF EXISTS idx_timeline_event_dedup;
+ DROP INDEX IF EXISTS idx_timeline_event_page;
+ ALTER TABLE timeline_entries DROP CONSTRAINT IF EXISTS timeline_entries_event_page_id_fkey;
+ `);
+ await engine.setConfig('version', '119');
+
+ // Simulates interruption after bootstrap added the column but before
+ // schema-blob replay and migration v121 completed the FK/index work.
+ await engine.initSchema();
+
+ expect(await engine.getConfig('version')).toBe(String(LATEST_VERSION));
+ const { rows } = await db.query(`
+ SELECT to_regclass('idx_timeline_event_page') AS lookup_idx,
+ to_regclass('idx_timeline_event_dedup') AS dedup_idx
+ `);
+ expect(rows[0]?.lookup_idx).not.toBeNull();
+ expect(rows[0]?.dedup_idx).not.toBeNull();
+ } finally {
+ await engine.disconnect();
+ }
+ }, 30000);
});
diff --git a/test/schema-bootstrap-coverage.test.ts b/test/schema-bootstrap-coverage.test.ts
index 8d8caae8b7..5bda62e0d9 100644
--- a/test/schema-bootstrap-coverage.test.ts
+++ b/test/schema-bootstrap-coverage.test.ts
@@ -168,6 +168,9 @@ const REQUIRED_BOOTSTRAP_COVERAGE: ForwardReference[] = [
// SCHEMA_SQL replay creates the index. Powers `gbrain extract --stale` + the
// `links_extraction_lag` doctor check.
{ kind: 'column', table: 'pages', column: 'links_extracted_at' },
+ // v121 — referenced by the timeline event lookup and dedup indexes before
+ // the numbered migration can add the column on an existing brain.
+ { kind: 'column', table: 'timeline_entries', column: 'event_page_id' },
];
test('applyForwardReferenceBootstrap covers every forward reference declared in REQUIRED_BOOTSTRAP_COVERAGE', async () => {