Skip to content

Compile cached statements, alasql.compile() and views again after a schema change in any database to close #2613 - #2620

Merged
mathiasrw merged 3 commits into
AlaSQL:developfrom
Anikesh0001:fix/global-schema-version
Oct 20, 2026
Merged

mathiasrw merged 3 commits into
AlaSQL:developfrom
Anikesh0001:fix/global-schema-version

Conversation

@Anikesh0001

Copy link
Copy Markdown
Contributor

Closes #2613

Problem

As described in #2613, compiled SQL is kept in three places. After DROP TABLE and CREATE TABLE with other columns, two of them kept serving code compiled for the old table, and the first one did too when the table is in another database. Reproduced on develop (4505394):

Case Cache on Cache off
Statement cache, table in another database [{x:1}] [{x:1, y:2}]
Statement from alasql.compile() [{x:1}] [{x:1}]
View [{x:1}] [{x:1}]

Expected everywhere: [{x:1, y:2}].

Change

Following the direction in the issue: one global alasql.schemaVersion.

What increases it:

  • Every place that already increases db.dbversion for a schema change: CREATE TABLE / CREATE VIEW, DROP TABLE / DROP VIEW (both paths), CREATE INDEX and ALTER TABLE.
  • Transaction commit and rollback, and the FileStorage rollback.
  • ATTACH and DETACH.
  • DROP DATABASE. This is not in the issue's list, but a statement cached in database a that reads b.t kept using the dropped database's table, so I included it.

What checks it:

  • Statement cache (alasql.dexec): a cached statement is used only if both dbversion and schemaVersion match. statement.schemaVersion is set where statement.dbversion is set, and in 52linq.js too.
  • alasql.compile(): returns a function that compiles the parsed statement again when schemaVersion changed since the last compile. The properties of the compiled statement are copied onto the returned function, and promise() is kept. Calls without a schema change run the same compiled code as before.
  • Views (src/420from.js): the view's SELECT is compiled again when table.selectSchemaVersion differs.

As the issue says, any schema change now makes all compiled code compile again on next use, not only the code that depends on the changed table.

DROP INDEX still does nothing (src/66dropindex.js), so it does not change the version.

Tests

test/test2613.js runs each case in new databases a (in use) and b, with the cache on and off:

  • A: statement cache, table in another database
  • B: statement from alasql.compile()
  • C: view
  • D: a statement from alasql.compile() with parameters runs correctly without a schema change (guard test)
  • E: alasql.compile(...).promise() still works (guard test)

Results:

  • bun test ./test/test2613.js against develop's src/: 2 pass (D, E), 3 fail (A, B, C).
  • bun test ./test/test2613.js with this change: 5 pass, 0 fail.
  • yarn test-only (full suite, after sh build.sh): 2320 pass, 213 skip, 0 fail. On develop (4505394) before the change: 2315 pass, 213 skip, 0 fail.
  • prettier --list-different on all changed files: clean.

I did not run yarn test-browser, so ATTACH / DETACH of IndexedDB and localStorage databases was not tested in a browser.

Note: I used Claude Code to help investigate this issue and draft the change and tests. I reviewed the result, checked that the new tests fail without the fix, and ran the checks above locally.

🤖 Generated with Claude Code

Anikesh0001 and others added 3 commits October 20, 2026 13:08
…chema change in any database to close AlaSQL#2613

Compiled SQL is kept in the statement cache, in functions returned by
alasql.compile() and in the compiled SELECT of a view. Only the statement
cache was checked, and only against the version of the database the
statement runs in. A schema change in another database, or any schema
change for alasql.compile() and views, left code compiled for the old
schema in use.

Add alasql.schemaVersion. Every place that changes db.dbversion for a
schema change increases it too, as do transaction commit and rollback,
ATTACH, DETACH and DROP DATABASE. The statement cache, functions from
alasql.compile() and views store the version they were compiled for and
compile again when it differs.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…rop the repeated dbversion bumps in ALTER TABLE
@mathiasrw

Copy link
Copy Markdown
Member

Thanks @Anikesh0001

I pushed two small commits on top. Every schema change now goes through one schemaChanged(db) helper (it still increases db.dbversion, which user code reads and increases to reset the cache), and alasql.compile() refreshes its properties after a recompile, with a test.

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.

Compiled statements are not compiled again after a schema change in another database, in alasql.compile() and in views

2 participants