Repository navigation
Description
Several bugs come from INSERT, UPDATE and DELETE checking a table's rules in more than one place. Memory tables check keys, defaults, foreign keys and triggers inside table.insert, table.update and table.delete, while tables in a storage engine take a separate path, and each engine saves its own selection of the table's definition.
What this issue is about
This issue asks for a change in structure, not for a fix of each bug listed below. The bugs show where the current structure fails, and they serve as test cases. Three changes.
-
The table definition as data. Keys, foreign keys, checks, defaults, identity and triggers are plain objects on the table, not closures or code strings built in
CREATE TABLE.table.schema = {columns, pk, uniques, foreignKeys, checks, defaults, identity, triggers};
-
One write pipeline. INSERT, UPDATE and DELETE pass their rows through the same steps, whether the table lives in memory or in a storage engine, and OUTPUT is built in one place.
writeRows(table, 'insert', rows, ctx) // defaults, identity, NOT NULL, checks, primary and unique keys, foreign keys, // triggers before, storage.insert(rows), key indexes, triggers after // returns {count, output}
-
One storage interface that memory tables and each engine (IndexedDB, localStorage, FileStorage) implement, so every engine stores the whole definition and every engine is checked the same way.
interface Storage { load(table); insert(table, rows); update(table, changes); delete(table, rows); truncate(table); saveSchema(table, schema); }
Key indexes belong to the table and are rebuilt when table.data is replaced. Triggers get the row as NEW and OLD from the pipeline.
Before you start
This issue does not depend on the other areas and can start now. Only UPDATE ? and DELETE FROM ? wait for the source interface of #2724, as they become a source plus this pipeline (#2682).
Ground rules
- Results stay as in 4.x for memory tables. The one intended change is that tables in a storage engine enforce what memory tables already enforce (keys, checks, foreign keys). Only the bugs a pull request names may change results.
- Small steps. A good first pull request moves the key and check logic out of
table.insertinto one function and calls it from both the memory path and the engine path ofyy.Insert.compile. That fixes Give every storage engine one storage interface that saves the whole table definition and writes through the pipeline #2681 for INSERT. - Each pull request passes the full test suite and the parity check in
test/parity/against the build before it. - The code this touches moves to ES6 as part of the work, with constraints as objects.
Bugs traced to this
- Storage engines skip keys and checks: UNIQUE key doesn't work with localStorage engine. #566, UNIQUE with FILESTORAGE not Working! #1148, Foreign key via
REFERENCESdoes not work withFILESTORAGE#1846, Give every storage engine one storage interface that saves the whole table definition and writes through the pipeline #2681 - Indexes out of date after
table.datais assigned: After assigning table.data directly, INSERT stores duplicate keys and DELETE throws #2715, Move the key and check rules of a table into one module #2716 - Foreign keys tied to the primary key: A foreign key to a UNIQUE column rejects values that exist #2661, Check a foreign key against the key of the referenced table that it names #2662
- Trigger arguments and OUTPUT handled per statement: Pass the rows of INSERT, UPDATE and DELETE through one write pipeline #2650, Review #2377 for code improvements: CALL triggers get the row instead of their arguments #2685
UPDATE ?andDELETE FROM ?through a temporary table: Run UPDATE, DELETE and INSERT on a parameter array without a temporary table #2682
Activity