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
Let me start by understanding the repository structure and finding the relevant code for the typed-table-emit path. The issue is about a generator that creates SQLite tables with too many columns when a resource's response schema has many leaf fields.
Let me explore the repository structure first.
Assistant
Let me look at the generator code and the store templates more
Assistant
Let me explore the generator directory structure.
Assistant
Let me look at the store-related templates in the generator:
Assistant
Let me look at the main store.go.tmpl template, which is likely where CREATE TABLE is composed.
Assistant
Let me read the template file:
Assistant
Let me look at the store.go.tmpl file, focusing on the CREATE TABLE part:
Assistant
Let me find where the store code is generated - likely in a Go file in the generator package
Assistant
Let me look at schema_builder.go, which is likely where the columns are computed:
Assistant
Now let me look at the store.go.tmpl template to understand how it renders tables
Assistant
Let me look at how BuildSchema is called and the tables are passed to the store template
Assistant
Let me look at the generator.go code around where BuildSchema is called and store data is constructed
Assistant
Let me look at the template's critical sections - the CREATE TABLE and Upsert generation
Assistant
Let me find the
hasTypedTablefunction:Assistant
Let me look at the hasTypedTable function and surrounding code in
Assistant
Let me look at more of the template to understand the full picture - especially UpsertBatch and the generic resources table
Assistant
Let me look at the UpsertBatch function to understand the generic vs typed insert paths
Assistant
Let me look at all uses of hasTypedTable in the store template:
Assistant
Let me also check sync.go.tmpl and the test template for hasTypedTable usage:
Assistant
Let me look at lines 780-810 of store.go.tmpl (typed upsert function):
Assistant
Now I have a comprehensive understanding. Let me look at the exact lines I need to change. Let me also check the sync.go.tmpl use of hasTypedTable
Assistant
Let me check the test template us
Assistant
Now I have a complete understanding. Let me implement the changes. Here's my plan:
schema_builder.go: AddmaxTypedColumns = 1500, addJSONOnlyfield toTableDef, check column count and reset wide tablesgenerator.go: Add template function to combinehasTypedTablewithJSONOnlyfor CREATE TABLE gatingstore.go.tmpl: Create per-resource table for JSON-only resources in addition to typed onessync.go.tmplandstore_upsert_batch_test.go.tmpl: No changes needed (they only gate onhasTypedTablewhich is false for JSON-only)
Let's start with `schema_builder.go
Assistant
Let me look at the exact BuildSchema flow around where table columns are built
Assistant
Now I have a clear picture. After the gravity >= 2 block (lines 77-113) and the FTS5 block (lines ~115-125), I need to check if the column count exceeds maxTypedColumns. If so, reset everything.
Let me also check the rest of BuildSchema to see where tables get appended:
Assistant
Now I have a full understanding. Let me implement the changes.
Step 1: Add
JSONOnlyfield toTableDefandmaxTypedColumnsconstant inschema_builder.goAssistant
Let me make the edits now.
First, edit schema_builder.go to add the JSONOnly field and maxTypedColumns constant.
Assistant
Let me make the edits now. First, I'll add the
JSONOnlyfield toTableDefand add themaxTypedColumnsconstantAssistant
Good. Now add the
maxTypedColumnsconstant before `baseTableColumnsAssistant
Now I need to add the column-count check and JSON-only fallback. Let me look at the area where the FTS5 block ends and the table is appended
Assistant
Now add the JSON-only fallback check and add required imports. First, the
Assistant
Let me check the current imports:
Assistant
I see, the file has been modified. Let me use the exact text from the file. Let me go to line 3 and replace the import block
Assistant
Now let me add the JSON-only fallback check. Let me find the exact lines:
Assistant
Now add the JSON-only fallback check before
tables = append(tables, table)Assistant
I see there's a duplicate
tables = append(tables, table)- line 156 needs to be removed. Let me fix that.Assistant
Good. Now I need to modify
generator.goto add a template function for the CREATE TABLE gate and modify the template.Let me look at the template function area in generator
Assistant
Now add the
hasStoreTabletemplate function afterhasTypedTablein generatorAssistant
Let me navigate to the exact line and check the content
Assistant
Let me add the
hasStoreTablefunction using exactAssistant
I see issues - there's a duplicate
hasTypedTableand a brokenpathContainsParam. Let me fix both.Assistant
Now let me modify the template. Let me look at the exact lines around 275-280:
Assistant
Now modify the template to use
hasStoreTablefor the CREATE TABLE gate:Assistant
Let me use edit with line numbers:
Assistant
Now let me verify the code compiles correctly
Assistant
Let me check the Go environment and try building:
Assistant
Let me set GOPATH and try building
Assistant
Let me set the required Go environment variables
Assistant
The build succeeds. Now let me run the existing tests for the generator package to make sure nothing is broken
Assistant
The test failure is pre-existing (govulncheck vulnerability scan, unrelated to my changes). Let me specifically run the schema builder tests:
Assistant
All schema builder tests pass. Let me now create a quick verification test to verify my changes work correctly:
Assistant
Let me create the test inside the generator package instead:
Assistant
Let me check the correct type names in the spec package:
Assistant
Let me check the Endpoint type and Response field:
Assistant
Let me look at the exact type definitions:
Assistant
Now let me fix the test file. First, let me remove the invalid test and create a proper one:
Assistant
Let me look at how existing schema builder tests construct specs:
Assistant
Let me check where BuildSchema is called and make sure I handle the dependent-parent column case:
Assistant
I need to add a re-check in
schemaWithDependentParentsfor the case where parent_id addition pushes a table over the limit. Let me look at that function:Assistant
Let me look at the full
schemaWithDependentParentsfunction:Assistant
Let me add a re-check in
schemaWithDependentParentsafter adding dependent parent columns. Let me edit that function:Assistant
There's a duplicate
return schema. Let me fix it:Assistant
Good. Let me verify the build still compiles:
Assistant
Let me check the syntax error around line 2501:
Assistant
Let me check the exact line number:
Assistant
Let me check a broader area around line 2501:
Assistant
Let me look at the broader context to understand the brace structure:
Assistant
I see - there's a missing closing brace for the
if g.profile != nilblock. Let me fix this:Assistant
This is getting messy with the brace structure. Let me reset the file and start fresh with a cleaner
Assistant
Good, let me look at the original function and apply changes carefully:
Assistant
Let me now apply changes carefully. First, add the
hasStoreTabletemplate function:Assistant
Now insert the
hasStoreTablefunction afterhasTypedTable:Assistant
Now let me add the re-check in
schemaWithDependentParents. Let me find the exact lines:Assistant
Let me get accurate line numbers:
Assistant
Now let me replace the
return schemaline with the re-checkAssistant
Now let me verify the build:
Assistant
Build succeeds. Now let me run the schema builder tests
Assistant
All schema builder tests pass. Let me run the broader generator tests (excluding the known vulncheck failure):
Assistant
All tests pass except the pre-existing govulncheck vulnerability. Let me verify the template changes are correct:
Assistant
Let me now write a proper end-to-end test to verify my changes:
Assistant
Let me just run a focused test to verify the behavior. Let me create a simple inline Go program:
Assistant
Let me now verify my changes by writing a quick inline test:
Assistant
All tests pass with correct behavior:
- Wide resource (2000 fields): Falls back to JSON-only with 3 columns ✅
- Narrow resource (50 fields): Retains typed columns ✅
- Edge case: 1500 scalar fields + 3 base = 1503 > 1500, falls back correctly ✅
- Log output:
store-fallback: control (2003 cols) → JSON-onlyis emitted
Let me clean up the temporary test file:
Assistant
Let me verify all changes are in order with a final review:
Assistant
Let me review the final diff:
Assistant
The diff looks clean and correct. Let me run a final round of tests:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.