architecture-harness 0.21.0 → 0.21.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "architecture-harness",
3
- "version": "0.21.0",
3
+ "version": "0.21.1",
4
4
  "type": "module",
5
5
  "description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
6
6
  "license": "MIT",
@@ -22,7 +22,8 @@ const SNAKE = /\b([a-z][a-z0-9]*_[a-z0-9_]+)\b/g;
22
22
  // Tables are matched by position instead. A single-word table name is
23
23
  // indistinguishable from a keyword by shape, and a stop-list of keywords is the
24
24
  // same trap as a list of column types: the one it misses blocks a unit.
25
- const TABLE_POSITION = /\b(?:from|join|into|update)\s+([a-z][a-z0-9_]*)/g;
25
+ // A name after `from` or `join` that opens a parenthesis is a function, such as `unnest(...)`.
26
+ const TABLE_POSITION = /\b(from|join|into|update)\s+([a-z][a-z0-9_]*)(\s*\()?/g;
26
27
  /** The few words that legally stand where a table would: `do update set`, `join lateral (`. */
27
28
  const NOT_A_TABLE = new Set(["set", "lateral", "only", "select"]);
28
29
  /** Names a statement binds for itself: a common table expression, or an alias. */
@@ -52,6 +53,8 @@ export const SQL_BUILTINS = Object.freeze([
52
53
  "jsonb_set",
53
54
  "current_timestamp",
54
55
  "current_date",
56
+ "clock_timestamp",
57
+ "unnest",
55
58
  ]);
56
59
 
57
60
  /** The text between the parenthesis at `open` and its match. */
@@ -67,10 +70,27 @@ function parens(source, open) {
67
70
  return source.slice(open + 1);
68
71
  }
69
72
 
70
- /** A constraint line declares no column, so only a name followed by a type counts. */
73
+ /** The parts of a column list, split on the commas outside any parenthesis. */
74
+ function topLevelParts(body) {
75
+ const parts = [];
76
+ let depth = 0;
77
+ let start = 0;
78
+ for (let index = 0; index < body.length; index += 1) {
79
+ if (body[index] === "(") depth += 1;
80
+ if (body[index] === ")") depth -= 1;
81
+ if (body[index] === "," && depth === 0) {
82
+ parts.push(body.slice(start, index));
83
+ start = index + 1;
84
+ }
85
+ }
86
+ parts.push(body.slice(start));
87
+ return parts;
88
+ }
89
+
90
+ /** A constraint declares no column, so only a name followed by a type counts. Columns may share a line. */
71
91
  function addColumnLines(columns, body) {
72
- for (const line of body.split("\n")) {
73
- const column = COLUMN_LINE.exec(line.trim());
92
+ for (const part of topLevelParts(blanked(body))) {
93
+ const column = COLUMN_LINE.exec(part.trim());
74
94
  if (column && !CONSTRAINT_OPENER.has(column[1])) columns.add(column[1]);
75
95
  }
76
96
  }
@@ -78,7 +98,9 @@ function addColumnLines(columns, body) {
78
98
  /** @returns {Map<string, Set<string>>} table to its declared columns, across every migration. */
79
99
  export function declaredColumns(sources) {
80
100
  const tables = new Map();
81
- for (const sql of sources) {
101
+ for (const source of sources) {
102
+ // Keywords and unquoted names ignore case, so a migration in capitals declares the same columns.
103
+ const sql = source.toLowerCase();
82
104
  CREATE_TABLE.lastIndex = 0;
83
105
  let match = CREATE_TABLE.exec(sql);
84
106
  while (match !== null) {
@@ -184,7 +206,8 @@ export function usedIdentifiers(source) {
184
206
  TABLE_POSITION.lastIndex = 0;
185
207
  let table = TABLE_POSITION.exec(statement);
186
208
  while (table !== null) {
187
- if (!NOT_A_TABLE.has(table[1])) tables.add(table[1]);
209
+ const isCall = table[3] !== undefined && (table[1] === "from" || table[1] === "join");
210
+ if (!NOT_A_TABLE.has(table[2]) && !isCall) tables.add(table[2]);
188
211
  table = TABLE_POSITION.exec(statement);
189
212
  }
190
213
  }
@@ -60,6 +60,31 @@ test("an add column belongs to its own alter table, never to the one before it",
60
60
  assert.ok(tables.get("sites").has("site_name"));
61
61
  });
62
62
 
63
+ test("a migration in capitals declares the same columns", () => {
64
+ const tables = declaredColumns([ddl, "ALTER TABLE projects ADD COLUMN blackboard_edited boolean NOT NULL DEFAULT false;"]);
65
+ assert.ok(tables.get("projects").has("blackboard_edited"));
66
+ });
67
+
68
+ test("columns that share a line are each declared, a generated column included", () => {
69
+ const tables = declaredColumns([
70
+ `create table results (
71
+ -- one row per item
72
+ item_id uuid not null, sub_pin_id uuid, owner_id uuid generated always as (coalesce(sub_pin_id, pin_id)) stored, checked boolean not null,
73
+ result text not null default 'a, b', corrected_at timestamptz,
74
+ primary key (item_id)
75
+ );`,
76
+ ]);
77
+ assert.deepEqual([...tables.get("results")].sort(), ["checked", "corrected_at", "item_id", "owner_id", "result", "sub_pin_id"]);
78
+ });
79
+
80
+ test("a function in a from clause, and clock_timestamp, are no undeclared names", () => {
81
+ const tables = declaredColumns([ddl]);
82
+ const source = resource(
83
+ "insert into projects (id, created_at) select photo_id, clock_timestamp() from unnest($1::uuid[]) as photo_id",
84
+ );
85
+ assert.deepEqual(checkSqlIdentifiers(tables, [{ path: "r.ts", source }]), []);
86
+ });
87
+
63
88
  test("an agreeing slice has no finding", () => {
64
89
  const tables = declaredColumns([ddl]);
65
90
  const source = resource("select id, tenant_id, code from projects where tenant_id = $1");