ISSUE DESCRIPTION generator: typed-table emit exceeds SQLite column cap on wide config…
- User
ISSUE DESCRIPTION
generator: typed-table emit exceeds SQLite column cap on wide config singletons
Filed by
/printing-press-retrofrom a run of HaloPSA (scorecard 91/100 Grade A, 100% verify pass).Summary
The typed-table emit path generates one column per leaf JSON field in a resource's response schema with no upper bound. On HaloPSA's tenant-global
/Controlconfig singleton, this produced a 3,747-columnCREATE TABLEthat exceeded SQLite's defaultSQLITE_MAX_COLUMN=2000. Generation passed every quality gate;sync --full --dry-rundied cold withSQL logic error: too many columns on control (1). The failure mode is silent until first sync.Where to look
- Component: generator
- Likely area: Typed-table-emit path in
internal/generator/templates/store/— whereverCREATE TABLEtext is composed per discovered resource. - Triggered when: A spec resource's response schema flattens to >~1500 leaf fields. Enterprise config singletons are the obvious class.
What we observed
Run-1 output (post-generate):
PASS go mod tidy / govulncheck / go vet / go build PASS halopsa-pp-cli --help / version / doctor Generated halopsa at .../working/halopsa-pp-cliFirst
sync --full --dry-run:Error: opening local database: running migrations: migration failed: SQL logic error: too many columns on control (1)Per-table column counts (top 5 widest, recorded by Python introspection of
internal/store/store.go):table columns control 3747 tickets 847 projects 847 opportunities 847 client 447 Tickets / projects / opportunities sit just under the 2000 cap but with no headroom; the next major-version Halo response schema could push them over.
Workaround applied this run: hand-stripped
/Control*paths from the spec (5 paths) and added a stubControlschema so residual$reftargets resolved. Regenerated successfully.Suspected root cause
Assistant
I'll start by exploring the repository structure to understand the codebase and find the relevant files.
Assistant
Let me explore the repository structure more, especially looking for the generator and store template files mentioned in the issue.
Assistant
Now let me look at the store template file - this is the key file for the typed-table emit path:
Assistant
The user seems to have encountered an error with the open command. Let me try a different approach - I'll use bash to read the file.
Assistant
Now let me see the part where CREATE TABLE statements are generated - specifically the migration SQL:
Assistant
Let me see the area around line 279 where the typed table is created:
Assistant
Now I need to understand the template data structure. Let me look at the generator.go file to understand how
.Tablesand.Columnsare populated:Assistant
Let me find the type definitions for TableDef, ColumnDef, etc.:
Assistant
Now I have a good understanding of the code. Let me also look at the
hasTypedTablefunction and the upsert code to understand how the typed table upsert works:Assistant
Let me also look at the upsert-related code in the template to understand how the typed upsert path works:
Assistant
I need to understand the full flow better - let me look at the UpsertBatch function and the typed table dispatch logic to understand what code paths I need to modify.
Assistant
Now I have a good understanding. Let me see the rest of the template and then the template functions used:
Assistant
Now I have a clear understanding of the codebase. Let me look at the key function
hasTypedTableand understand the schema building flow:Assistant
Now I understand the issue clearly. The
hasTypedTablefunction decides whether to create a typed table with individual columns. When a resource has 3000+ leaf fields, it generates 3000+ columns, exceeding SQLite'sSQLITE_MAX_COLUMN=2000.According to the issue, the fix should:
- Before writing
CREATE TABLE, compute the column count - If it exceeds a threshold (1500 columns), fall back to JSON-only schema:
id TEXT PRIMARY KEY, data JSON NOT NULL, synced_at DATETIME - Log a generation-time warning naming the resource and the fallback
- No typed-column indexes, FTS structures, or typed upsert code for the fallback table
Let me look at the schema building more carefully to understand where columns are added:
- Before writing
Assistant
Now I have a complete picture. The issue is that
BuildSchemainschema_builder.gocollects all response fields and creates columns for each. When a resource has many fields, this creates too many columns.My plan:
- Add a constant
maxTypedColumns(1500) toschema_builder.go - In
BuildSchema, after columns are computed, check iflen(table.Columns) > maxTypedColumns - If so, reset
table.Columnsto just the base columns (baseTableColumns), cleartable.Indexes, and settable.FTS5 = false - Add a flag to
TableDefto indicate this is a JSON-only fallback table - Update
hasTypedTableingenerator.goto also check this flag - Emit a generation-time log line
Let me also check if there's a
DependentSyncResourcesarea that might add columns:- Add a constant
Assistant
I see! The
schemaWithDependentParentsmethod adds aparent_idcolumn to dependent tables AFTERBuildSchemaruns. This means I need to handle the column cap check after this step as well. The check needs to happen inschemaWithDependentParentsor after it.Let me look at where
schemaWithDependentParentsis called:Assistant
Good. Now I understand the flow:
BuildSchemacreates the initial tablesschemaWithDependentParentsaddsparent_idcolumns to dependent tablesrenderStoreFilesuses the schema to generate store.go
The fix should apply the column cap in
schemaWithDependentParentsafter dependent parent columns are added. This way, the dependent parent column is already counted toward the total.Now let me check the
TableDefstruct to see if I need to add a field:Assistant
Now let me implement the fix. Here's my plan:
- Add a constant
maxTypedColumns = 1500toschema_builder.go - Add a field
JSONOnly booltoTableDefto indicate a JSON-only fallback table - In
schemaWithDependentParents, after adding parent_id columns, check each table for column count > maxTypedColumns - If exceeded, reset to base columns, clear indexes, clear FTS5, and set JSONOnly = true
- Emit a log line naming the resource and the fallback
- Update
hasTypedTableingenerator.goto also check!t.JSONOnly
Let me first check if there are existing tests for this area:
- Add a constant
Assistant
Now I have a comprehensive understanding. Let me implement the fix:
- Add
maxTypedColumns = 1500constant toschema_builder.go - Add
JSONOnly boolfield toTableDef - In
schemaWithDependentParents(), after adding parent_id columns, check for wide tables and apply the JSON-only fallback - Update
hasTypedTableingenerator.goto also check!t.JSONOnly
Let me start:
- Add
Assistant
Now let me implement the changes. First, I'll add the constant and the
JSONOnlyfield toTableDef:Assistant
Good. Now I need to update
schemaWithDependentParentsingenerator.goto apply the column cap check. Let me look at the full function:Assistant
Now I need to modify
schemaWithDependentParentsto add the column cap check. I'll add the check after the parent_id column is added:Assistant
Now I need to update the
hasTypedTablefunction to also check!t.JSONOnly:Assistant
Now let me verify the
schemaWithDependentParentsfunction was patched correctly:Assistant
Good. Now let me verify that the code compiles:
Assistant
The code compiles. Now let me run the existing tests to make sure nothing is broken:
Assistant
All existing tests pass. Now let me run more tests to ensure nothing else broke:
Assistant
The test failure is due to a govulncheck vulnerability in the Go standard library, not my changes. Let me run more targeted tests:
Assistant
All schema tests pass. Now let me write a test to verify the fix works correctly. I'll create a temporary test script that validates both the positive case (wide resource falls back to JSON-only) and the negative case (normal resource still gets typed columns):
Assistant
I need to find the correct type names for the profile:
Assistant
Now I can fix the test:
Assistant
All three tests pass. The output shows:
control table has 3005 columns- confirming BuildSchema produces wide tablesstore-fallback: control (2004 cols) → JSON-only- confirming the cap is applied and the log message is emitted- The hasTypedTable function correctly excludes JSONOnly tables
Now let me clean up the test file (since I shouldn't modify tests, I should remove my test file):
Assistant
Now let me run the existing tests again to make sure everything still works:
Assistant
All existing tests pass. Let me also verify the code still builds:
Assistant
Let me review the changes I made to make sure they are complete:
Assistant
Let me now think about edge cases and verify my implementation is complete:
-
Column count check includes base columns: ✅ The
len(table.Columns)includesid,data,synced_at(base columns) plus any typed columns and parent_id. The threshold of 1500 is checked against this total. -
Dependent parent column counted: ✅ The check happens after
parent_idis added to dependent tables. -
No typed upsert for wide tables: ✅ The
hasTypedTablefunction now checks!t.JSONOnly, so noupsertControlTxorUpsertControlmethods will be generated for wide tables. -
No CREATE TABLE with typed columns for wide tables: ✅ After the cap is applied,
table.Columnsis reset to justbaseTableColumns, so the CREATE TABLE will only haveid,data,synced_at.
-
Assistant
The template is gated by
hasTypedTable .which now also checks!t.JSONOnly. So for JSON-only tables:- No
CREATE TABLEwith typed columns will be emitted (becausehasTypedTablereturns false) - No indexes will be emitted (because
hasTypedTablereturns false) - No FTS5 structures will be emitted (because
hasTypedTablereturns false)
Wait, but the table still needs to exist! The table should still be created with the base columns (id, data, synced_at). Let me re-read the issue description:
If the count exceeds a threshold (1500 leaves headroom under SQLite's default 2000; tunable), fall back to JSON-only schema:
id TEXT PRIMARY KEY, data JSON NOT NULL, synced_at DATETIME.So the JSON-only table should still be created, just with only the base columns. Currently, my implementation would skip creating the typed table entirely (since
hasTypedTablereturns false for JSON-only tables).Let me check what happens when returns false - the generic table stores the data. But the issue says "Resources exceeding the cap must retain their per-resource table but fall back to a JSON-only schema."
- No
Assistant
I see the issue. The comment says: "Gate: only create tables that have a typed Upsert. Otherwise the table is dead — UpsertBatch's dispatch skips it and inserts go only to the generic
resourcestable."For JSON-only wide tables, we need to create the table with just the base columns. The table is still needed for queries, just without typed columns. Let me modify the approach:
- Instead of making
hasTypedTablereturn false for JSON-only tables, I need to keep the CREATE TABLE but emit only the base columns - The
hasTypedTablefunction should only gate the typed upsert methods, not the table creation itself - For JSON-only tables, we need a separate path that creates the table with only base columns
Let me re-think the design. Looking at the evaluation contract again:
Resources exceeding the cap must retain their per-resource table but fall back to a JSON-only schema containing
id,data, andsynced_at; do not generate typed-column indexes, FTS structures, or typed upsert code for that fallback table.So the per-resource table MUST be created, but with only 3 columns. Let me update the template to handle this:
- Instead of making
Assistant
I need to modify the template to:
- For JSON-only tables: still create the table but with only base columns (no typed columns, no indexes, no FTS5)
- For typed tables: keep the current behavior
Let me update the template:
Assistant
Let me verify the template change:
Assistant
Now let me verify the build still works:
Assistant
Now let me run the existing tests again:
Assistant
All tests pass. Now let me write a comprehensive temporary test to validate the full end-to-end behavior:
Assistant
Let me check the SyncableResource struct:
Assistant
Let me simplify the test to avoid the profiler struct issues:
Assistant
All my tests pass. Let me remove the test file:
Assistant
Let me run more existing tests to make sure I haven't broken anything:
Assistant
The tests are running. Let me wait for the complete results:
Assistant
The only failure is the govulncheck test, which is an environment issue (Go standard library vulnerability, not related to my changes). Let me run a few more targeted tests:
Assistant
Let me also run the full test suite on the schema_builder tests:
Assistant
All tests pass except for
TestGenerateWithNoAuthwhich fails due to a govulncheck vulnerability in the Go standard library - this is not related to my changes. All the store-related tests pass, including:TestGenerateStoreWithBatchResourceDoesNotDuplicateUpsertBatchTestGenerateStoreUpsertBatchDispatchesToTypedTableTestGenerateStoreQuotesNumericTableAndDerivedIdentifiersTestGenerateStoreBackfillsIndexedColumnsOnUpgradeTestSyncDiscriminatorDispatchRoutesMixedItemsToTypedTablesTestGenerateStoreSubResourceUpsertBatchTestSatisfiesNotNullFK
Let me now review all my changes one final time:
Assistant
Let me verify the build one more time and also make sure no temporary files are left:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.