@starci/eslint-canon-be 3.18.4 → 3.19.0
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/CHANGELOG.md +4 -0
- package/dist/comments.js +1 -1
- package/dist/explicit-surface.js +1 -1
- package/dist/index.js +1 -1
- package/dist/jwt-alg.js +1 -1
- package/dist/lib/edition.d.ts +1 -1
- package/dist/lib/edition.js +1 -1
- package/dist/lib/hfs.d.ts +0 -6
- package/dist/lib/hfs.js +7 -5
- package/dist/lib/runtime-root.d.ts +6 -1
- package/dist/lib/runtime-root.js +8 -2
- package/dist/lib/subject-values.js +1 -1
- package/dist/module-layering.js +2 -2
- package/dist/project-graph.js +1 -1
- package/dist/third-party-owner.js +1 -1
- package/package.json +3 -1
- package/runtime/bundle.json +1 -190
- package/runtime/scripts/api/git/recorded-lines.mjs +5 -5
- package/runtime/scripts/hfs/project-graph.mjs +6 -6
- package/runtime/scripts/hfs/project-rule.mjs +3 -3
- package/runtime/config.example.yaml +0 -194
- package/runtime/engine/by-code-unit.mjs +0 -12
- package/runtime/engine/config.mjs +0 -461
- package/runtime/engine/invalid-config.mjs +0 -15
- package/runtime/engine/model-config.mjs +0 -112
- package/runtime/engine/orca-config.mjs +0 -34
- package/runtime/engine/plain-object.mjs +0 -5
- package/runtime/engine/release-config.mjs +0 -32
- package/runtime/engine/removed-vocabulary.mjs +0 -27
- package/runtime/engine/resources-config.mjs +0 -59
- package/runtime/engine/runtime-root.mjs +0 -138
- package/runtime/engine/secrets.mjs +0 -202
- package/runtime/engine/sonar-config.mjs +0 -24
- package/runtime/engine/temp-root.mjs +0 -26
- package/runtime/engine/yaml.mjs +0 -161
- package/runtime/knowledge/grammars/index.yaml +0 -114
- package/runtime/knowledge/hfs/canon-pins.yaml +0 -720
- package/runtime/knowledge/hfs/fe-stack.yaml +0 -272
- package/runtime/knowledge/hfs/rules.yaml +0 -6760
- package/runtime/knowledge/hfs/slots.yaml +0 -2783
- package/runtime/knowledge/patterns/fe/folder.yaml +0 -425
- package/runtime/knowledge/sonar-gate.yaml +0 -151
- package/runtime/modules/kernel/removed-vocabulary.yaml +0 -458
- package/runtime/modules/models/registry.yaml +0 -221
- package/runtime/modules/models/runtimes.yaml +0 -556
- package/runtime/modules/models/tiers.yaml +0 -66
- package/runtime/scripts/api/git/cat-file.mjs +0 -4
- package/runtime/scripts/api/git/config-get.mjs +0 -9
- package/runtime/scripts/api/git/diff.mjs +0 -4
- package/runtime/scripts/api/git/lib.mjs +0 -80
- package/runtime/scripts/api/git/ls-files.mjs +0 -5
- package/runtime/scripts/api/git/ls-tree.mjs +0 -4
- package/runtime/scripts/api/git/merge-base.mjs +0 -8
- package/runtime/scripts/api/git/remote.mjs +0 -4
- package/runtime/scripts/api/git/rev-parse-query.mjs +0 -4
- package/runtime/scripts/api/git/show.mjs +0 -5
- package/runtime/scripts/api/git/tag.mjs +0 -4
- package/runtime/scripts/hfs/allows.mjs +0 -77
- package/runtime/scripts/hfs/architecture/ast-walks.mjs +0 -250
- package/runtime/scripts/hfs/architecture/automatic-gates.mjs +0 -103
- package/runtime/scripts/hfs/architecture/backend-app-files.mjs +0 -114
- package/runtime/scripts/hfs/architecture/backend.mjs +0 -462
- package/runtime/scripts/hfs/architecture/background-unowned.mjs +0 -254
- package/runtime/scripts/hfs/architecture/canon-clones.mjs +0 -291
- package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +0 -224
- package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +0 -102
- package/runtime/scripts/hfs/architecture/clones.mjs +0 -287
- package/runtime/scripts/hfs/architecture/component-tiers.mjs +0 -161
- package/runtime/scripts/hfs/architecture/config-unread.mjs +0 -16
- package/runtime/scripts/hfs/architecture/config.mjs +0 -407
- package/runtime/scripts/hfs/architecture/connection-map.mjs +0 -350
- package/runtime/scripts/hfs/architecture/constructor-deps.mjs +0 -115
- package/runtime/scripts/hfs/architecture/context-coupling.mjs +0 -117
- package/runtime/scripts/hfs/architecture/context-map.mjs +0 -355
- package/runtime/scripts/hfs/architecture/context-owner.mjs +0 -102
- package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +0 -65
- package/runtime/scripts/hfs/architecture/context-transaction.mjs +0 -75
- package/runtime/scripts/hfs/architecture/contract-bounds.mjs +0 -72
- package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +0 -177
- package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +0 -360
- package/runtime/scripts/hfs/architecture/contracts.mjs +0 -489
- package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +0 -81
- package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +0 -173
- package/runtime/scripts/hfs/architecture/dead-exports.mjs +0 -420
- package/runtime/scripts/hfs/architecture/default-deny.mjs +0 -175
- package/runtime/scripts/hfs/architecture/doc-language.mjs +0 -45
- package/runtime/scripts/hfs/architecture/entity-registry.mjs +0 -51
- package/runtime/scripts/hfs/architecture/entrypoint.mjs +0 -75
- package/runtime/scripts/hfs/architecture/error-codes.mjs +0 -54
- package/runtime/scripts/hfs/architecture/error-masked.mjs +0 -78
- package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +0 -44
- package/runtime/scripts/hfs/architecture/feature-shape.mjs +0 -60
- package/runtime/scripts/hfs/architecture/framework-pinned.mjs +0 -41
- package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +0 -112
- package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +0 -193
- package/runtime/scripts/hfs/architecture/frontend-routing.mjs +0 -198
- package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +0 -368
- package/runtime/scripts/hfs/architecture/frontend-world.mjs +0 -374
- package/runtime/scripts/hfs/architecture/frontend.mjs +0 -248
- package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +0 -185
- package/runtime/scripts/hfs/architecture/hfs-graph.mjs +0 -77
- package/runtime/scripts/hfs/architecture/hfs.mjs +0 -394
- package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +0 -118
- package/runtime/scripts/hfs/architecture/i18n-keys.mjs +0 -213
- package/runtime/scripts/hfs/architecture/identity.mjs +0 -121
- package/runtime/scripts/hfs/architecture/index.mjs +0 -402
- package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +0 -69
- package/runtime/scripts/hfs/architecture/jwt-ast.mjs +0 -231
- package/runtime/scripts/hfs/architecture/machine-ast.mjs +0 -232
- package/runtime/scripts/hfs/architecture/managed-scripts.mjs +0 -32
- package/runtime/scripts/hfs/architecture/module-per-transport.mjs +0 -237
- package/runtime/scripts/hfs/architecture/next-data-contract.mjs +0 -126
- package/runtime/scripts/hfs/architecture/next-data-inference.mjs +0 -192
- package/runtime/scripts/hfs/architecture/next-data-key.mjs +0 -374
- package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +0 -26
- package/runtime/scripts/hfs/architecture/next-data.mjs +0 -425
- package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +0 -55
- package/runtime/scripts/hfs/architecture/owners.mjs +0 -142
- package/runtime/scripts/hfs/architecture/package-shape.mjs +0 -92
- package/runtime/scripts/hfs/architecture/presentation.mjs +0 -173
- package/runtime/scripts/hfs/architecture/reachability.mjs +0 -330
- package/runtime/scripts/hfs/architecture/register-once.mjs +0 -212
- package/runtime/scripts/hfs/architecture/registration.mjs +0 -372
- package/runtime/scripts/hfs/architecture/render-graph.mjs +0 -449
- package/runtime/scripts/hfs/architecture/render-prop-across-server.mjs +0 -58
- package/runtime/scripts/hfs/architecture/required-files.mjs +0 -237
- package/runtime/scripts/hfs/architecture/root-packages.mjs +0 -78
- package/runtime/scripts/hfs/architecture/route-files-thin.mjs +0 -133
- package/runtime/scripts/hfs/architecture/schema-owner.mjs +0 -288
- package/runtime/scripts/hfs/architecture/server-action.mjs +0 -78
- package/runtime/scripts/hfs/architecture/side-boundary.mjs +0 -13
- package/runtime/scripts/hfs/architecture/source-names-shape.mjs +0 -320
- package/runtime/scripts/hfs/architecture/source-names.mjs +0 -393
- package/runtime/scripts/hfs/architecture/sql-keys.mjs +0 -344
- package/runtime/scripts/hfs/architecture/sql-owner.mjs +0 -291
- package/runtime/scripts/hfs/architecture/sql-returning.mjs +0 -76
- package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +0 -117
- package/runtime/scripts/hfs/architecture/sql-tokens.mjs +0 -587
- package/runtime/scripts/hfs/architecture/staged-consumers.mjs +0 -42
- package/runtime/scripts/hfs/architecture/surface-nesting.mjs +0 -127
- package/runtime/scripts/hfs/architecture/surface.mjs +0 -88
- package/runtime/scripts/hfs/architecture/symbols.mjs +0 -273
- package/runtime/scripts/hfs/architecture/test-world-files.mjs +0 -263
- package/runtime/scripts/hfs/architecture/tiers.mjs +0 -256
- package/runtime/scripts/hfs/architecture/transport-owner.mjs +0 -248
- package/runtime/scripts/hfs/architecture/type-context.mjs +0 -335
- package/runtime/scripts/hfs/architecture/typescript.mjs +0 -477
- package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +0 -165
- package/runtime/scripts/hfs/declaration-shape.mjs +0 -196
- package/runtime/scripts/hfs/declaration-slots.mjs +0 -89
- package/runtime/scripts/hfs/edition-slots.mjs +0 -80
- package/runtime/scripts/hfs/explicit-surface-rule.mjs +0 -59
- package/runtime/scripts/hfs/linear-text.mjs +0 -30
- package/runtime/scripts/hfs/manifest-shape.mjs +0 -474
- package/runtime/scripts/hfs/param-names.mjs +0 -13
- package/runtime/scripts/hfs/path-findings.mjs +0 -167
- package/runtime/scripts/hfs/repo-identity.mjs +0 -89
- package/runtime/scripts/hfs/rule-catalog.mjs +0 -458
- package/runtime/scripts/hfs/rule-params-shape.mjs +0 -103
- package/runtime/scripts/hfs/rules/database-constants.mjs +0 -25
- package/runtime/scripts/hfs/rules/database-migrations.mjs +0 -684
- package/runtime/scripts/hfs/rules/fe-no-tests.mjs +0 -68
- package/runtime/scripts/hfs/rules/read.mjs +0 -45
- package/runtime/scripts/hfs/rules/secrets.mjs +0 -79
- package/runtime/scripts/hfs/rules/spec-placement.mjs +0 -28
- package/runtime/scripts/hfs/slot-app-view.mjs +0 -97
- package/runtime/scripts/hfs/slot-classify.mjs +0 -69
- package/runtime/scripts/hfs/slot-errors.mjs +0 -12
- package/runtime/scripts/hfs/slot-imports.mjs +0 -98
- package/runtime/scripts/hfs/slot-manifest-shape.mjs +0 -111
- package/runtime/scripts/hfs/slot-match.mjs +0 -164
- package/runtime/scripts/hfs/slot-path.mjs +0 -6
- package/runtime/scripts/hfs/slot-required.mjs +0 -95
- package/runtime/scripts/hfs/slot-semantic-problems.mjs +0 -175
- package/runtime/scripts/hfs/slot-side-problems.mjs +0 -32
- package/runtime/scripts/hfs/slots.mjs +0 -306
- package/runtime/scripts/hfs/test-world-slot.mjs +0 -5
- package/runtime/scripts/hfs/trailing-slashes.mjs +0 -8
- package/runtime/scripts/hfs/typescript-programs.mjs +0 -95
- package/runtime/scripts/hfs/view.mjs +0 -180
- package/runtime/scripts/lib/declaration-surface.mjs +0 -166
- package/runtime/scripts/lib/dockerfile.mjs +0 -164
- package/runtime/scripts/lib/env.mjs +0 -18
- package/runtime/scripts/lib/event-contract.mjs +0 -102
- package/runtime/scripts/lib/git-hooks-dir.mjs +0 -30
- package/runtime/scripts/lib/git.mjs +0 -35
- package/runtime/scripts/lib/glob.mjs +0 -32
- package/runtime/scripts/lib/grammar-lint.mjs +0 -57
- package/runtime/scripts/lib/i18n.mjs +0 -82
- package/runtime/scripts/lib/in-order.mjs +0 -90
- package/runtime/scripts/lib/json-clone.mjs +0 -70
- package/runtime/scripts/lib/json.mjs +0 -44
- package/runtime/scripts/lib/language.mjs +0 -233
- package/runtime/scripts/lib/list.mjs +0 -26
- package/runtime/scripts/lib/mutation-fence.mjs +0 -20
- package/runtime/scripts/lib/package-at.mjs +0 -62
- package/runtime/scripts/lib/path-key.mjs +0 -124
- package/runtime/scripts/lib/read-text.mjs +0 -47
- package/runtime/scripts/lib/regex.mjs +0 -3
- package/runtime/scripts/lib/rule-packs.mjs +0 -243
- package/runtime/scripts/lib/same-text.mjs +0 -4
- package/runtime/scripts/lib/secret-patterns.mjs +0 -230
- package/runtime/scripts/lib/sops-envelope.mjs +0 -212
- package/runtime/scripts/lib/stack-services.mjs +0 -191
- package/runtime/scripts/lib/ts-analysis.mjs +0 -39
- package/runtime/scripts/lib/ts-ast.mjs +0 -63
- package/runtime/scripts/lib/walk.mjs +0 -45
- package/runtime/scripts/lib/yaml-cached.mjs +0 -14
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { lsFiles } from '../../api/git/ls-files.mjs';
|
|
3
|
-
import { byCodeUnit } from '../../lib/list.mjs';
|
|
4
|
-
import { MIGRATIONS_DIR } from '../rules/database-constants.mjs';
|
|
5
|
-
import { databaseSqlStatements, databaseSqlTokens, isMigrationName, migrationStamp } from '../rules/database-migrations.mjs';
|
|
6
|
-
import { readText } from '../rules/read.mjs';
|
|
7
|
-
const word = (token, value) => token?.t === 'word' && token.v === value;
|
|
8
|
-
const identifier = (token) => token !== undefined && ['word', 'ident'].includes(token.t) && token.v === token.v.toLowerCase() ? token.v : null;
|
|
9
|
-
const OPAQUE_STATEMENTS = new Set(['do', 'call', 'select', 'with', 'insert', 'update', 'delete', 'merge', 'truncate', 'execute', 'rollback', 'abort', 'prepare', 'discard', 'refresh', 'copy']);
|
|
10
|
-
const settingName = (token, value) => token !== undefined && ['word', 'ident'].includes(token.t) && token.v.toLowerCase() === value;
|
|
11
|
-
/** A migration relation is public only when its qualification or the current search path proves it. */
|
|
12
|
-
function relationAt(tokens, start, schema) {
|
|
13
|
-
const first = identifier(tokens[start]);
|
|
14
|
-
if (first === null)
|
|
15
|
-
return null;
|
|
16
|
-
if (tokens[start + 1]?.v !== '.')
|
|
17
|
-
return { schema, table: first, next: start + 1 };
|
|
18
|
-
const table = identifier(tokens[start + 2]);
|
|
19
|
-
if (table === null || tokens[start + 3]?.v === '.')
|
|
20
|
-
return null;
|
|
21
|
-
return { schema: first, table, next: start + 3 };
|
|
22
|
-
}
|
|
23
|
-
/** Split declarations at their own commas, keeping type arguments and constraint column lists together. */
|
|
24
|
-
function clausesOf(tokens) {
|
|
25
|
-
let current = [];
|
|
26
|
-
const clauses = [current];
|
|
27
|
-
let depth = 0;
|
|
28
|
-
for (const token of tokens) {
|
|
29
|
-
if (token.v === '(')
|
|
30
|
-
depth += 1;
|
|
31
|
-
if (token.v === ')')
|
|
32
|
-
depth -= 1;
|
|
33
|
-
if (depth < 0)
|
|
34
|
-
return null;
|
|
35
|
-
if (token.v === ',' && depth === 0) {
|
|
36
|
-
current = [];
|
|
37
|
-
clauses.push(current);
|
|
38
|
-
}
|
|
39
|
-
else
|
|
40
|
-
current.push(token);
|
|
41
|
-
}
|
|
42
|
-
return depth === 0 ? clauses : null;
|
|
43
|
-
}
|
|
44
|
-
function parenthesized(tokens, start) {
|
|
45
|
-
if (tokens[start]?.v !== '(')
|
|
46
|
-
return null;
|
|
47
|
-
let depth = 1;
|
|
48
|
-
for (let index = start + 1; index < tokens.length; index += 1) {
|
|
49
|
-
if (tokens[index]?.v === '(')
|
|
50
|
-
depth += 1;
|
|
51
|
-
if (tokens[index]?.v === ')')
|
|
52
|
-
depth -= 1;
|
|
53
|
-
if (depth === 0)
|
|
54
|
-
return { body: tokens.slice(start + 1, index), next: index + 1 };
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
const isDeferred = (tokens) => tokens.some((token, index) => word(token, 'deferrable') && !word(tokens[index - 1], 'not'));
|
|
59
|
-
/** A declared key proves only its complete, plain column list; index expressions are never column names. */
|
|
60
|
-
function tableKey(tokens) {
|
|
61
|
-
if (isDeferred(tokens))
|
|
62
|
-
return null;
|
|
63
|
-
let start = word(tokens[0], 'constraint') ? 2 : 0;
|
|
64
|
-
if (word(tokens[start], 'primary') && word(tokens[start + 1], 'key'))
|
|
65
|
-
start += 2;
|
|
66
|
-
else if (word(tokens[start], 'unique'))
|
|
67
|
-
start += 1;
|
|
68
|
-
else
|
|
69
|
-
return null;
|
|
70
|
-
const enclosed = parenthesized(tokens, start);
|
|
71
|
-
const clauses = enclosed && clausesOf(enclosed.body);
|
|
72
|
-
if (!clauses?.length || clauses.some((clause) => clause.length !== 1 || identifier(clause[0]) === null))
|
|
73
|
-
return null;
|
|
74
|
-
// Every clause was just proven to be one identifier, so no clause is dropped.
|
|
75
|
-
return clauses.flatMap((clause) => { const column = identifier(clause[0]); return column === null ? [] : [column]; });
|
|
76
|
-
}
|
|
77
|
-
/** Inline key clauses are read at column depth, outside defaults, generated expressions and type parameters. */
|
|
78
|
-
function columnKey(tokens) {
|
|
79
|
-
const column = identifier(tokens[0]);
|
|
80
|
-
if (column === null || isDeferred(tokens))
|
|
81
|
-
return null;
|
|
82
|
-
let depth = 0;
|
|
83
|
-
for (let index = 1; index < tokens.length; index += 1) {
|
|
84
|
-
if (tokens[index]?.v === '(')
|
|
85
|
-
depth += 1;
|
|
86
|
-
if (tokens[index]?.v === ')')
|
|
87
|
-
depth -= 1;
|
|
88
|
-
if (depth === 0 && (word(tokens[index], 'unique') || (word(tokens[index], 'primary') && word(tokens[index + 1], 'key'))))
|
|
89
|
-
return [column];
|
|
90
|
-
}
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
function declarationKey(tokens) {
|
|
94
|
-
const tableConstraint = ['constraint', 'primary', 'unique', 'check', 'foreign', 'exclude'].some((value) => word(tokens[0], value));
|
|
95
|
-
return tableConstraint ? tableKey(tokens) : columnKey(tokens);
|
|
96
|
-
}
|
|
97
|
-
function addKey(keys, columns) {
|
|
98
|
-
if (columns && !keys.some((key) => key.length === columns.length && key.every((column, index) => column === columns[index])))
|
|
99
|
-
keys.push(columns);
|
|
100
|
-
}
|
|
101
|
-
function clearKeys(state, uncertain = false) {
|
|
102
|
-
for (const table of state.tables.keys())
|
|
103
|
-
state.tables.set(table, []);
|
|
104
|
-
if (uncertain)
|
|
105
|
-
state.safe.clear();
|
|
106
|
-
}
|
|
107
|
-
function opaqueExecution(state) {
|
|
108
|
-
clearKeys(state, true);
|
|
109
|
-
state.schema = null;
|
|
110
|
-
}
|
|
111
|
-
function forgetRelation(state, relation) {
|
|
112
|
-
if (!relation || relation.schema === null)
|
|
113
|
-
clearKeys(state, true);
|
|
114
|
-
else if (relation.schema === 'public') {
|
|
115
|
-
state.tables.set(relation.table, []);
|
|
116
|
-
state.safe.delete(relation.table);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/** Conditional table creation cannot establish what an unknown pre-existing table actually constrains. */
|
|
120
|
-
function createTable(state, tokens, start) {
|
|
121
|
-
const conditional = word(tokens[start], 'if') && word(tokens[start + 1], 'not') && word(tokens[start + 2], 'exists');
|
|
122
|
-
if (conditional)
|
|
123
|
-
start += 3;
|
|
124
|
-
const relation = relationAt(tokens, start, state.schema);
|
|
125
|
-
if (!relation || relation.schema !== 'public')
|
|
126
|
-
return;
|
|
127
|
-
if (conditional) {
|
|
128
|
-
if (!state.tables.has(relation.table))
|
|
129
|
-
state.tables.set(relation.table, []);
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
const enclosed = parenthesized(tokens, relation.next);
|
|
133
|
-
const clauses = enclosed && clausesOf(enclosed.body);
|
|
134
|
-
const keys = [];
|
|
135
|
-
state.safe.delete(relation.table);
|
|
136
|
-
state.tables.set(relation.table, keys);
|
|
137
|
-
if (!clauses)
|
|
138
|
-
return;
|
|
139
|
-
state.safe.add(relation.table);
|
|
140
|
-
for (const clause of clauses)
|
|
141
|
-
addKey(keys, declarationKey(clause));
|
|
142
|
-
}
|
|
143
|
-
function safeAlter(tokens) {
|
|
144
|
-
if (word(tokens[0], 'owner') || word(tokens[0], 'cluster'))
|
|
145
|
-
return true;
|
|
146
|
-
const security = tokens.some((token) => word(token, 'security'));
|
|
147
|
-
if (security && ['enable', 'disable', 'force', 'no'].some((value) => word(tokens[0], value)))
|
|
148
|
-
return true;
|
|
149
|
-
return (word(tokens[0], 'set') || word(tokens[0], 'reset')) && tokens[1]?.v === '(';
|
|
150
|
-
}
|
|
151
|
-
/** ADD can establish a new key; drops, renames and unreadable alterations discard the affected proof. */
|
|
152
|
-
function alterClause(state, relation, tokens) {
|
|
153
|
-
if (tokens.some((token) => word(token, 'inherit') || word(token, 'partition'))) {
|
|
154
|
-
clearKeys(state, true);
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
if (word(tokens[0], 'validate') || tokens.some((token) => word(token, 'default') || word(token, 'check') || word(token, 'generated') || word(token, 'using'))) {
|
|
158
|
-
opaqueExecution(state);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
if (safeAlter(tokens))
|
|
162
|
-
return;
|
|
163
|
-
if (word(tokens[0], 'drop') && (word(tokens[1], 'column') || word(tokens[1], 'constraint'))) {
|
|
164
|
-
if (relation.schema === 'public')
|
|
165
|
-
state.tables.set(relation.table, []);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
if (!word(tokens[0], 'add')) {
|
|
169
|
-
forgetRelation(state, relation);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
let start = word(tokens[1], 'column') ? 2 : 1;
|
|
173
|
-
if (word(tokens[start], 'if'))
|
|
174
|
-
return;
|
|
175
|
-
if (relation.schema !== 'public' || !state.safe.has(relation.table))
|
|
176
|
-
return;
|
|
177
|
-
const keys = state.tables.get(relation.table) ?? [];
|
|
178
|
-
state.tables.set(relation.table, keys);
|
|
179
|
-
addKey(keys, declarationKey(tokens.slice(start)));
|
|
180
|
-
}
|
|
181
|
-
function alterTable(state, tokens, start) {
|
|
182
|
-
if (word(tokens[start], 'if') && word(tokens[start + 1], 'exists'))
|
|
183
|
-
start += 2;
|
|
184
|
-
if (word(tokens[start], 'only'))
|
|
185
|
-
start += 1;
|
|
186
|
-
const relation = relationAt(tokens, start, state.schema);
|
|
187
|
-
if (!relation || relation.schema === null) {
|
|
188
|
-
clearKeys(state, true);
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const clauses = clausesOf(tokens.slice(relation.next));
|
|
192
|
-
if (!clauses) {
|
|
193
|
-
forgetRelation(state, relation);
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
for (const clause of clauses)
|
|
197
|
-
alterClause(state, relation, clause);
|
|
198
|
-
}
|
|
199
|
-
function dropTable(state, tokens, start) {
|
|
200
|
-
if (word(tokens[start], 'if') && word(tokens[start + 1], 'exists'))
|
|
201
|
-
start += 2;
|
|
202
|
-
for (const clause of clausesOf(tokens.slice(start)) ?? [])
|
|
203
|
-
forgetRelation(state, relationAt(clause, 0, state.schema));
|
|
204
|
-
}
|
|
205
|
-
function searchPath(state, tokens) {
|
|
206
|
-
const local = word(tokens[1], 'local');
|
|
207
|
-
const start = local || word(tokens[1], 'session') ? 2 : 1;
|
|
208
|
-
if (word(tokens[start], 'schema')) {
|
|
209
|
-
const named = tokens[start + 1];
|
|
210
|
-
state.schema = !local && named?.t === 'string' ? named.v : null;
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
if (!settingName(tokens[start], 'search_path')) {
|
|
214
|
-
if (tokens.some((token) => settingName(token, 'search_path')) || word(tokens[start], 'all'))
|
|
215
|
-
state.schema = null;
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
if (word(tokens[0], 'reset')) {
|
|
219
|
-
state.schema = 'public';
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
if (!word(tokens[start + 1], 'to') && tokens[start + 1]?.v !== '=') {
|
|
223
|
-
state.schema = null;
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
const value = tokens[start + 2];
|
|
227
|
-
const declared = identifier(value) ?? (value?.t === 'string' ? value.v : null);
|
|
228
|
-
state.schema = local ? null : declared;
|
|
229
|
-
}
|
|
230
|
-
function createObject(tokens) {
|
|
231
|
-
let start = 1;
|
|
232
|
-
if (word(tokens[start], 'or') && word(tokens[start + 1], 'replace'))
|
|
233
|
-
start += 2;
|
|
234
|
-
if (word(tokens[start], 'global') || word(tokens[start], 'local'))
|
|
235
|
-
start += 1;
|
|
236
|
-
const temporary = word(tokens[start], 'temp') || word(tokens[start], 'temporary');
|
|
237
|
-
if (temporary || word(tokens[start], 'unlogged'))
|
|
238
|
-
start += 1;
|
|
239
|
-
if (word(tokens[start], 'unique'))
|
|
240
|
-
start += 1;
|
|
241
|
-
return { start, temporary };
|
|
242
|
-
}
|
|
243
|
-
/** CTAS, materialization and index building can call user code; event triggers also affect later DDL. */
|
|
244
|
-
function executingCreate(state, tokens, start) {
|
|
245
|
-
if (word(tokens[start], 'event') && word(tokens[start + 1], 'trigger')) {
|
|
246
|
-
state.tainted = true;
|
|
247
|
-
opaqueExecution(state);
|
|
248
|
-
return true;
|
|
249
|
-
}
|
|
250
|
-
if (word(tokens[start], 'index') || (word(tokens[start], 'materialized') && word(tokens[start + 1], 'view'))) {
|
|
251
|
-
opaqueExecution(state);
|
|
252
|
-
return true;
|
|
253
|
-
}
|
|
254
|
-
if (!word(tokens[start], 'table'))
|
|
255
|
-
return false;
|
|
256
|
-
let depth = 0;
|
|
257
|
-
for (const token of tokens.slice(start + 1)) {
|
|
258
|
-
if (token.v === '(')
|
|
259
|
-
depth += 1;
|
|
260
|
-
if (token.v === ')')
|
|
261
|
-
depth -= 1;
|
|
262
|
-
if (depth === 0 && word(token, 'as')) {
|
|
263
|
-
opaqueExecution(state);
|
|
264
|
-
return true;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
return false;
|
|
268
|
-
}
|
|
269
|
-
function applyCreate(state, tokens) {
|
|
270
|
-
// A private or temporary child also contributes rows to a public parent's ordinary SELECT.
|
|
271
|
-
if (tokens.some((token) => word(token, 'inherits'))) {
|
|
272
|
-
clearKeys(state, true);
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
const { start, temporary } = createObject(tokens);
|
|
276
|
-
if (executingCreate(state, tokens, start))
|
|
277
|
-
return;
|
|
278
|
-
if (temporary) {
|
|
279
|
-
opaqueExecution(state);
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
if (word(tokens[start], 'table'))
|
|
283
|
-
createTable(state, tokens, start + 1);
|
|
284
|
-
}
|
|
285
|
-
function applyTableCommand(state, tokens, command) {
|
|
286
|
-
if (word(tokens[1], 'table'))
|
|
287
|
-
command(state, tokens, 2);
|
|
288
|
-
else
|
|
289
|
-
clearKeys(state, true);
|
|
290
|
-
}
|
|
291
|
-
/** Reduce only unconditional schema declarations; executable bodies may remove facts but never manufacture them. */
|
|
292
|
-
function applyStatement(state, tokens) {
|
|
293
|
-
if (state.tainted)
|
|
294
|
-
return;
|
|
295
|
-
if (word(tokens[0], 'set') || word(tokens[0], 'reset')) {
|
|
296
|
-
searchPath(state, tokens);
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
if (tokens[0]?.t === 'word' && OPAQUE_STATEMENTS.has(tokens[0].v)) {
|
|
300
|
-
opaqueExecution(state);
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
if (word(tokens[0], 'commit') && word(tokens[1], 'prepared')) {
|
|
304
|
-
opaqueExecution(state);
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
if (word(tokens[0], 'create')) {
|
|
308
|
-
applyCreate(state, tokens);
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
if (word(tokens[0], 'alter')) {
|
|
312
|
-
applyTableCommand(state, tokens, alterTable);
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
if (word(tokens[0], 'drop'))
|
|
316
|
-
applyTableCommand(state, tokens, dropTable);
|
|
317
|
-
}
|
|
318
|
-
function activeMigrations(root) {
|
|
319
|
-
const listed = lsFiles(['-z', '--cached', '--exclude-standard', '--', MIGRATIONS_DIR], { dir: root, maxBuffer: 256 * 1024 * 1024 });
|
|
320
|
-
if (listed.error || listed.status !== 0)
|
|
321
|
-
return null;
|
|
322
|
-
return listed.stdout.split('\0').filter((file) => path.posix.dirname(file) === MIGRATIONS_DIR
|
|
323
|
-
&& isMigrationName(path.posix.basename(file)) && migrationStamp(path.posix.basename(file).slice(0, 14)) !== null).sort(byCodeUnit);
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Public-table key sets proven by ordered, tracked active migrations. Direct event triggers taint later DDL proof.
|
|
327
|
-
* Opaque bodies discard current facts; persistent schema machinery hidden inside them is outside static authority.
|
|
328
|
-
*/
|
|
329
|
-
export function sqlUniqueSetsOf(appRoot) {
|
|
330
|
-
const root = path.resolve(appRoot);
|
|
331
|
-
const files = activeMigrations(root);
|
|
332
|
-
const state = { tables: new Map(), safe: new Set(), schema: 'public', tainted: false };
|
|
333
|
-
for (const file of files ?? []) {
|
|
334
|
-
const text = readText(root, file);
|
|
335
|
-
if (text === null) {
|
|
336
|
-
clearKeys(state, true);
|
|
337
|
-
state.schema = null;
|
|
338
|
-
continue;
|
|
339
|
-
}
|
|
340
|
-
for (const statement of databaseSqlStatements(databaseSqlTokens(text)))
|
|
341
|
-
applyStatement(state, statement);
|
|
342
|
-
}
|
|
343
|
-
return state.tainted ? new Map() : state.tables;
|
|
344
|
-
}
|
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { analyzeSql, HOLE } from './sql-tokens.mjs';
|
|
3
|
-
import { machineKit } from './machine-ast.mjs';
|
|
4
|
-
import { contextModelOf } from './context-map.mjs';
|
|
5
|
-
import { sqlUniqueSetsOf } from './sql-keys.mjs';
|
|
6
|
-
/**
|
|
7
|
-
* R86 `sql-owner` (BE_SQL_TABLE_OWNER). Every `sql` tagged template (the tag declared in platform/database) of a
|
|
8
|
-
* `<name>.sql.ts` file in a capability's `persistence/` is read with the tokenizer of sql-tokens.mjs and judged against the
|
|
9
|
-
* schema authority of the whole program:
|
|
10
|
-
* - it writes (INSERT INTO, UPDATE, DELETE FROM, MERGE INTO, TRUNCATE) only tables the file's own capability declares;
|
|
11
|
-
* - it reads (FROM, JOIN, USING) only tables of its own CONTEXT (the connection its capability's arrays are registered on): its own
|
|
12
|
-
* capability's, or those of a same-context capability its owner may import (the tier matrix, through `resolver.importAllowed`
|
|
13
|
-
* to the table owner's public entry); a table of another context is refused (cross-context data comes only by events);
|
|
14
|
-
* - every table it names is declared by some entity under TypeORM authority, or by the generated
|
|
15
|
-
* public tables the ordered db/migrations create under the SQL migration authority;
|
|
16
|
-
* - a SELECT carries LIMIT, or reads a single relation under a complete primary/unique-key equality to scalar values,
|
|
17
|
-
* or selects only proven scalar aggregates without GROUP BY. Inferred bounds reject multiplying projections and set operations.
|
|
18
|
-
* Table positions that hold a `${...}` substitution (SqlIdent) are dynamic; they are counted in the coverage and judged by
|
|
19
|
-
* nobody. A join needs an explicit result bound because key constraints on its first relation do not bound joined rows.
|
|
20
|
-
*/
|
|
21
|
-
export const SQL_OWNER_RULE_IDS = ['BE_SQL_TABLE_OWNER'];
|
|
22
|
-
const RULE = 'BE_SQL_TABLE_OWNER';
|
|
23
|
-
const TYPEORM = 'typeorm';
|
|
24
|
-
const hasOwner = (file) => Boolean(file.owner);
|
|
25
|
-
const literalStrings = (ts, node) => {
|
|
26
|
-
if (!node)
|
|
27
|
-
return null;
|
|
28
|
-
if (ts.isStringLiteralLike(node))
|
|
29
|
-
return [node.text];
|
|
30
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
31
|
-
const literals = node.elements.filter((item) => ts.isStringLiteralLike(item));
|
|
32
|
-
if (literals.length === node.elements.length)
|
|
33
|
-
return literals.map(item => item.text);
|
|
34
|
-
}
|
|
35
|
-
return null;
|
|
36
|
-
};
|
|
37
|
-
function isTypeormDecorator(kit, checker, node, ...names) {
|
|
38
|
-
const call = kit.ts.isCallExpression(node.expression) ? node.expression.expression : node.expression;
|
|
39
|
-
const binding = kit.importBinding(checker, call);
|
|
40
|
-
return binding?.module === TYPEORM && names.includes(binding.name);
|
|
41
|
-
}
|
|
42
|
-
/** Whether an object literal carries `unique: true`. */
|
|
43
|
-
function isUniqueTrue(kit, item) {
|
|
44
|
-
if (!kit.ts.isObjectLiteralExpression(item))
|
|
45
|
-
return false;
|
|
46
|
-
const unique = kit.propertyOf(item, 'unique');
|
|
47
|
-
return unique !== undefined && kit.valueOfProperty(unique).kind === kit.ts.SyntaxKind.TrueKeyword;
|
|
48
|
-
}
|
|
49
|
-
function entityTableInfo(kit, checker, statement) {
|
|
50
|
-
const { ts } = kit;
|
|
51
|
-
const entity = kit.decorators(statement).find(decorator => isTypeormDecorator(kit, checker, decorator, 'Entity'));
|
|
52
|
-
if (!entity)
|
|
53
|
-
return null;
|
|
54
|
-
const argument = ts.isCallExpression(entity.expression) ? entity.expression.arguments[0] ?? null : null;
|
|
55
|
-
let table = argument && ts.isStringLiteralLike(argument) ? argument.text : null;
|
|
56
|
-
if (table === null && argument && ts.isObjectLiteralExpression(argument)) {
|
|
57
|
-
const property = kit.propertyOf(argument, 'name');
|
|
58
|
-
const value = property ? kit.valueOfProperty(property) : null;
|
|
59
|
-
if (value && ts.isStringLiteralLike(value))
|
|
60
|
-
table = value.text;
|
|
61
|
-
}
|
|
62
|
-
return { table };
|
|
63
|
-
}
|
|
64
|
-
function mappedColumnName(kit, member, property) {
|
|
65
|
-
const { ts } = kit;
|
|
66
|
-
let column = property;
|
|
67
|
-
for (const decorator of kit.decorators(member)) {
|
|
68
|
-
const args = ts.isCallExpression(decorator.expression) ? decorator.expression.arguments : [];
|
|
69
|
-
for (const item of args)
|
|
70
|
-
if (ts.isObjectLiteralExpression(item)) {
|
|
71
|
-
const named = kit.propertyOf(item, 'name');
|
|
72
|
-
const value = named ? kit.valueOfProperty(named) : null;
|
|
73
|
-
if (value && ts.isStringLiteralLike(value))
|
|
74
|
-
column = value.text;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return column;
|
|
78
|
-
}
|
|
79
|
-
function addUniqueColumnSet(kit, checker, decorator, column, primary, sets) {
|
|
80
|
-
const { ts } = kit;
|
|
81
|
-
const args = ts.isCallExpression(decorator.expression) ? decorator.expression.arguments : [];
|
|
82
|
-
const uniqueOption = args.some(item => isUniqueTrue(kit, item));
|
|
83
|
-
if (isTypeormDecorator(kit, checker, decorator, 'PrimaryColumn', 'PrimaryGeneratedColumn'))
|
|
84
|
-
primary.push(column.toLowerCase());
|
|
85
|
-
else if (isTypeormDecorator(kit, checker, decorator, 'Column', 'Index') && uniqueOption)
|
|
86
|
-
sets.push([column.toLowerCase()]);
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* The members of an entity class with the checker of the file declaring each: those of every class it extends first (a column base
|
|
90
|
-
* such as UuidAbstractEntity of platform/database maps no table of its own but gives its columns and its primary key to the entity),
|
|
91
|
-
* then its own.
|
|
92
|
-
*/
|
|
93
|
-
function entityMembers(kit, checker, statement, seen = new Set()) {
|
|
94
|
-
const { ts } = kit;
|
|
95
|
-
if (seen.has(statement))
|
|
96
|
-
return [];
|
|
97
|
-
seen.add(statement);
|
|
98
|
-
const bases = (statement.heritageClauses ?? []).filter(clause => clause.token === ts.SyntaxKind.ExtendsKeyword)
|
|
99
|
-
.flatMap(clause => clause.types).flatMap(type => kit.declarationsOf(checker, type.expression)).filter((node) => ts.isClassDeclaration(node));
|
|
100
|
-
const inherited = bases.flatMap(base => entityMembers(kit, kit.checkerOf(base.getSourceFile()) ?? checker, base, seen));
|
|
101
|
-
return [...inherited, ...statement.members.map((member) => ({ member, checker }))];
|
|
102
|
-
}
|
|
103
|
-
function entityColumnSets(kit, checker, statement) {
|
|
104
|
-
const { ts } = kit;
|
|
105
|
-
const dbName = new Map();
|
|
106
|
-
const primary = [];
|
|
107
|
-
const sets = [];
|
|
108
|
-
for (const { member, checker: memberChecker } of entityMembers(kit, checker, statement)) {
|
|
109
|
-
if (!ts.isPropertyDeclaration(member) || !member.name || !ts.isIdentifier(member.name))
|
|
110
|
-
continue;
|
|
111
|
-
const property = member.name.text;
|
|
112
|
-
const column = mappedColumnName(kit, member, property);
|
|
113
|
-
dbName.set(property, column.toLowerCase());
|
|
114
|
-
for (const decorator of kit.decorators(member))
|
|
115
|
-
addUniqueColumnSet(kit, memberChecker, decorator, column, primary, sets);
|
|
116
|
-
}
|
|
117
|
-
if (primary.length)
|
|
118
|
-
sets.push(primary);
|
|
119
|
-
return { dbName, sets };
|
|
120
|
-
}
|
|
121
|
-
function addEntityUniqueSets(kit, checker, statement, dbName, sets) {
|
|
122
|
-
const { ts } = kit;
|
|
123
|
-
for (const decorator of kit.decorators(statement)) {
|
|
124
|
-
const args = ts.isCallExpression(decorator.expression) ? decorator.expression.arguments : [];
|
|
125
|
-
const list = args.find(item => ts.isArrayLiteralExpression(item) && literalStrings(ts, item));
|
|
126
|
-
const columns = list ? literalStrings(ts, list) : null;
|
|
127
|
-
const unique = isTypeormDecorator(kit, checker, decorator, 'Unique') || (isTypeormDecorator(kit, checker, decorator, 'Index') && args.some(item => isUniqueTrue(kit, item)));
|
|
128
|
-
if (unique && columns)
|
|
129
|
-
sets.push(columns.map(column => (dbName.get(column) ?? column).toLowerCase()));
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
/** Records the entities one `.entity.ts` file declares into `tables`; returns how many of its entities have no readable table name. */
|
|
133
|
-
function addFileEntities(kit, file, tables) {
|
|
134
|
-
const checker = kit.checkerOf(file.sourceFile);
|
|
135
|
-
let unreadable = 0;
|
|
136
|
-
for (const statement of file.sourceFile.statements) {
|
|
137
|
-
if (!kit.ts.isClassDeclaration(statement))
|
|
138
|
-
continue;
|
|
139
|
-
const entity = entityTableInfo(kit, checker, statement);
|
|
140
|
-
if (!entity)
|
|
141
|
-
continue;
|
|
142
|
-
if (entity.table === null) {
|
|
143
|
-
unreadable += 1;
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
const { dbName, sets } = entityColumnSets(kit, checker, statement);
|
|
147
|
-
addEntityUniqueSets(kit, checker, statement, dbName, sets);
|
|
148
|
-
const key = entity.table.toLowerCase();
|
|
149
|
-
if (!tables.has(key))
|
|
150
|
-
tables.set(key, { table: entity.table, owner: file.owner.root, rel: file.rel, uniqueSets: sets });
|
|
151
|
-
}
|
|
152
|
-
return unreadable;
|
|
153
|
-
}
|
|
154
|
-
export function entitiesOf(kit, graph) {
|
|
155
|
-
const tables = new Map();
|
|
156
|
-
let unreadable = 0;
|
|
157
|
-
for (const file of graph.files.values()) {
|
|
158
|
-
if (file.slot !== 'be.persistence' || !path.posix.basename(file.rel).endsWith('.entity.ts') || !hasOwner(file))
|
|
159
|
-
continue;
|
|
160
|
-
unreadable += addFileEntities(kit, file, tables);
|
|
161
|
-
}
|
|
162
|
-
return { tables, unreadable };
|
|
163
|
-
}
|
|
164
|
-
const capabilityTable = (root) => path.posix.basename(root).replaceAll('-', '_').toLowerCase();
|
|
165
|
-
/** SQL-authority declarations (the public tables db/migrations create), with an owner when a table name matches a capability's persistence folder. */
|
|
166
|
-
function sqlDeclarations(graph, appRoot) {
|
|
167
|
-
const keys = sqlUniqueSetsOf(appRoot);
|
|
168
|
-
const tables = new Map([...keys.keys()].map((table) => [table, { table, owner: null, rel: null, uniqueSets: keys.get(table) ?? [] }]));
|
|
169
|
-
for (const file of graph.files.values()) {
|
|
170
|
-
if (file.slot !== 'be.persistence' || !file.owner)
|
|
171
|
-
continue;
|
|
172
|
-
const key = capabilityTable(file.owner.root);
|
|
173
|
-
const declaration = tables.get(key);
|
|
174
|
-
if (declaration?.owner === null)
|
|
175
|
-
tables.set(key, { ...declaration, owner: file.owner.root, rel: file.rel });
|
|
176
|
-
}
|
|
177
|
-
return tables;
|
|
178
|
-
}
|
|
179
|
-
/** A capability owns the table named for it and the tables that start with its name and an underscore (`realtime`, `realtime_events`). */
|
|
180
|
-
const isOwnTable = (table, ownTable) => {
|
|
181
|
-
const name = table.toLowerCase();
|
|
182
|
-
return name === ownTable || name.startsWith(`${ownTable}_`);
|
|
183
|
-
};
|
|
184
|
-
function reportSqlWrite({ tables, sqlAuthority, file, ownTable, report }, write) {
|
|
185
|
-
const declaration = tables.get(write.table.toLowerCase());
|
|
186
|
-
if (!declaration) {
|
|
187
|
-
report(sqlAuthority
|
|
188
|
-
? `SQL writes table ${write.table}, which no db/migrations migration creates; add the table in a migration first.`
|
|
189
|
-
: `SQL writes table ${write.table}, which no @Entity declares; declare the entity in the owning capability's persistence/entities.`, { table: write.table });
|
|
190
|
-
}
|
|
191
|
-
else if (sqlAuthority) {
|
|
192
|
-
if (isOwnTable(write.table, ownTable) && (declaration.owner === null || declaration.owner === file.owner.root))
|
|
193
|
-
return;
|
|
194
|
-
const ownerNote = declaration.owner ? `, owned by ${declaration.owner}` : '';
|
|
195
|
-
report(`SQL in ${file.owner.root} writes table ${write.table}${ownerNote}; under the SQL migration authority a capability writes only its own table (${ownTable}), named for that capability.`, { table: write.table, ...(declaration.owner ? { tableOwner: declaration.owner } : {}) });
|
|
196
|
-
}
|
|
197
|
-
else if (declaration.owner !== file.owner.root) {
|
|
198
|
-
report(`SQL in ${file.owner.root} writes table ${write.table}, owned by ${declaration.owner}; a capability writes only the tables of its own entities, call the owner's public API instead.`, { table: write.table, tableOwner: declaration.owner });
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
function reportSqlWrites(input) {
|
|
202
|
-
for (const write of input.result.writes)
|
|
203
|
-
reportSqlWrite(input, write);
|
|
204
|
-
}
|
|
205
|
-
function reportSqlRead(tables, sqlAuthority, file, model, resolver, report, read) {
|
|
206
|
-
const declaration = tables.get(read.table.toLowerCase());
|
|
207
|
-
if (!declaration) {
|
|
208
|
-
report(sqlAuthority
|
|
209
|
-
? `SQL reads table ${read.table}, which no db/migrations migration creates; add the table in a migration first.`
|
|
210
|
-
: `SQL reads table ${read.table}, which no @Entity declares; declare the entity in the owning capability's persistence/entities.`, { table: read.table });
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
if (!declaration.owner || declaration.owner === file.owner.root)
|
|
214
|
-
return;
|
|
215
|
-
const ownContext = model.contextOfCapability(file.owner.root);
|
|
216
|
-
const tableContext = model.contextOfCapability(declaration.owner);
|
|
217
|
-
if (ownContext && tableContext && ownContext !== tableContext) {
|
|
218
|
-
report(`SQL in ${file.owner.root} (context ${ownContext}) reads table ${read.table}, owned by ${declaration.owner} (context ${tableContext}); a context reads and writes only its own context's tables, so a cross-context JOIN or subquery is refused. Keep a local copy fed by the other context's events (a reactor), or read a projection.`, { table: read.table, tableOwner: declaration.owner, context: ownContext, tableContext });
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
const verdict = resolver.importAllowed(file.rel, `${declaration.owner}/index.ts`);
|
|
222
|
-
if (!verdict.allowed)
|
|
223
|
-
report(`SQL in ${file.owner.root} reads table ${read.table}, owned by ${declaration.owner}, which ${file.owner.root} may not import (${verdict.reason}); read it through the owner's public API.`, { table: read.table, tableOwner: declaration.owner, reason: verdict.reason });
|
|
224
|
-
}
|
|
225
|
-
function reportSqlReads({ result, tables, sqlAuthority, file, model, resolver, report }) {
|
|
226
|
-
for (const read of result.reads)
|
|
227
|
-
reportSqlRead(tables, sqlAuthority, file, model, resolver, report, read);
|
|
228
|
-
}
|
|
229
|
-
function reportSqlSelects({ result, tables, report }) {
|
|
230
|
-
for (const select of result.selects) {
|
|
231
|
-
if (select.bounded || (select.table && !tables.has(select.table.toLowerCase())))
|
|
232
|
-
continue;
|
|
233
|
-
const tableNote = select.table ? `on ${select.table} ` : '';
|
|
234
|
-
report(`SELECT ${tableNote}has no LIMIT and does not constrain a primary key or unique column with =; bound it with LIMIT (PAGE_SIZE_MAX, LIST_ROWS_MAX or BATCH_ROWS from platform/database), filter by key, or select only aggregates.`, { table: select.table ?? undefined });
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
function inspectSqlTag(input, state, file, checker, node) {
|
|
238
|
-
const { kit, ts, tables, sqlAuthority, model, resolver } = input;
|
|
239
|
-
if (!ts.isTaggedTemplateExpression(node))
|
|
240
|
-
return true;
|
|
241
|
-
const home = kit.declarationsOf(checker, node.tag).map(kit.ownerOfDeclaration).find(Boolean);
|
|
242
|
-
if (home?.tier !== 'platform' || home?.name !== 'database') {
|
|
243
|
-
state.foreignTags += 1;
|
|
244
|
-
return true;
|
|
245
|
-
}
|
|
246
|
-
state.templates += 1;
|
|
247
|
-
const template = node.template;
|
|
248
|
-
const text = ts.isNoSubstitutionTemplateLiteral(template) ? template.text
|
|
249
|
-
: template.head.text + template.templateSpans.map(span => HOLE + span.literal.text).join('');
|
|
250
|
-
const seen = new Set();
|
|
251
|
-
const report = (message, extra = {}) => {
|
|
252
|
-
const key = `${message}`;
|
|
253
|
-
if (seen.has(key))
|
|
254
|
-
return;
|
|
255
|
-
seen.add(key);
|
|
256
|
-
state.violations.push({ ruleId: RULE, path: file.rel, ...kit.at(file.rel, file.sourceFile, node), message, ...extra });
|
|
257
|
-
};
|
|
258
|
-
const result = analyzeSql(text, { uniqueSetsOf: (table, schema) => {
|
|
259
|
-
if (sqlAuthority && ((schema && schema !== 'public') || table !== table.toLowerCase()))
|
|
260
|
-
return null;
|
|
261
|
-
return tables.get(table.toLowerCase())?.uniqueSets ?? null;
|
|
262
|
-
} });
|
|
263
|
-
state.dynamic += result.dynamic;
|
|
264
|
-
const ownTable = capabilityTable(file.owner.root);
|
|
265
|
-
reportSqlWrites({ result, tables, sqlAuthority, file, ownTable, report });
|
|
266
|
-
reportSqlReads({ result, tables, sqlAuthority, file, model, resolver, report });
|
|
267
|
-
reportSqlSelects({ result, tables, report });
|
|
268
|
-
return true;
|
|
269
|
-
}
|
|
270
|
-
function inspectSqlFile(input, state, file) {
|
|
271
|
-
const { kit } = input;
|
|
272
|
-
if (file.slot !== 'be.persistence' || !path.posix.basename(file.rel).endsWith('.sql.ts') || !hasOwner(file))
|
|
273
|
-
return;
|
|
274
|
-
const checker = kit.checkerOf(file.sourceFile);
|
|
275
|
-
kit.walk(file.sourceFile, (node) => inspectSqlTag(input, state, file, checker, node));
|
|
276
|
-
}
|
|
277
|
-
export function checkSqlOwner(input) {
|
|
278
|
-
const { config, graph } = input;
|
|
279
|
-
const kit = machineKit(input);
|
|
280
|
-
const { ts, resolver } = kit;
|
|
281
|
-
const sqlAuthority = resolver.ruleParams()?.schemaAuthority === 'sql';
|
|
282
|
-
const entityDeclarations = sqlAuthority ? { tables: new Map(), unreadable: 0 } : entitiesOf(kit, graph);
|
|
283
|
-
const tables = sqlAuthority ? sqlDeclarations(graph, config.packageRoot) : entityDeclarations.tables;
|
|
284
|
-
const model = contextModelOf(kit, graph);
|
|
285
|
-
const state = { violations: [], templates: 0, dynamic: 0, foreignTags: 0 };
|
|
286
|
-
const inspection = { kit, ts, tables, sqlAuthority, model, resolver };
|
|
287
|
-
for (const file of graph.files.values())
|
|
288
|
-
inspectSqlFile(inspection, state, file);
|
|
289
|
-
return { violations: state.violations, coverage: { status: 'checked', entities: entityDeclarations.tables.size, unreadableEntities: entityDeclarations.unreadable,
|
|
290
|
-
...(sqlAuthority ? { sqlTables: tables.size } : {}), templates: state.templates, dynamicIdentifiers: state.dynamic, foreignTags: state.foreignTags } };
|
|
291
|
-
}
|