Skip to content

Keep the definition of a table as data in table.schema and build its rules from it #3169

Backlog

Description

@mathiasrw

What happens today

CREATE TABLE (src/statements/createtable.js, line 142) turns the definition into closures and code strings on the table object.

  • table.pk and table.uk[] hold onrightfns code and onrightfn functions (lines 224, 233, 300, 322). Column UNIQUE keeps names in uk.columns, table UNIQUE keeps column objects with an expression.
  • CHECK constraints and foreign keys are closures in table.checks[] (lines 209, 263, 386).
  • Defaults and ON UPDATE are code strings table.defaultfns and table.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 in src/statements/trigger.js.
  • ALTER TABLE (src/statements/altertable.js) leaves a TODO defaultfns (line 81).

What to change

  1. Build table.schema in CREATE TABLE as 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}],
    };
  2. Add compileTableRules(table) in src/statements/tablerules.js (Move the key and check rules of a table into one module #2716). It builds pk.onrightfn, uk, table.checks, defaultfns, onupdatefns and the identity entries from table.schema, with the same code and error texts. CREATE TABLE builds the schema and then calls it.

  3. ALTER TABLE (add, drop, rename, modify column), CREATE INDEX and DROP INDEX for UNIQUE, CREATE TRIGGER and DROP TRIGGER update table.schema and call compileTableRules again where a rule changed.

  4. Identity counters (table.identities[col].value) stay where they are.

  5. 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.schema holds them as data.
  • It checks that table.pk.onrightfns, table.defaultfns and table.onupdatefns are 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.

Depends on

#2716

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions