Repository navigation
Fix UNIQUE/PRIMARY KEY silently ignored on FILESTORAGE and LOCALSTORAGE tables - #2977
Open
shaktimaan00 wants to merge 1 commit into
Open
shaktimaan00 wants to merge 1 commit into
shaktimaan00 wants to merge 1 commit into
Conversation
CREATE TABLE returned early for engine-backed tables (FILESTORAGE, LOCALSTORAGE) before ever setting table.insert(), the function that checks PRIMARY KEY and UNIQUE before a row goes in. INSERT then fell back to a raw concat, so constrained columns on these tables accepted duplicates silently. Move the engine.createTable() call to after table.insert() is wired up, and have INSERT only take the engine's fast append path when the table actually has no PK/UNIQUE to check. LOCALSTORAGE has its own wrinkle: every reload rebuilt the table from scratch via 'new alasql.Table()', dropping the live table.insert closure along with it. Reuse the existing table instance when one is already there, the same way the FILESTORAGE engine already does. Fixes AlaSQL#1148
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.
Fixes #1148.
What was wrong
CREATE TABLEon an engine-backed table (FILESTORAGE, LOCALSTORAGE) returned early by calling the engine'screateTable(), before the code further down that setstable.insert()— the function that checks PRIMARY KEY / UNIQUE before a row goes in. So those tables never gottable.insert()at all.INSERTfalls back to a rawdata.concat()when a table has noinsert(), which is also what the storage engines' own fast path (intoTable) expects. Net effect: UNIQUE and PRIMARY KEY were checked in memory but silently skipped as soon as a table lived in FILESTORAGE or LOCALSTORAGE.Repro from the issue:
All four rows went in; the fourth should have been rejected (duplicate on
id... originally reported as the third row id=3 colliding, same bug either way).Fix
src/60createtable.js: callengine.createTable()aftertable.insert()is wired up, not before, so engine-backed tables get it too.src/70insert.js: only take the engine's fastintoTable()append path when the table has no PK/UNIQUE to check. Tables with a constraint go throughtable.insert()instead, same as an in-memory table.src/92localstorage.js:LS.restoreTable()rebuilt the table object from scratch on every reload, which would have thrown away the livetable.insert()closure right after CREATE TABLE set it. It now keeps the existing table instance when there is one, the same way the FILESTORAGE engine already does inFS.loadTableData().Testing
Added
test/test1148.js: UNIQUE and PRIMARY KEY duplicate-rejection on FILESTORAGE, and UNIQUE duplicate-rejection on LOCALSTORAGE (plus a plain-table insert alongside it, to make sure the fast append path for unconstrained tables still works).Ran the full suite (
bun test) before and after: no regressions, the new test fails ondevelopand passes with this branch. The one failing test in the full run (test624CSVParsing.js, a bad-URL network timeout) fails the same way ondevelopand is unrelated.