@pgpmjs/slice 0.10.2 → 0.11.2
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/esm/exclude.js +239 -0
- package/esm/index.js +1 -0
- package/exclude.d.ts +116 -0
- package/exclude.js +244 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +4 -4
package/esm/exclude.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { classifyStatements } from '@pgpmjs/transform';
|
|
2
|
+
import { parseSql } from 'plpgsql-parser';
|
|
3
|
+
function inSubsystem(ref, schemas) {
|
|
4
|
+
return ref.schema !== null && schemas.has(ref.schema);
|
|
5
|
+
}
|
|
6
|
+
function refKey(ref) {
|
|
7
|
+
return `${ref.schema ?? ''}.${ref.name}`;
|
|
8
|
+
}
|
|
9
|
+
function pushUnique(list, ref) {
|
|
10
|
+
if (!list.some(x => x.schema === ref.schema && x.name === ref.name))
|
|
11
|
+
list.push(ref);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The classifier does not tag a reference with its namespace, so resolution
|
|
15
|
+
* tries each object namespace (FK targets are known to be relations) before
|
|
16
|
+
* falling back to the schema-level default.
|
|
17
|
+
*/
|
|
18
|
+
function resolveRebind(router, ref, fk) {
|
|
19
|
+
if (!router)
|
|
20
|
+
return false;
|
|
21
|
+
const namespaces = fk
|
|
22
|
+
? ['relation']
|
|
23
|
+
: ['function', 'relation', 'type', 'unknown'];
|
|
24
|
+
for (const ns of namespaces) {
|
|
25
|
+
const target = router.resolveObject(ref.schema, ref.name, ns);
|
|
26
|
+
if (target !== undefined && (target.name !== undefined || target.schema !== undefined)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Decide statement membership: a statement belongs to the subsystem when
|
|
34
|
+
* everything it creates/targets is inside it, or (creating nothing) when all
|
|
35
|
+
* of its references point inside it (GRANTs on subsystem objects).
|
|
36
|
+
*/
|
|
37
|
+
function classifyMembership(facts, schemas) {
|
|
38
|
+
if (facts.kind === 'schema') {
|
|
39
|
+
const created = facts.creates[0];
|
|
40
|
+
return created && schemas.has(created.name) ? 'inside' : 'outside';
|
|
41
|
+
}
|
|
42
|
+
if (facts.creates.length > 0) {
|
|
43
|
+
const inside = facts.creates.filter(c => inSubsystem(c, schemas)).length;
|
|
44
|
+
if (inside === facts.creates.length)
|
|
45
|
+
return 'inside';
|
|
46
|
+
if (inside === 0)
|
|
47
|
+
return 'outside';
|
|
48
|
+
return 'mixed';
|
|
49
|
+
}
|
|
50
|
+
const refs = [...facts.references, ...facts.bodyReferences];
|
|
51
|
+
if (refs.length > 0) {
|
|
52
|
+
return refs.every(r => inSubsystem(r, schemas)) ? 'inside' : 'outside';
|
|
53
|
+
}
|
|
54
|
+
// No creates, no visible references: COMMENT ON, bare DROP, SET, ...
|
|
55
|
+
return 'opaque';
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Partition a SQL script into a subsystem (statements to exclude) and its
|
|
59
|
+
* survivors, measure the subsystem's external contract, and verify that a
|
|
60
|
+
* routing profile rebinds every surviving reference into it.
|
|
61
|
+
*
|
|
62
|
+
* Pure and I/O-free. The caller applies the actual removal/rewrite (e.g. via
|
|
63
|
+
* `transpileBundle`'s `transformScript` with the same router); this function
|
|
64
|
+
* only *decides* and *checks* — exclusion is safe iff `unsatisfied` is empty.
|
|
65
|
+
*/
|
|
66
|
+
export function excludeSubsystem(sql, selector, options = {}) {
|
|
67
|
+
const schemas = new Set(selector.schemas);
|
|
68
|
+
const statements = classifyStatements(sql);
|
|
69
|
+
const router = options.rebinds;
|
|
70
|
+
const excluded = [];
|
|
71
|
+
const kept = [];
|
|
72
|
+
const warnings = [];
|
|
73
|
+
const provides = [];
|
|
74
|
+
const internal = [];
|
|
75
|
+
const required = new Map();
|
|
76
|
+
const unsatisfied = [];
|
|
77
|
+
statements.forEach((facts, i) => {
|
|
78
|
+
const membership = classifyMembership(facts, schemas);
|
|
79
|
+
if (membership === 'inside') {
|
|
80
|
+
excluded.push(i);
|
|
81
|
+
for (const c of facts.creates) {
|
|
82
|
+
if (inSubsystem(c, schemas))
|
|
83
|
+
pushUnique(provides, { schema: c.schema, name: c.name });
|
|
84
|
+
}
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
kept.push(i);
|
|
88
|
+
if (membership === 'mixed') {
|
|
89
|
+
warnings.push({
|
|
90
|
+
kind: 'mixed-statement',
|
|
91
|
+
statement: i,
|
|
92
|
+
detail: `creates objects both inside and outside the subsystem: ${facts.creates
|
|
93
|
+
.map(refKey)
|
|
94
|
+
.join(', ')}`
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
else if (membership === 'opaque') {
|
|
98
|
+
warnings.push({
|
|
99
|
+
kind: 'opaque-statement',
|
|
100
|
+
statement: i,
|
|
101
|
+
detail: `${facts.nodeTag} target is not classified; verify it does not reference the subsystem`
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (facts.dynamicSql) {
|
|
105
|
+
warnings.push({
|
|
106
|
+
kind: 'dynamic-sql',
|
|
107
|
+
statement: i,
|
|
108
|
+
detail: 'kept statement executes dynamic SQL; references inside it are unchecked'
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const refs = new Map();
|
|
112
|
+
for (const r of [...facts.references, ...facts.bodyReferences]) {
|
|
113
|
+
if (inSubsystem(r, schemas))
|
|
114
|
+
refs.set(refKey(r), { ref: r, fk: refs.get(refKey(r))?.fk ?? false });
|
|
115
|
+
}
|
|
116
|
+
for (const t of facts.fkTargets) {
|
|
117
|
+
if (inSubsystem(t, schemas))
|
|
118
|
+
refs.set(refKey(t), { ref: t, fk: true });
|
|
119
|
+
}
|
|
120
|
+
for (const { ref, fk } of refs.values()) {
|
|
121
|
+
const key = refKey(ref);
|
|
122
|
+
const dep = required.get(key) ?? { object: { schema: ref.schema, name: ref.name }, fk: false, dependents: [] };
|
|
123
|
+
dep.fk = dep.fk || fk;
|
|
124
|
+
if (!dep.dependents.includes(i))
|
|
125
|
+
dep.dependents.push(i);
|
|
126
|
+
required.set(key, dep);
|
|
127
|
+
const rebound = resolveRebind(router, ref, fk);
|
|
128
|
+
if (!rebound && !unsatisfied.some(u => refKey(u.object) === key && u.statement === i)) {
|
|
129
|
+
unsatisfied.push({ object: { schema: ref.schema, name: ref.name }, statement: i, fk });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
for (const p of provides) {
|
|
134
|
+
if (!required.has(refKey(p)))
|
|
135
|
+
pushUnique(internal, p);
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
excluded,
|
|
139
|
+
kept,
|
|
140
|
+
contract: {
|
|
141
|
+
provides,
|
|
142
|
+
required: [...required.values()],
|
|
143
|
+
internal
|
|
144
|
+
},
|
|
145
|
+
unsatisfied,
|
|
146
|
+
warnings,
|
|
147
|
+
statements
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Blank a script: keep its leading text (pgpm header) and transaction
|
|
152
|
+
* statements, drop everything else. Used for the scripts of a change whose
|
|
153
|
+
* deploy is entirely inside an excluded subsystem — its verify/revert bodies
|
|
154
|
+
* target dropped objects, so they must go with it.
|
|
155
|
+
*/
|
|
156
|
+
export function blankScriptSql(sql) {
|
|
157
|
+
const parsed = parseSql(sql);
|
|
158
|
+
const prefix = parsed.stmts.length > 0 ? sql.slice(0, parsed.stmts[0].stmt_location ?? 0) : sql;
|
|
159
|
+
const pieces = [];
|
|
160
|
+
for (const s of parsed.stmts) {
|
|
161
|
+
if (!('TransactionStmt' in s.stmt))
|
|
162
|
+
continue;
|
|
163
|
+
const start = s.stmt_location ?? 0;
|
|
164
|
+
const len = s.stmt_len ?? sql.length - start;
|
|
165
|
+
pieces.push(sql.slice(start, start + len).trim() + ';');
|
|
166
|
+
}
|
|
167
|
+
return prefix + pieces.join('\n\n') + (pieces.length > 0 ? '\n' : '');
|
|
168
|
+
}
|
|
169
|
+
/** Qualified name (`[schema, name]` items) from a raw parse-tree name list. */
|
|
170
|
+
function qualifiedName(node) {
|
|
171
|
+
const items = node?.List
|
|
172
|
+
?.items;
|
|
173
|
+
if (!items)
|
|
174
|
+
return undefined;
|
|
175
|
+
const parts = items.map(x => x.String?.sval).filter((s) => typeof s === 'string');
|
|
176
|
+
if (parts.length === 2)
|
|
177
|
+
return { schema: parts[0], name: parts[1] };
|
|
178
|
+
if (parts.length === 1)
|
|
179
|
+
return { schema: null, name: parts[0] };
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Whether an opaque statement (invisible to classification) provably targets
|
|
184
|
+
* only subsystem objects, so it can be removed along with them: bare `DROP`
|
|
185
|
+
* of subsystem objects, `COMMENT ON` a subsystem object.
|
|
186
|
+
*/
|
|
187
|
+
function opaqueTargetsSubsystem(stmt, schemas) {
|
|
188
|
+
if (stmt.DropStmt?.objects) {
|
|
189
|
+
const objects = stmt.DropStmt.objects;
|
|
190
|
+
const refs = objects.map(qualifiedName);
|
|
191
|
+
return refs.length > 0 && refs.every(r => r !== undefined && inSubsystem(r, schemas));
|
|
192
|
+
}
|
|
193
|
+
if (stmt.CommentStmt?.object) {
|
|
194
|
+
const ref = qualifiedName(stmt.CommentStmt.object);
|
|
195
|
+
return ref !== undefined && inSubsystem(ref, schemas);
|
|
196
|
+
}
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Remove a subsystem's statements from a SQL script, preserving the original
|
|
201
|
+
* text of every survivor (no reformat — statements are sliced out of the
|
|
202
|
+
* source by parser-reported location). Also removes opaque statements that
|
|
203
|
+
* provably target only subsystem objects (`DROP`/`COMMENT ON` them), which
|
|
204
|
+
* membership classification alone keeps.
|
|
205
|
+
*
|
|
206
|
+
* Requires `await loadModule()` first (same as every other sync API here).
|
|
207
|
+
* This performs no safety check by itself — callers decide what to do with
|
|
208
|
+
* `result.unsatisfied` (typically: refuse before ever writing output).
|
|
209
|
+
*/
|
|
210
|
+
export function stripSubsystemSql(sql, selector, options = {}) {
|
|
211
|
+
const result = excludeSubsystem(sql, selector, options);
|
|
212
|
+
const schemas = new Set(selector.schemas);
|
|
213
|
+
const parsed = parseSql(sql);
|
|
214
|
+
if (parsed.stmts.length !== result.statements.length) {
|
|
215
|
+
throw new Error(`stripSubsystemSql: parser saw ${parsed.stmts.length} statements but the classifier saw ` +
|
|
216
|
+
`${result.statements.length}; refusing to slice by index`);
|
|
217
|
+
}
|
|
218
|
+
const drop = new Set(result.excluded);
|
|
219
|
+
for (const i of result.kept) {
|
|
220
|
+
if (opaqueTargetsSubsystem(parsed.stmts[i].stmt, schemas))
|
|
221
|
+
drop.add(i);
|
|
222
|
+
}
|
|
223
|
+
// Text before the first statement (pgpm headers, leading comments) is
|
|
224
|
+
// preserved verbatim so script identity headers survive the strip.
|
|
225
|
+
const prefix = parsed.stmts.length > 0 ? sql.slice(0, parsed.stmts[0].stmt_location ?? 0) : sql;
|
|
226
|
+
const pieces = [];
|
|
227
|
+
parsed.stmts.forEach((s, i) => {
|
|
228
|
+
if (drop.has(i))
|
|
229
|
+
return;
|
|
230
|
+
const start = s.stmt_location ?? 0;
|
|
231
|
+
const len = s.stmt_len ?? sql.length - start;
|
|
232
|
+
pieces.push(sql.slice(start, start + len).trim() + ';');
|
|
233
|
+
});
|
|
234
|
+
return {
|
|
235
|
+
sql: prefix + pieces.join('\n\n') + (pieces.length > 0 ? '\n' : ''),
|
|
236
|
+
result,
|
|
237
|
+
dropped: [...drop].sort((a, b) => a - b)
|
|
238
|
+
};
|
|
239
|
+
}
|
package/esm/index.js
CHANGED
package/exclude.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { SchemaRouter, StatementFacts } from '@pgpmjs/transform';
|
|
2
|
+
import { SqlObjectRef } from './refs';
|
|
3
|
+
/**
|
|
4
|
+
* Selects a *subsystem*: the set of objects a module owns that a consumer
|
|
5
|
+
* wants to remove and substitute with another provider. Selection is by
|
|
6
|
+
* schema — every object created in one of these schemas belongs to the
|
|
7
|
+
* subsystem, as do the schemas themselves.
|
|
8
|
+
*/
|
|
9
|
+
export interface SubsystemSelector {
|
|
10
|
+
schemas: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* One reference the surviving statements make *into* the subsystem. This is
|
|
14
|
+
* the subsystem's external contract: whatever replaces it must satisfy every
|
|
15
|
+
* one of these (or the consumer must rewrite the referencing statement).
|
|
16
|
+
*/
|
|
17
|
+
export interface SubsystemDependency {
|
|
18
|
+
/** The subsystem object being depended on. */
|
|
19
|
+
object: SqlObjectRef;
|
|
20
|
+
/** True when at least one dependent is a foreign-key constraint. */
|
|
21
|
+
fk: boolean;
|
|
22
|
+
/** Statement indexes (into the classified statement list) that depend on it. */
|
|
23
|
+
dependents: number[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The measured external contract of a subsystem: what the rest of the SQL
|
|
27
|
+
* actually requires of it, derived purely from the reference graph.
|
|
28
|
+
*/
|
|
29
|
+
export interface SubsystemContract {
|
|
30
|
+
/** Objects the subsystem creates. */
|
|
31
|
+
provides: SqlObjectRef[];
|
|
32
|
+
/** Subsystem objects referenced from outside — the replacement surface. */
|
|
33
|
+
required: SubsystemDependency[];
|
|
34
|
+
/** Subsystem objects nothing outside references — safe to drop silently. */
|
|
35
|
+
internal: SqlObjectRef[];
|
|
36
|
+
}
|
|
37
|
+
export interface ExcludeWarning {
|
|
38
|
+
/**
|
|
39
|
+
* - `mixed-statement`: a statement creates objects both inside and outside
|
|
40
|
+
* the subsystem, so it can be neither dropped nor kept cleanly.
|
|
41
|
+
* - `opaque-statement`: a statement's target is invisible to classification
|
|
42
|
+
* (e.g. `COMMENT ON`, bare `DROP`), so it is kept but may reference a
|
|
43
|
+
* dropped object.
|
|
44
|
+
* - `dynamic-sql`: a kept statement runs `EXECUTE`; references inside the
|
|
45
|
+
* dynamic string cannot be checked against the subsystem.
|
|
46
|
+
*/
|
|
47
|
+
kind: 'mixed-statement' | 'opaque-statement' | 'dynamic-sql';
|
|
48
|
+
statement: number;
|
|
49
|
+
detail: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A kept statement references a subsystem object that no route rebinds to a
|
|
53
|
+
* replacement. Exclusion is unsafe until every one of these is resolved.
|
|
54
|
+
*/
|
|
55
|
+
export interface UnsatisfiedReference {
|
|
56
|
+
object: SqlObjectRef;
|
|
57
|
+
statement: number;
|
|
58
|
+
fk: boolean;
|
|
59
|
+
}
|
|
60
|
+
export interface ExcludeResult {
|
|
61
|
+
/** Indexes of statements belonging to the subsystem (to be dropped). */
|
|
62
|
+
excluded: number[];
|
|
63
|
+
/** Indexes of surviving statements. */
|
|
64
|
+
kept: number[];
|
|
65
|
+
/** The subsystem's measured external contract. */
|
|
66
|
+
contract: SubsystemContract;
|
|
67
|
+
/**
|
|
68
|
+
* References into the subsystem from kept statements that the provided
|
|
69
|
+
* router does not rebind. Empty ⇔ the exclusion is safe.
|
|
70
|
+
*/
|
|
71
|
+
unsatisfied: UnsatisfiedReference[];
|
|
72
|
+
warnings: ExcludeWarning[];
|
|
73
|
+
/** The classified statements, for callers that need the facts. */
|
|
74
|
+
statements: StatementFacts[];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Partition a SQL script into a subsystem (statements to exclude) and its
|
|
78
|
+
* survivors, measure the subsystem's external contract, and verify that a
|
|
79
|
+
* routing profile rebinds every surviving reference into it.
|
|
80
|
+
*
|
|
81
|
+
* Pure and I/O-free. The caller applies the actual removal/rewrite (e.g. via
|
|
82
|
+
* `transpileBundle`'s `transformScript` with the same router); this function
|
|
83
|
+
* only *decides* and *checks* — exclusion is safe iff `unsatisfied` is empty.
|
|
84
|
+
*/
|
|
85
|
+
export declare function excludeSubsystem(sql: string, selector: SubsystemSelector, options?: {
|
|
86
|
+
rebinds?: SchemaRouter;
|
|
87
|
+
}): ExcludeResult;
|
|
88
|
+
/**
|
|
89
|
+
* Blank a script: keep its leading text (pgpm header) and transaction
|
|
90
|
+
* statements, drop everything else. Used for the scripts of a change whose
|
|
91
|
+
* deploy is entirely inside an excluded subsystem — its verify/revert bodies
|
|
92
|
+
* target dropped objects, so they must go with it.
|
|
93
|
+
*/
|
|
94
|
+
export declare function blankScriptSql(sql: string): string;
|
|
95
|
+
export interface StripSubsystemResult {
|
|
96
|
+
/** The surviving SQL, with subsystem statements removed. */
|
|
97
|
+
sql: string;
|
|
98
|
+
/** The analysis behind the removal (contract, unsatisfied, warnings). */
|
|
99
|
+
result: ExcludeResult;
|
|
100
|
+
/** Every removed statement index (excluded + opaque subsystem-targeted). */
|
|
101
|
+
dropped: number[];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Remove a subsystem's statements from a SQL script, preserving the original
|
|
105
|
+
* text of every survivor (no reformat — statements are sliced out of the
|
|
106
|
+
* source by parser-reported location). Also removes opaque statements that
|
|
107
|
+
* provably target only subsystem objects (`DROP`/`COMMENT ON` them), which
|
|
108
|
+
* membership classification alone keeps.
|
|
109
|
+
*
|
|
110
|
+
* Requires `await loadModule()` first (same as every other sync API here).
|
|
111
|
+
* This performs no safety check by itself — callers decide what to do with
|
|
112
|
+
* `result.unsatisfied` (typically: refuse before ever writing output).
|
|
113
|
+
*/
|
|
114
|
+
export declare function stripSubsystemSql(sql: string, selector: SubsystemSelector, options?: {
|
|
115
|
+
rebinds?: SchemaRouter;
|
|
116
|
+
}): StripSubsystemResult;
|
package/exclude.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.excludeSubsystem = excludeSubsystem;
|
|
4
|
+
exports.blankScriptSql = blankScriptSql;
|
|
5
|
+
exports.stripSubsystemSql = stripSubsystemSql;
|
|
6
|
+
const transform_1 = require("@pgpmjs/transform");
|
|
7
|
+
const plpgsql_parser_1 = require("plpgsql-parser");
|
|
8
|
+
function inSubsystem(ref, schemas) {
|
|
9
|
+
return ref.schema !== null && schemas.has(ref.schema);
|
|
10
|
+
}
|
|
11
|
+
function refKey(ref) {
|
|
12
|
+
return `${ref.schema ?? ''}.${ref.name}`;
|
|
13
|
+
}
|
|
14
|
+
function pushUnique(list, ref) {
|
|
15
|
+
if (!list.some(x => x.schema === ref.schema && x.name === ref.name))
|
|
16
|
+
list.push(ref);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The classifier does not tag a reference with its namespace, so resolution
|
|
20
|
+
* tries each object namespace (FK targets are known to be relations) before
|
|
21
|
+
* falling back to the schema-level default.
|
|
22
|
+
*/
|
|
23
|
+
function resolveRebind(router, ref, fk) {
|
|
24
|
+
if (!router)
|
|
25
|
+
return false;
|
|
26
|
+
const namespaces = fk
|
|
27
|
+
? ['relation']
|
|
28
|
+
: ['function', 'relation', 'type', 'unknown'];
|
|
29
|
+
for (const ns of namespaces) {
|
|
30
|
+
const target = router.resolveObject(ref.schema, ref.name, ns);
|
|
31
|
+
if (target !== undefined && (target.name !== undefined || target.schema !== undefined)) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Decide statement membership: a statement belongs to the subsystem when
|
|
39
|
+
* everything it creates/targets is inside it, or (creating nothing) when all
|
|
40
|
+
* of its references point inside it (GRANTs on subsystem objects).
|
|
41
|
+
*/
|
|
42
|
+
function classifyMembership(facts, schemas) {
|
|
43
|
+
if (facts.kind === 'schema') {
|
|
44
|
+
const created = facts.creates[0];
|
|
45
|
+
return created && schemas.has(created.name) ? 'inside' : 'outside';
|
|
46
|
+
}
|
|
47
|
+
if (facts.creates.length > 0) {
|
|
48
|
+
const inside = facts.creates.filter(c => inSubsystem(c, schemas)).length;
|
|
49
|
+
if (inside === facts.creates.length)
|
|
50
|
+
return 'inside';
|
|
51
|
+
if (inside === 0)
|
|
52
|
+
return 'outside';
|
|
53
|
+
return 'mixed';
|
|
54
|
+
}
|
|
55
|
+
const refs = [...facts.references, ...facts.bodyReferences];
|
|
56
|
+
if (refs.length > 0) {
|
|
57
|
+
return refs.every(r => inSubsystem(r, schemas)) ? 'inside' : 'outside';
|
|
58
|
+
}
|
|
59
|
+
// No creates, no visible references: COMMENT ON, bare DROP, SET, ...
|
|
60
|
+
return 'opaque';
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Partition a SQL script into a subsystem (statements to exclude) and its
|
|
64
|
+
* survivors, measure the subsystem's external contract, and verify that a
|
|
65
|
+
* routing profile rebinds every surviving reference into it.
|
|
66
|
+
*
|
|
67
|
+
* Pure and I/O-free. The caller applies the actual removal/rewrite (e.g. via
|
|
68
|
+
* `transpileBundle`'s `transformScript` with the same router); this function
|
|
69
|
+
* only *decides* and *checks* — exclusion is safe iff `unsatisfied` is empty.
|
|
70
|
+
*/
|
|
71
|
+
function excludeSubsystem(sql, selector, options = {}) {
|
|
72
|
+
const schemas = new Set(selector.schemas);
|
|
73
|
+
const statements = (0, transform_1.classifyStatements)(sql);
|
|
74
|
+
const router = options.rebinds;
|
|
75
|
+
const excluded = [];
|
|
76
|
+
const kept = [];
|
|
77
|
+
const warnings = [];
|
|
78
|
+
const provides = [];
|
|
79
|
+
const internal = [];
|
|
80
|
+
const required = new Map();
|
|
81
|
+
const unsatisfied = [];
|
|
82
|
+
statements.forEach((facts, i) => {
|
|
83
|
+
const membership = classifyMembership(facts, schemas);
|
|
84
|
+
if (membership === 'inside') {
|
|
85
|
+
excluded.push(i);
|
|
86
|
+
for (const c of facts.creates) {
|
|
87
|
+
if (inSubsystem(c, schemas))
|
|
88
|
+
pushUnique(provides, { schema: c.schema, name: c.name });
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
kept.push(i);
|
|
93
|
+
if (membership === 'mixed') {
|
|
94
|
+
warnings.push({
|
|
95
|
+
kind: 'mixed-statement',
|
|
96
|
+
statement: i,
|
|
97
|
+
detail: `creates objects both inside and outside the subsystem: ${facts.creates
|
|
98
|
+
.map(refKey)
|
|
99
|
+
.join(', ')}`
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else if (membership === 'opaque') {
|
|
103
|
+
warnings.push({
|
|
104
|
+
kind: 'opaque-statement',
|
|
105
|
+
statement: i,
|
|
106
|
+
detail: `${facts.nodeTag} target is not classified; verify it does not reference the subsystem`
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (facts.dynamicSql) {
|
|
110
|
+
warnings.push({
|
|
111
|
+
kind: 'dynamic-sql',
|
|
112
|
+
statement: i,
|
|
113
|
+
detail: 'kept statement executes dynamic SQL; references inside it are unchecked'
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const refs = new Map();
|
|
117
|
+
for (const r of [...facts.references, ...facts.bodyReferences]) {
|
|
118
|
+
if (inSubsystem(r, schemas))
|
|
119
|
+
refs.set(refKey(r), { ref: r, fk: refs.get(refKey(r))?.fk ?? false });
|
|
120
|
+
}
|
|
121
|
+
for (const t of facts.fkTargets) {
|
|
122
|
+
if (inSubsystem(t, schemas))
|
|
123
|
+
refs.set(refKey(t), { ref: t, fk: true });
|
|
124
|
+
}
|
|
125
|
+
for (const { ref, fk } of refs.values()) {
|
|
126
|
+
const key = refKey(ref);
|
|
127
|
+
const dep = required.get(key) ?? { object: { schema: ref.schema, name: ref.name }, fk: false, dependents: [] };
|
|
128
|
+
dep.fk = dep.fk || fk;
|
|
129
|
+
if (!dep.dependents.includes(i))
|
|
130
|
+
dep.dependents.push(i);
|
|
131
|
+
required.set(key, dep);
|
|
132
|
+
const rebound = resolveRebind(router, ref, fk);
|
|
133
|
+
if (!rebound && !unsatisfied.some(u => refKey(u.object) === key && u.statement === i)) {
|
|
134
|
+
unsatisfied.push({ object: { schema: ref.schema, name: ref.name }, statement: i, fk });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
for (const p of provides) {
|
|
139
|
+
if (!required.has(refKey(p)))
|
|
140
|
+
pushUnique(internal, p);
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
excluded,
|
|
144
|
+
kept,
|
|
145
|
+
contract: {
|
|
146
|
+
provides,
|
|
147
|
+
required: [...required.values()],
|
|
148
|
+
internal
|
|
149
|
+
},
|
|
150
|
+
unsatisfied,
|
|
151
|
+
warnings,
|
|
152
|
+
statements
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Blank a script: keep its leading text (pgpm header) and transaction
|
|
157
|
+
* statements, drop everything else. Used for the scripts of a change whose
|
|
158
|
+
* deploy is entirely inside an excluded subsystem — its verify/revert bodies
|
|
159
|
+
* target dropped objects, so they must go with it.
|
|
160
|
+
*/
|
|
161
|
+
function blankScriptSql(sql) {
|
|
162
|
+
const parsed = (0, plpgsql_parser_1.parseSql)(sql);
|
|
163
|
+
const prefix = parsed.stmts.length > 0 ? sql.slice(0, parsed.stmts[0].stmt_location ?? 0) : sql;
|
|
164
|
+
const pieces = [];
|
|
165
|
+
for (const s of parsed.stmts) {
|
|
166
|
+
if (!('TransactionStmt' in s.stmt))
|
|
167
|
+
continue;
|
|
168
|
+
const start = s.stmt_location ?? 0;
|
|
169
|
+
const len = s.stmt_len ?? sql.length - start;
|
|
170
|
+
pieces.push(sql.slice(start, start + len).trim() + ';');
|
|
171
|
+
}
|
|
172
|
+
return prefix + pieces.join('\n\n') + (pieces.length > 0 ? '\n' : '');
|
|
173
|
+
}
|
|
174
|
+
/** Qualified name (`[schema, name]` items) from a raw parse-tree name list. */
|
|
175
|
+
function qualifiedName(node) {
|
|
176
|
+
const items = node?.List
|
|
177
|
+
?.items;
|
|
178
|
+
if (!items)
|
|
179
|
+
return undefined;
|
|
180
|
+
const parts = items.map(x => x.String?.sval).filter((s) => typeof s === 'string');
|
|
181
|
+
if (parts.length === 2)
|
|
182
|
+
return { schema: parts[0], name: parts[1] };
|
|
183
|
+
if (parts.length === 1)
|
|
184
|
+
return { schema: null, name: parts[0] };
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Whether an opaque statement (invisible to classification) provably targets
|
|
189
|
+
* only subsystem objects, so it can be removed along with them: bare `DROP`
|
|
190
|
+
* of subsystem objects, `COMMENT ON` a subsystem object.
|
|
191
|
+
*/
|
|
192
|
+
function opaqueTargetsSubsystem(stmt, schemas) {
|
|
193
|
+
if (stmt.DropStmt?.objects) {
|
|
194
|
+
const objects = stmt.DropStmt.objects;
|
|
195
|
+
const refs = objects.map(qualifiedName);
|
|
196
|
+
return refs.length > 0 && refs.every(r => r !== undefined && inSubsystem(r, schemas));
|
|
197
|
+
}
|
|
198
|
+
if (stmt.CommentStmt?.object) {
|
|
199
|
+
const ref = qualifiedName(stmt.CommentStmt.object);
|
|
200
|
+
return ref !== undefined && inSubsystem(ref, schemas);
|
|
201
|
+
}
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Remove a subsystem's statements from a SQL script, preserving the original
|
|
206
|
+
* text of every survivor (no reformat — statements are sliced out of the
|
|
207
|
+
* source by parser-reported location). Also removes opaque statements that
|
|
208
|
+
* provably target only subsystem objects (`DROP`/`COMMENT ON` them), which
|
|
209
|
+
* membership classification alone keeps.
|
|
210
|
+
*
|
|
211
|
+
* Requires `await loadModule()` first (same as every other sync API here).
|
|
212
|
+
* This performs no safety check by itself — callers decide what to do with
|
|
213
|
+
* `result.unsatisfied` (typically: refuse before ever writing output).
|
|
214
|
+
*/
|
|
215
|
+
function stripSubsystemSql(sql, selector, options = {}) {
|
|
216
|
+
const result = excludeSubsystem(sql, selector, options);
|
|
217
|
+
const schemas = new Set(selector.schemas);
|
|
218
|
+
const parsed = (0, plpgsql_parser_1.parseSql)(sql);
|
|
219
|
+
if (parsed.stmts.length !== result.statements.length) {
|
|
220
|
+
throw new Error(`stripSubsystemSql: parser saw ${parsed.stmts.length} statements but the classifier saw ` +
|
|
221
|
+
`${result.statements.length}; refusing to slice by index`);
|
|
222
|
+
}
|
|
223
|
+
const drop = new Set(result.excluded);
|
|
224
|
+
for (const i of result.kept) {
|
|
225
|
+
if (opaqueTargetsSubsystem(parsed.stmts[i].stmt, schemas))
|
|
226
|
+
drop.add(i);
|
|
227
|
+
}
|
|
228
|
+
// Text before the first statement (pgpm headers, leading comments) is
|
|
229
|
+
// preserved verbatim so script identity headers survive the strip.
|
|
230
|
+
const prefix = parsed.stmts.length > 0 ? sql.slice(0, parsed.stmts[0].stmt_location ?? 0) : sql;
|
|
231
|
+
const pieces = [];
|
|
232
|
+
parsed.stmts.forEach((s, i) => {
|
|
233
|
+
if (drop.has(i))
|
|
234
|
+
return;
|
|
235
|
+
const start = s.stmt_location ?? 0;
|
|
236
|
+
const len = s.stmt_len ?? sql.length - start;
|
|
237
|
+
pieces.push(sql.slice(start, start + len).trim() + ';');
|
|
238
|
+
});
|
|
239
|
+
return {
|
|
240
|
+
sql: prefix + pieces.join('\n\n') + (pieces.length > 0 ? '\n' : ''),
|
|
241
|
+
result,
|
|
242
|
+
dropped: [...drop].sort((a, b) => a - b)
|
|
243
|
+
};
|
|
244
|
+
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpmjs/slice",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.2",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "PGPM plan slicing — split a monolithic pgpm plan into packages with pattern strategies and opt-in AST-based dependency closure",
|
|
6
6
|
"main": "index.js",
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
"makage": "^0.3.0"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@pgpmjs/ast": "^0.9.
|
|
46
|
-
"@pgpmjs/transform": "^0.
|
|
45
|
+
"@pgpmjs/ast": "^0.9.5",
|
|
46
|
+
"@pgpmjs/transform": "^0.12.2",
|
|
47
47
|
"minimatch": "^10.2.5",
|
|
48
48
|
"plpgsql-parser": "^18.2.1"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "a430897efa1488ec24fc636f0d5023207bba7ba7"
|
|
51
51
|
}
|