Skip to content

Fix UNIQUE/PRIMARY KEY silently ignored on FILESTORAGE and LOCALSTORAGE tables - #2977

Open
shaktimaan00 wants to merge 1 commit into
AlaSQL:developfrom
shaktimaan00:fix-unique-constraint-filestorage
Open

shaktimaan00 wants to merge 1 commit into
AlaSQL:developfrom
shaktimaan00:fix-unique-constraint-filestorage

Conversation

@shaktimaan00

Copy link
Copy Markdown
Contributor

Fixes #1148.

What was wrong

CREATE TABLE on an engine-backed table (FILESTORAGE, LOCALSTORAGE) returned early by calling the engine's createTable(), before the code further down that sets table.insert() — the function that checks PRIMARY KEY / UNIQUE before a row goes in. So those tables never got table.insert() at all.

INSERT falls back to a raw data.concat() when a table has no insert(), 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:

ATTACH FILESTORAGE DATABASE testdb("./testdb.json");
USE testdb;
CREATE TABLE usersTable (id INT UNIQUE, name NVARCHAR(255))
INSERT INTO usersTable VALUES (1,"Andrey")
INSERT INTO usersTable VALUES (2,"Kate")
INSERT INTO usersTable VALUES (3,"Kate")
INSERT INTO usersTable VALUES (3,"Andrey")

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: call engine.createTable() after table.insert() is wired up, not before, so engine-backed tables get it too.
  • src/70insert.js: only take the engine's fast intoTable() append path when the table has no PK/UNIQUE to check. Tables with a constraint go through table.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 live table.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 in FS.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 on develop and passes with this branch. The one failing test in the full run (test624CSVParsing.js, a bad-URL network timeout) fails the same way on develop and is unrelated.

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
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.

UNIQUE with FILESTORAGE not Working!

2 participants