Repository navigation
Compile cached statements, alasql.compile() and views again after a schema change in any database to close #2613 - #2620
Merged
Conversation
…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
…is compiled again
Member
|
Thanks @Anikesh0001 I pushed two small commits on top. Every schema change now goes through one |
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.
Closes #2613
Problem
As described in #2613, compiled SQL is kept in three places. After
DROP TABLEandCREATE TABLEwith 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 ondevelop(4505394):[{x:1}][{x:1, y:2}]alasql.compile()[{x:1}][{x:1}][{x:1}][{x:1}]Expected everywhere:
[{x:1, y:2}].Change
Following the direction in the issue: one global
alasql.schemaVersion.What increases it:
db.dbversionfor a schema change:CREATE TABLE/CREATE VIEW,DROP TABLE/DROP VIEW(both paths),CREATE INDEXandALTER TABLE.ATTACHandDETACH.DROP DATABASE. This is not in the issue's list, but a statement cached in databaseathat readsb.tkept using the dropped database's table, so I included it.What checks it:
alasql.dexec): a cached statement is used only if bothdbversionandschemaVersionmatch.statement.schemaVersionis set wherestatement.dbversionis set, and in52linq.jstoo.alasql.compile(): returns a function that compiles the parsed statement again whenschemaVersionchanged since the last compile. The properties of the compiled statement are copied onto the returned function, andpromise()is kept. Calls without a schema change run the same compiled code as before.src/420from.js): the view's SELECT is compiled again whentable.selectSchemaVersiondiffers.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 INDEXstill does nothing (src/66dropindex.js), so it does not change the version.Tests
test/test2613.jsruns each case in new databasesa(in use) andb, with the cache on and off:alasql.compile()alasql.compile()with parameters runs correctly without a schema change (guard test)alasql.compile(...).promise()still works (guard test)Results:
bun test ./test/test2613.jsagainstdevelop'ssrc/: 2 pass (D, E), 3 fail (A, B, C).bun test ./test/test2613.jswith this change: 5 pass, 0 fail.yarn test-only(full suite, aftersh build.sh): 2320 pass, 213 skip, 0 fail. Ondevelop(4505394) before the change: 2315 pass, 213 skip, 0 fail.prettier --list-differenton 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