@aria-framework/db-worker 0.31.0 → 0.31.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -896,6 +896,24 @@ cross-partner references refused by the composite keys) and plants a difference
896
896
 
897
897
  ## Changelog
898
898
 
899
+ - **0.31.1** — `check` reads `CREATE TABLE … AS / LIKE / OF / PARTITION OF` from the statement's shape, not from a
900
+ keyword next to the name. For a table whose name it reads, a table that writes out its own columns has `(` straight
901
+ after its name, no top-level `LIKE` item in that body and no top-level `AS` after it; any other CREATE TABLE is
902
+ refused. Parens and double-quoted names in the body are walked, not counted.
903
+ - **Fixed:** a name ending in "as" or "of" is now read whole. 0.31.0 refused `CREATE TABLE cameras (…)` as
904
+ `camer` + `AS`. An app that quoted such a name to get past 0.31.0 may keep the quotes.
905
+ - **Now refused:** forms 0.31.0 accepted, for any name:
906
+ - `AS` after a column list, `WITH (…)`, `USING`, `TABLESPACE`, `ON COMMIT` or `WITHOUT OIDS`;
907
+ - a `LIKE` that is not the body's first item.
908
+ - **Now refused, and valid:** a table whose unquoted name holds `$` or a non-ASCII letter (`cam$1 (…)`, `café (…)`),
909
+ with "its columns cannot be read". `check` reads such a name only up to that character, so it cannot see the body.
910
+ - **Also fixed:** a column named `preferences` followed by a type was read as `REFERENCES` and refused as a key
911
+ (`preferences users`). `REFERENCES` must now start a word; every real key is still found.
912
+ - **Unchanged, not checked:** a name `check` cannot read at all (a quoted name with a space, for example) is still
913
+ skipped by every check on that statement, as in 0.31.0.
914
+ - Tests: `test/tenancyGenerator.js`, "CREATE TABLE … AS / LIKE / OF / PARTITION OF IS READ FROM THE STATEMENT'S
915
+ SHAPE" and "REFERENCES IS A WORD".
916
+
899
917
  - **0.31.0** — Table definitions as data (above): `defineTables`, `tenancyFragment`, `classificationOf`,
900
918
  `isTableDefinitions`, `TableDefinitionError`, `checkDefinedSchema`, `assertDefinedSchema`; `defineTenancy` reads
901
919
  `TABLE_DEFINITIONS`; `aria-tenancy new --definitions <set>`, and `check` holds generated pairs byte for byte; the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aria-framework/db-worker",
3
- "version": "0.31.0",
3
+ "version": "0.31.1",
4
4
  "description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -80,7 +80,7 @@
80
80
  * This is a line-and-regex reader, not a SQL parser, so where a construct is hard to read it is REFUSED in a policed
81
81
  * migration whatever it contains: any DO block, CREATE FUNCTION / PROCEDURE, any dollar quote, CREATE RULE, ALTER
82
82
  * ROLE, a CREATE VIEW without WITH (security_invoker = true) (a view runs as its owner, who bypasses RLS) or any
83
- * materialized view, CREATE TABLE … AS / LIKE, a nested block comment, a schema other than public, the runner markers
83
+ * materialized view, CREATE TABLE … AS / LIKE / OF / PARTITION OF (copiesColumns), a nested block comment, a schema other than public, the runner markers
84
84
  * `-- aria:covers-through` / `-- aria:no-transaction` (one makes later files ledgered WITHOUT running, the other drops
85
85
  * the transaction), ALTER VIEW, OWNER TO, SELECT … INTO (a table the SQLite classification guard never sees), SET
86
86
  * SCHEMA, session_replication_role (replica mode stops enforcing keys), DROP CONSTRAINT (except a lone drop of a CHECK
@@ -384,9 +384,11 @@ function fold(sql) {
384
384
  /** An identifier as Postgres resolves it: quoted kept, unquoted case-folded. */
385
385
  const ident = (s) => (s.startsWith('"') ? s.slice(1, -1) : s.toLowerCase());
386
386
 
387
+ const CREATE_TABLE_HEAD = /^\s*CREATE\s+(?:(?:GLOBAL|LOCAL)\s+)?(?:TEMP(?:ORARY)?\s+|UNLOGGED\s+)?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?("\w+"|\w+)/i;
388
+
387
389
  /** The table a CREATE TABLE / ALTER TABLE statement acts on, else null. */
388
390
  function tableOf(sql) {
389
- let m = /^\s*CREATE\s+(?:(?:GLOBAL|LOCAL)\s+)?(?:TEMP(?:ORARY)?\s+|UNLOGGED\s+)?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?("\w+"|\w+)/i.exec(sql);
391
+ let m = CREATE_TABLE_HEAD.exec(sql);
390
392
  if (m) return { kind: 'create', table: ident(m[1]) };
391
393
  m = /^\s*ALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?("\w+"|\w+)/i.exec(sql);
392
394
  if (m) return { kind: 'alter', table: ident(m[1]) };
@@ -414,12 +416,49 @@ function bodyItems(sql) {
414
416
  return items.map((s) => s.trim()).filter(Boolean);
415
417
  }
416
418
 
419
+ /**
420
+ * Does this CREATE TABLE (its normalised head) take its columns from somewhere else, so they cannot be read: AS (a
421
+ * query's), LIKE (another table's), OF (a type's), PARTITION OF (a parent's)? Decided from the statement's SHAPE, not
422
+ * from a keyword next to the name: a table that writes out its own columns has `(` straight after its name, has no
423
+ * top-level LIKE item in that body, and no top-level AS after it. So `cameras (` is never `camer` + AS, and
424
+ * `copies (a, b) AS SELECT …`, `copies WITH (fillfactor = 70) AS …` and `copies (x INT, LIKE notes)` are refused.
425
+ * The name is read as tableOf reads it (CREATE_TABLE_HEAD), so both see the same name.
426
+ */
427
+ function copiesColumns(head) {
428
+ const m = CREATE_TABLE_HEAD.exec(head);
429
+ // Not reached from checkTree, which calls this only after tableOf matched the same pattern; kept so another caller
430
+ // fails closed. A name the pattern cannot read at all (a quoted name with a space, say) makes tableOf return null,
431
+ // and checkTree then checks nothing about that statement: an older gap this function does not close.
432
+ if (!m) return true;
433
+ const rest = head.slice(m[0].length);
434
+ if (!/^\s*\(/.test(rest)) return true;
435
+ // The body: from that `(` to its own closing paren. Literals' contents are already blanked in `head`; a
436
+ // double-quoted identifier is skipped whole, so a paren or the word AS inside one counts for nothing.
437
+ const open = rest.indexOf('(');
438
+ let depth = 0;
439
+ let close = -1;
440
+ for (let i = open; i < rest.length; i++) {
441
+ const c = rest[i];
442
+ if (c === '"') { const j = rest.indexOf('"', i + 1); if (j < 0) return true; i = j; continue; }
443
+ if (c === '(') depth++;
444
+ else if (c === ')' && --depth === 0) { close = i; break; }
445
+ }
446
+ if (close < 0) return true;
447
+ if (bodyItems(rest.slice(open, close + 1)).some((it) => /^LIKE\b/i.test(it))) return true;
448
+ // After the body, outside any parens and quoted names: WITH (…), PARTITION BY RANGE (…), TABLESPACE, ON COMMIT
449
+ // never say AS; a column-name list followed by AS SELECT does.
450
+ let after = rest.slice(close + 1).replace(/"(?:[^"]|"")*"/g, ' ');
451
+ for (let prev = null; prev !== after;) { prev = after; after = after.replace(/\([^()]*\)/g, ' '); }
452
+ return /\bAS\b/i.test(after);
453
+ }
454
+
417
455
  const cols = (s) => s.split(',').map((x) => x.trim()).filter(Boolean).map(ident);
418
456
 
419
457
  /** Every key declared in a statement: { cols, parent, parentCols, onDelete, onUpdate, inline }. */
420
458
  function keysIn(sql) {
421
459
  const keys = [];
422
- const re = /REFERENCES\s*("\w+"|\w+)\s*(?:\(([^)]*)\))?/gi;
460
+ // `\b`: the keyword, not the end of a word (a column `preferences users` is not a key into users).
461
+ const re = /\bREFERENCES\s*("\w+"|\w+)\s*(?:\(([^)]*)\))?/gi;
423
462
  let m;
424
463
  while ((m = re.exec(sql))) {
425
464
  const before = sql.slice(0, m.index);
@@ -750,7 +789,9 @@ function checkTree({ files, manifest, frozen, partnerIdType, principalTable = 'u
750
789
  const child = on.table;
751
790
  if (on.kind === 'create') {
752
791
  if (!classified.has(child)) problems.push(`${f}: creates ${child}, which ${manifestName} does not classify`);
753
- if (/^CREATE\s+.*?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?("\w+"|\w+)\s*(?:\(\s*LIKE\b|AS\b|PARTITION\s+OF\b|OF\b)/i.test(head)) {
792
+ // By the statement's shape (copiesColumns), not a keyword after the name: 0.31.0 let the name give its tail
793
+ // to the keyword (`cameras (` read as `camer` + AS), and missed AS after a column list or WITH (…).
794
+ if (copiesColumns(head)) {
754
795
  refuse('CREATE TABLE … AS / LIKE / OF — its columns cannot be read');
755
796
  } else if (!scopedNames.has(child) && bodyItems(s.sql).some((it) => /^"?(partner_id|customer_id)"?\s/i.test(it))) {
756
797
  problems.push(`${f}: ${child} has a partner_id or customer_id column but is not scoped in ${manifestName}`);