Skip to content

Fix: generateTables failing on a CREATE INDEX over a missing column - #194

Merged
gmpassos merged 1 commit into
masterfrom
fix/create-index-missing-column
Oct 28, 2026
Merged

gmpassos merged 1 commit into
masterfrom
fix/create-index-missing-column

Conversation

@gmpassos

Copy link
Copy Markdown
Contributor

Problem

An existing table missing a column that was added to the entity later, with an index over that column, failed the initialization in generateTables (seen on PostgreSQL):

CREATE INDEX IF NOT EXISTS "order_payment__expiration_date__idx" ON "order_payment" ("expiration_date") ;
[ERROR] Severity.error 42703: column "expiration_date" does not exist
...
Bad state: Error creating table SQL: ...

CREATE TABLE IF NOT EXISTS doesn't add the column, so its CREATE INDEX failed and the initialization was aborted before checkTables could report the missing column with the SQLs to fix it.

Fix

DBSQLAdapter.populateTables: when a CREATE INDEX fails, the indexed columns are checked against the existing table (getTableFieldsTypesImpl, not cached):

  • Columns missing and checkTables pending: the index is skipped with a warning. checkTables then reports the missing columns and indexes, with the fix SQLs, e.g. on PostgreSQL:
    ALTER TABLE "address" ADD COLUMN IF NOT EXISTS "city" VARCHAR(100) ;
    CREATE INDEX IF NOT EXISTS "address__state_city__idx" ON "address" ("state", "city") ;
    CREATE INDEX IF NOT EXISTS "address_city_number_custom_idx" ON "address" ("city", "number") ;
    CREATE INDEX IF NOT EXISTS "address__city__idx" ON "address" ("city") ;
  • Columns missing, without checkTables: an error naming the missing columns.
  • Any other failure: unchanged (Error creating table SQL).

New DBSQLAdapter.extractCreateIndexInSQL (table and plain columns of a CREATE INDEX; null for expressions).

Tests

  • Shared adapter test (runAdapterTests), run for PostgreSQL and MySQL (Docker), SQLite and memory: address.city and its indexes removed (an outdated table). On PostgreSQL/MySQL with generateTables + checkTables, a new provider is booted over it: generateTables passes, checkTables fails, and its suggested SQLs fix the table.
  • Without the fix, the PostgreSQL test fails with the same 42703 / Error creating table SQL as in production.
  • SQLite unit tests (bones_api_cov_sqlite_test.dart): extractCreateIndexInSQL, the skip, the error without checkTables, and the boot scenario.
  • Full suite: 2687 passed (ensure_build too, after the commit).

Version bumped to 1.28.1 (1.28.0 already tagged), with its CHANGELOG section.

🤖 Generated with Claude Code

An existing table without a column added to the entity later (not added
by `CREATE TABLE IF NOT EXISTS`) made its generated `CREATE INDEX` fail
(PostgreSQL: `42703: column "..." does not exist`), aborting the
initialization before `checkTables` could report it.

- `DBSQLAdapter.populateTables`: a failing `CREATE INDEX` over columns
  missing in its (existing) table is skipped, with a warning, when
  `checkTables` is pending: it reports the missing columns (and indexes),
  with the SQLs to fix them. Without `checkTables`: an error naming them.
- New `DBSQLAdapter.extractCreateIndexInSQL`.
- Tests: SQLite unit tests, and a shared adapter test (PostgreSQL and
  MySQL via Docker, SQLite, memory), including a boot with
  `generateTables` + `checkTables` over the outdated table.
- Version 1.28.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.70%. Comparing base (0a6d4ce) to head (c03f3b7).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #194      +/-   ##
==========================================
+ Coverage   95.62%   95.70%   +0.08%     
==========================================
  Files          75       75              
  Lines       25754    25775      +21     
==========================================
+ Hits        24627    24669      +42     
+ Misses       1127     1106      -21     
Flag Coverage Δ
unittests 95.70% <100.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gmpassos
gmpassos merged commit f0bb3b2 into master Oct 28, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant