Repository navigation
Description
What happens today
CREATE TABLE (src/statements/createtable.js, line 142) turns the definition into closures and code strings on the table object.
table.pkandtable.uk[]holdonrightfnscode andonrightfnfunctions (lines 224, 233, 300, 322). Column UNIQUE keeps names inuk.columns, table UNIQUE keeps column objects with anexpression.- CHECK constraints and foreign keys are closures in
table.checks[](lines 209, 263, 386). - Defaults and ON UPDATE are code strings
table.defaultfnsandtable.onupdatefns(lines 291-292), spliced into INSERT and UPDATE code. - Identity is
table.identities[col] = {value, step}. - Triggers are
table.beforeinsert[name]and the like, filled insrc/statements/trigger.js. ALTER TABLE(src/statements/altertable.js) leaves aTODO defaultfns(line 81).
What to change
-
Build
table.schemainCREATE TABLEas plain data, next to today's fields. Expressions stay parsed nodes.table.schema = { columns: [{columnid, dbtypeid, dbsize, dbprecision, notnull}], pk: {columns: ['a', 'b'], prefixes: [undefined, 3]} | null, uniques: [{columns: [...], expressions: [node, ...] | null, name}], foreignKeys: [{columns: ['v'], table: {databaseid, tableid}, refColumns: ['u'] | null, name}], checks: [{name, expression: node}], defaults: {n: node}, onupdate: {m: node}, identity: {id: {seed: 1, step: 1}}, triggers: [{name, when: 'BEFORE', action: 'INSERT', statement: node | null, funcid}], };
-
Add
compileTableRules(table)insrc/statements/tablerules.js(Move the key and check rules of a table into one module #2716). It buildspk.onrightfn,uk,table.checks,defaultfns,onupdatefnsand the identity entries fromtable.schema, with the same code and error texts.CREATE TABLEbuilds the schema and then calls it. -
ALTER TABLE(add, drop, rename, modify column),CREATE INDEXandDROP INDEXfor UNIQUE,CREATE TRIGGERandDROP TRIGGERupdatetable.schemaand callcompileTableRulesagain where a rule changed. -
Identity counters (
table.identities[col].value) stay where they are. -
Leave every reader of the old fields (
insert.js,update.js, the table closures, the engines) as it is.
What stays the same
- Every result and every error text, for memory tables and engines.
- Today's fields keep their values.
- Engines save and restore exactly what they do now.
Done when
- A new test file named after this issue creates tables with each kind of rule (column and table PRIMARY KEY, composite and prefix keys, column and table UNIQUE with an expression, CHECK, foreign key in both forms, DEFAULT, ON UPDATE, IDENTITY and SERIAL, triggers) and checks that
table.schemaholds them as data. - It checks that
table.pk.onrightfns,table.defaultfnsandtable.onupdatefnsare the same strings as on main, and that ALTER TABLE and CREATE and DROP TRIGGER update the schema. - The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Activity