@starci/eslint-canon-be 3.8.19 → 3.9.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 +12 -0
- package/package.json +1 -1
- package/runtime/bundle.json +192 -0
- package/runtime/engine/by-code-unit.mjs +6 -6
- package/runtime/engine/model-config.mjs +98 -76
- package/runtime/engine/orca-config.mjs +22 -17
- package/runtime/engine/release-config.mjs +22 -18
- package/runtime/engine/removed-vocabulary.mjs +21 -20
- package/runtime/engine/resources-config.mjs +42 -35
- package/runtime/engine/runtime-root.mjs +11 -13
- package/runtime/engine/secrets.mjs +170 -102
- package/runtime/engine/sonar-config.mjs +16 -13
- package/runtime/engine/temp-root.mjs +3 -9
- package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
- package/runtime/knowledge/hfs/rules.yaml +6 -6
- package/runtime/knowledge/hfs/slots.yaml +2 -2
- package/runtime/knowledge/sonar-gate.yaml +1 -1
- package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
- package/runtime/scripts/api/git/cat-file.mjs +0 -1
- package/runtime/scripts/api/git/config-get.mjs +1 -2
- package/runtime/scripts/api/git/diff.mjs +0 -1
- package/runtime/scripts/api/git/lib.mjs +32 -32
- package/runtime/scripts/api/git/ls-files.mjs +0 -1
- package/runtime/scripts/api/git/ls-tree.mjs +0 -1
- package/runtime/scripts/api/git/merge-base.mjs +2 -3
- package/runtime/scripts/api/git/remote.mjs +0 -1
- package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
- package/runtime/scripts/api/git/show.mjs +0 -1
- package/runtime/scripts/hfs/allows.mjs +37 -38
- package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
- package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
- package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
- package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
- package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
- package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
- package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
- package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
- package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
- package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
- package/runtime/scripts/hfs/architecture/config.mjs +302 -268
- package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
- package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
- package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
- package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
- package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
- package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
- package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
- package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
- package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
- package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
- package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
- package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
- package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
- package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
- package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
- package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
- package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
- package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
- package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
- package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
- package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
- package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
- package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
- package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
- package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
- package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
- package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
- package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
- package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
- package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
- package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
- package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
- package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
- package/runtime/scripts/hfs/architecture/index.mjs +272 -272
- package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
- package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
- package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
- package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
- package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
- package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
- package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
- package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
- package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
- package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
- package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
- package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
- package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
- package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
- package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
- package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
- package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
- package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
- package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
- package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
- package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
- package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
- package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
- package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
- package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
- package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
- package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
- package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
- package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
- package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
- package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
- package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
- package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
- package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
- package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
- package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
- package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
- package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
- package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
- package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
- package/runtime/scripts/hfs/edition-slots.mjs +43 -40
- package/runtime/scripts/hfs/linear-text.mjs +20 -20
- package/runtime/scripts/hfs/param-names.mjs +6 -11
- package/runtime/scripts/hfs/path-findings.mjs +120 -100
- package/runtime/scripts/hfs/repo-identity.mjs +55 -42
- package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
- package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
- package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
- package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
- package/runtime/scripts/hfs/rules/read.mjs +19 -15
- package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
- package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
- package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
- package/runtime/scripts/hfs/slot-classify.mjs +42 -38
- package/runtime/scripts/hfs/slot-errors.mjs +8 -7
- package/runtime/scripts/hfs/slot-imports.mjs +70 -63
- package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
- package/runtime/scripts/hfs/slot-match.mjs +122 -111
- package/runtime/scripts/hfs/slot-path.mjs +0 -2
- package/runtime/scripts/hfs/slot-required.mjs +65 -59
- package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
- package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
- package/runtime/scripts/hfs/slots.mjs +175 -152
- package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
- package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
- package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
- package/runtime/scripts/hfs/view.mjs +38 -40
- package/runtime/scripts/lib/dockerfile.mjs +131 -114
- package/runtime/scripts/lib/env.mjs +3 -6
- package/runtime/scripts/lib/event-contract.mjs +73 -78
- package/runtime/scripts/lib/git.mjs +15 -16
- package/runtime/scripts/lib/glob.mjs +23 -14
- package/runtime/scripts/lib/grammar-lint.mjs +33 -37
- package/runtime/scripts/lib/i18n.mjs +32 -35
- package/runtime/scripts/lib/in-order.mjs +57 -38
- package/runtime/scripts/lib/json-clone.mjs +45 -36
- package/runtime/scripts/lib/json.mjs +26 -14
- package/runtime/scripts/lib/language.mjs +133 -129
- package/runtime/scripts/lib/list.mjs +9 -14
- package/runtime/scripts/lib/mutation-fence.mjs +10 -5
- package/runtime/scripts/lib/path-key.mjs +53 -38
- package/runtime/scripts/lib/read-text.mjs +30 -17
- package/runtime/scripts/lib/regex.mjs +1 -2
- package/runtime/scripts/lib/same-text.mjs +0 -1
- package/runtime/scripts/lib/sops-envelope.mjs +148 -120
- package/runtime/scripts/lib/stack-services.mjs +128 -129
- package/runtime/scripts/lib/ts-analysis.mjs +28 -26
- package/runtime/scripts/lib/ts-ast.mjs +36 -42
- package/runtime/scripts/lib/walk.mjs +33 -27
- package/runtime/scripts/lib/yaml-cached.mjs +4 -4
|
@@ -4,258 +4,341 @@ import { byCodeUnit } from '../../lib/list.mjs';
|
|
|
4
4
|
import { MIGRATIONS_DIR } from '../rules/database-constants.mjs';
|
|
5
5
|
import { databaseSqlStatements, databaseSqlTokens, isMigrationName, migrationStamp } from '../rules/database-migrations.mjs';
|
|
6
6
|
import { readText } from '../rules/read.mjs';
|
|
7
|
-
|
|
8
7
|
const word = (token, value) => token?.t === 'word' && token.v === value;
|
|
9
|
-
const identifier = (token) => ['word', 'ident'].includes(token
|
|
8
|
+
const identifier = (token) => token !== undefined && ['word', 'ident'].includes(token.t) && token.v === token.v.toLowerCase() ? token.v : null;
|
|
10
9
|
const OPAQUE_STATEMENTS = new Set(['do', 'call', 'select', 'with', 'insert', 'update', 'delete', 'merge', 'truncate', 'execute', 'rollback', 'abort', 'prepare', 'discard', 'refresh', 'copy']);
|
|
11
|
-
const settingName = (token, value) => ['word', 'ident'].includes(token
|
|
12
|
-
|
|
10
|
+
const settingName = (token, value) => token !== undefined && ['word', 'ident'].includes(token.t) && token.v.toLowerCase() === value;
|
|
13
11
|
/** A migration relation is public only when its qualification or the current search path proves it. */
|
|
14
12
|
function relationAt(tokens, start, schema) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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 };
|
|
21
22
|
}
|
|
22
|
-
|
|
23
23
|
/** Split declarations at their own commas, keeping type arguments and constraint column lists together. */
|
|
24
24
|
function clausesOf(tokens) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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;
|
|
35
43
|
}
|
|
36
|
-
|
|
37
44
|
function parenthesized(tokens, start) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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;
|
|
46
57
|
}
|
|
47
|
-
|
|
48
58
|
const isDeferred = (tokens) => tokens.some((token, index) => word(token, 'deferrable') && !word(tokens[index - 1], 'not'));
|
|
49
|
-
|
|
50
59
|
/** A declared key proves only its complete, plain column list; index expressions are never column names. */
|
|
51
60
|
function tableKey(tokens) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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]; });
|
|
61
76
|
}
|
|
62
|
-
|
|
63
77
|
/** Inline key clauses are read at column depth, outside defaults, generated expressions and type parameters. */
|
|
64
78
|
function columnKey(tokens) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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;
|
|
74
92
|
}
|
|
75
|
-
|
|
76
93
|
function declarationKey(tokens) {
|
|
77
|
-
|
|
78
|
-
|
|
94
|
+
const tableConstraint = ['constraint', 'primary', 'unique', 'check', 'foreign', 'exclude'].some((value) => word(tokens[0], value));
|
|
95
|
+
return tableConstraint ? tableKey(tokens) : columnKey(tokens);
|
|
79
96
|
}
|
|
80
|
-
|
|
81
97
|
function addKey(keys, columns) {
|
|
82
|
-
|
|
98
|
+
if (columns && !keys.some((key) => key.length === columns.length && key.every((column, index) => column === columns[index])))
|
|
99
|
+
keys.push(columns);
|
|
83
100
|
}
|
|
84
|
-
|
|
85
101
|
function clearKeys(state, uncertain = false) {
|
|
86
|
-
|
|
87
|
-
|
|
102
|
+
for (const table of state.tables.keys())
|
|
103
|
+
state.tables.set(table, []);
|
|
104
|
+
if (uncertain)
|
|
105
|
+
state.safe.clear();
|
|
88
106
|
}
|
|
89
|
-
|
|
90
107
|
function opaqueExecution(state) {
|
|
91
|
-
|
|
92
|
-
|
|
108
|
+
clearKeys(state, true);
|
|
109
|
+
state.schema = null;
|
|
93
110
|
}
|
|
94
|
-
|
|
95
111
|
function forgetRelation(state, relation) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
+
}
|
|
101
118
|
}
|
|
102
|
-
|
|
103
119
|
/** Conditional table creation cannot establish what an unknown pre-existing table actually constrains. */
|
|
104
120
|
function createTable(state, tokens, start) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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));
|
|
121
142
|
}
|
|
122
|
-
|
|
123
143
|
function safeAlter(tokens) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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 === '(';
|
|
128
150
|
}
|
|
129
|
-
|
|
130
151
|
/** ADD can establish a new key; drops, renames and unreadable alterations discard the affected proof. */
|
|
131
152
|
function alterClause(state, relation, tokens) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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)));
|
|
149
180
|
}
|
|
150
|
-
|
|
151
181
|
function alterTable(state, tokens, start) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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);
|
|
159
198
|
}
|
|
160
|
-
|
|
161
199
|
function dropTable(state, tokens, start) {
|
|
162
|
-
|
|
163
|
-
|
|
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));
|
|
164
204
|
}
|
|
165
|
-
|
|
166
205
|
function searchPath(state, tokens) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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;
|
|
182
229
|
}
|
|
183
|
-
|
|
184
230
|
function createObject(tokens) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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 };
|
|
192
242
|
}
|
|
193
|
-
|
|
194
243
|
/** CTAS, materialization and index building can call user code; event triggers also affect later DDL. */
|
|
195
244
|
function executingCreate(state, tokens, start) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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;
|
|
213
268
|
}
|
|
214
|
-
|
|
215
269
|
function applyCreate(state, tokens) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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);
|
|
222
284
|
}
|
|
223
|
-
|
|
224
285
|
function applyTableCommand(state, tokens, command) {
|
|
225
|
-
|
|
226
|
-
|
|
286
|
+
if (word(tokens[1], 'table'))
|
|
287
|
+
command(state, tokens, 2);
|
|
288
|
+
else
|
|
289
|
+
clearKeys(state, true);
|
|
227
290
|
}
|
|
228
|
-
|
|
229
291
|
/** Reduce only unconditional schema declarations; executable bodies may remove facts but never manufacture them. */
|
|
230
292
|
function applyStatement(state, tokens) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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);
|
|
238
317
|
}
|
|
239
|
-
|
|
240
318
|
function activeMigrations(root) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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);
|
|
245
324
|
}
|
|
246
|
-
|
|
247
325
|
/**
|
|
248
326
|
* Public-table key sets proven by ordered, tracked active migrations. Direct event triggers taint later DDL proof.
|
|
249
327
|
* Opaque bodies discard current facts; persistent schema machinery hidden inside them is outside static authority.
|
|
250
328
|
*/
|
|
251
329
|
export function sqlUniqueSetsOf(appRoot) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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;
|
|
261
344
|
}
|