fix: post-migration schema verification with self-healing#488
Merged
fix: post-migration schema verification with self-healing#488
Conversation
PgBouncer transaction-mode poolers can silently swallow ALTER TABLE statements: the SQL doesn't error, but the column never gets created. The migration system increments the schema version counter anyway, so gbrain thinks it's on the latest version but the actual table is missing columns. This caused production embed failures when the embed handler tried to INSERT into columns that didn't exist. Add verifySchema() that runs after all migrations complete: 1. Parses CREATE TABLE + ALTER TABLE ADD COLUMN from schema-embedded.ts 2. Queries information_schema.columns for actual DB state 3. Diffs expected vs actual columns 4. Self-heals missing columns via ALTER TABLE ADD COLUMN IF NOT EXISTS 5. Throws with actionable diagnostics if self-heal fails Called from PostgresEngine.initSchema() after runMigrations(). PGLite skipped (in-process, no PgBouncer).
garrytan
added a commit
that referenced
this pull request
Apr 28, 2026
Merges 5 master commits since last merge: v0.22.1 autopilot fix wave (#447), v0.22.2 minions worker reliability (#458), v0.22.4 frontmatter-guard (#448), sourceId in cycle sync phase (#475), and post-migration schema verification (#488). Conflict resolutions: - VERSION: kept this branch's reserved 0.27.0 slot (master at 0.22.6). - CHANGELOG.md: kept v0.27.0 entry at top, then master's v0.22.6 → v0.21.0 entries below in order. - CLAUDE.md: merged the v0.27 cycle bullet (8 phases, synthesize, patterns, transcript-discovery, dream CLI flags) with master's v0.22.1/v0.22.5 cycle additions (signal: AbortSignal, willRunExtractPhase, resolveSourceForDir). - src/core/cycle.ts: kept v0.27 yieldDuringPhase + synthInputFile/synthDate/synthFrom/synthTo CycleOpts fields AND added master's v0.22.1 signal: AbortSignal field. Both coexist. - llms-full.txt: regenerated against the merged tree. The dream_verdicts schema migration moved v25 → v30 in the prior merge. Master ended at v29 (cathedral_ii_code_edges_rls); v30 is uncontested. Tests pass post-merge: 105/105 dream + cycle tests across 9 files. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
mgunnin
added a commit
to mgunnin/gbrain
that referenced
this pull request
Apr 28, 2026
* upstream/master: v0.22.6.1 fix: PGLite/initSchema upgrade-hardening wave (closes 2-year wedge cycle) (garrytan#440) fix: post-migration schema verification with self-healing (garrytan#488)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PgBouncer transaction-mode poolers can silently swallow ALTER TABLE statements during migrations. The SQL executes without error, but the column never materializes in the database. The migration system increments the schema version counter anyway, creating a dangerous desync: the application thinks it's on v29 but the actual table is missing columns.
This caused production embed failures when the embed handler tried to INSERT into
symbol_type,start_line,end_line,parent_symbol_pathcolumns that didn't exist on thecontent_chunkstable.Error Log
The schema version in
configread v29 (latest), butinformation_schema.columnsshowed the v26/v27 columns (symbol_type,start_line,end_line,parent_symbol_path,doc_comment,symbol_name_qualified,search_vector) were absent from the actual table. The migrations had "succeeded" — PgBouncer acknowledged every statement — but ALTER TABLE was a no-op behind the pooler.What We Tried
gbrain apply-migrations— no-op because schema version already at v29Solution
Add a
verifySchema()function (src/core/schema-verify.ts) that runs AFTER all migrations complete inPostgresEngine.initSchema(). It:schema-embedded.ts— bothCREATE TABLEblocks andALTER TABLE ADD COLUMN IF NOT EXISTSstatementsinformation_schema.columnsfor the actual database stateALTER TABLE ADD COLUMN IF NOT EXISTSwith simplified definitions (strips FKs, CHECKs, UNIQUE that can't go in ADD COLUMN)Behavior matrix
Files changed
src/core/schema-verify.ts(new) —parseExpectedColumns(),simplifyColumnDef(),verifySchema()src/core/postgres-engine.ts— callsverifySchema(this)at end ofinitSchema()src/core/db.ts— re-exportsverifySchemafor backward-compat module-level callerstest/schema-verify.test.ts(new) — 12 tests covering parser + simplifierpackage.json— version bump to 0.22.6CHANGELOG.md— release notesTesting
symbol_type,start_line,end_line,parent_symbol_path,search_vector)simplifyColumnDefcorrectly handles: REFERENCES with ON DELETE/UPDATE, nested CHECK constraints, UNIQUE, vector types, array types, TSVECTORNeed help on this PR? Tag
@codesmithwith what you need.