@pgpmjs/slice 0.11.2 → 0.13.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/esm/exclude.js +90 -41
- package/esm/index.js +1 -0
- package/esm/object-graph.js +113 -0
- package/exclude.d.ts +46 -1
- package/exclude.js +90 -40
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/object-graph.d.ts +70 -0
- package/object-graph.js +121 -0
- package/package.json +5 -5
package/esm/exclude.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { parseSqlProgram } from '@pgpmjs/transform';
|
|
2
|
+
import { buildObjectGraph } from './object-graph';
|
|
3
3
|
function inSubsystem(ref, schemas) {
|
|
4
4
|
return ref.schema !== null && schemas.has(ref.schema);
|
|
5
5
|
}
|
|
@@ -64,14 +64,32 @@ function classifyMembership(facts, schemas) {
|
|
|
64
64
|
* only *decides* and *checks* — exclusion is safe iff `unsatisfied` is empty.
|
|
65
65
|
*/
|
|
66
66
|
export function excludeSubsystem(sql, selector, options = {}) {
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
|
|
67
|
+
const program = parseSqlProgram(sql);
|
|
68
|
+
const analysis = analyzeProgram(program, new Set(selector.schemas), options.rebinds);
|
|
69
|
+
return {
|
|
70
|
+
excluded: analysis.excluded,
|
|
71
|
+
kept: analysis.kept,
|
|
72
|
+
contract: contractOf(analysis.provides, analysis.required),
|
|
73
|
+
unsatisfied: analysis.unsatisfied,
|
|
74
|
+
warnings: analysis.warnings,
|
|
75
|
+
statements: program.statements.map(s => s.facts),
|
|
76
|
+
program
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function contractOf(provides, required) {
|
|
80
|
+
const internal = [];
|
|
81
|
+
for (const p of provides) {
|
|
82
|
+
if (!required.has(refKey(p)))
|
|
83
|
+
pushUnique(internal, p);
|
|
84
|
+
}
|
|
85
|
+
return { provides, required: [...required.values()], internal };
|
|
86
|
+
}
|
|
87
|
+
function analyzeProgram(program, schemas, router) {
|
|
88
|
+
const statements = program.statements.map(s => s.facts);
|
|
70
89
|
const excluded = [];
|
|
71
90
|
const kept = [];
|
|
72
91
|
const warnings = [];
|
|
73
92
|
const provides = [];
|
|
74
|
-
const internal = [];
|
|
75
93
|
const required = new Map();
|
|
76
94
|
const unsatisfied = [];
|
|
77
95
|
statements.forEach((facts, i) => {
|
|
@@ -130,23 +148,71 @@ export function excludeSubsystem(sql, selector, options = {}) {
|
|
|
130
148
|
}
|
|
131
149
|
}
|
|
132
150
|
});
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
151
|
+
return { excluded, kept, warnings, provides, required, unsatisfied };
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Analyze a subsystem exclusion across a set of already-parsed programs
|
|
155
|
+
* (typically one per pgpm change) in a single pass: the contract and cascade
|
|
156
|
+
* safety are measured over all programs together — which is what catches
|
|
157
|
+
* cross-change dependencies on the subsystem — while drop/strip/prune
|
|
158
|
+
* decisions are returned per program, derived from statement membership and
|
|
159
|
+
* ownership rather than survivor counting. The unified {@link SqlObjectGraph}
|
|
160
|
+
* over the same programs is returned for structural queries.
|
|
161
|
+
*/
|
|
162
|
+
export function excludeSubsystemPrograms(programs, selector, options = {}) {
|
|
163
|
+
const entries = programs instanceof Map ? [...programs.entries()] : programs;
|
|
164
|
+
const schemas = new Set(selector.schemas);
|
|
165
|
+
const provides = [];
|
|
166
|
+
const required = new Map();
|
|
167
|
+
const unsatisfied = [];
|
|
168
|
+
const warnings = [];
|
|
169
|
+
const perProgram = new Map();
|
|
170
|
+
for (const [name, program] of entries) {
|
|
171
|
+
const analysis = analyzeProgram(program, schemas, options.rebinds);
|
|
172
|
+
for (const p of analysis.provides)
|
|
173
|
+
pushUnique(provides, p);
|
|
174
|
+
for (const [key, dep] of analysis.required) {
|
|
175
|
+
const merged = required.get(key) ?? { object: dep.object, fk: false, dependents: [] };
|
|
176
|
+
merged.fk = merged.fk || dep.fk;
|
|
177
|
+
required.set(key, merged);
|
|
178
|
+
}
|
|
179
|
+
unsatisfied.push(...analysis.unsatisfied.map(u => ({ ...u, program: name })));
|
|
180
|
+
warnings.push(...analysis.warnings.map(w => ({ ...w, program: name })));
|
|
181
|
+
const dropped = new Set(analysis.excluded);
|
|
182
|
+
for (const i of analysis.kept) {
|
|
183
|
+
if (opaqueTargetsSubsystem(program.statements[i].stmt, schemas))
|
|
184
|
+
dropped.add(i);
|
|
185
|
+
}
|
|
186
|
+
const survivors = program.statements.filter((s, i) => !dropped.has(i) && !('TransactionStmt' in s.stmt));
|
|
187
|
+
perProgram.set(name, {
|
|
188
|
+
dropped: [...dropped].sort((a, b) => a - b),
|
|
189
|
+
fullyExcluded: dropped.size > 0 && survivors.length === 0,
|
|
190
|
+
sql: sliceSurvivors(program, dropped)
|
|
191
|
+
});
|
|
136
192
|
}
|
|
137
193
|
return {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
contract: {
|
|
141
|
-
provides,
|
|
142
|
-
required: [...required.values()],
|
|
143
|
-
internal
|
|
144
|
-
},
|
|
194
|
+
graph: buildObjectGraph(entries),
|
|
195
|
+
contract: contractOf(provides, required),
|
|
145
196
|
unsatisfied,
|
|
146
197
|
warnings,
|
|
147
|
-
|
|
198
|
+
programs: perProgram
|
|
148
199
|
};
|
|
149
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Slice the surviving statements out of a program's source, preserving the
|
|
203
|
+
* leading text (pgpm headers) and each survivor's original bytes.
|
|
204
|
+
*/
|
|
205
|
+
function sliceSurvivors(program, dropped) {
|
|
206
|
+
const { source, statements } = program;
|
|
207
|
+
const prefix = statements.length > 0 ? source.slice(0, statements[0].span.start) : source;
|
|
208
|
+
const pieces = [];
|
|
209
|
+
statements.forEach((s, i) => {
|
|
210
|
+
if (dropped.has(i))
|
|
211
|
+
return;
|
|
212
|
+
pieces.push(s.raw.trim() + ';');
|
|
213
|
+
});
|
|
214
|
+
return prefix + pieces.join('\n\n') + (pieces.length > 0 ? '\n' : '');
|
|
215
|
+
}
|
|
150
216
|
/**
|
|
151
217
|
* Blank a script: keep its leading text (pgpm header) and transaction
|
|
152
218
|
* statements, drop everything else. Used for the scripts of a change whose
|
|
@@ -154,15 +220,13 @@ export function excludeSubsystem(sql, selector, options = {}) {
|
|
|
154
220
|
* target dropped objects, so they must go with it.
|
|
155
221
|
*/
|
|
156
222
|
export function blankScriptSql(sql) {
|
|
157
|
-
const
|
|
158
|
-
const prefix =
|
|
223
|
+
const { statements } = parseSqlProgram(sql);
|
|
224
|
+
const prefix = statements.length > 0 ? sql.slice(0, statements[0].span.start) : sql;
|
|
159
225
|
const pieces = [];
|
|
160
|
-
for (const s of
|
|
226
|
+
for (const s of statements) {
|
|
161
227
|
if (!('TransactionStmt' in s.stmt))
|
|
162
228
|
continue;
|
|
163
|
-
|
|
164
|
-
const len = s.stmt_len ?? sql.length - start;
|
|
165
|
-
pieces.push(sql.slice(start, start + len).trim() + ';');
|
|
229
|
+
pieces.push(s.raw.trim() + ';');
|
|
166
230
|
}
|
|
167
231
|
return prefix + pieces.join('\n\n') + (pieces.length > 0 ? '\n' : '');
|
|
168
232
|
}
|
|
@@ -210,29 +274,14 @@ function opaqueTargetsSubsystem(stmt, schemas) {
|
|
|
210
274
|
export function stripSubsystemSql(sql, selector, options = {}) {
|
|
211
275
|
const result = excludeSubsystem(sql, selector, options);
|
|
212
276
|
const schemas = new Set(selector.schemas);
|
|
213
|
-
const
|
|
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
|
-
}
|
|
277
|
+
const { statements } = result.program;
|
|
218
278
|
const drop = new Set(result.excluded);
|
|
219
279
|
for (const i of result.kept) {
|
|
220
|
-
if (opaqueTargetsSubsystem(
|
|
280
|
+
if (opaqueTargetsSubsystem(statements[i].stmt, schemas))
|
|
221
281
|
drop.add(i);
|
|
222
282
|
}
|
|
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
283
|
return {
|
|
235
|
-
sql:
|
|
284
|
+
sql: sliceSurvivors(result.program, drop),
|
|
236
285
|
result,
|
|
237
286
|
dropped: [...drop].sort((a, b) => a - b)
|
|
238
287
|
};
|
package/esm/index.js
CHANGED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
export function objectKey(ref) {
|
|
2
|
+
return `${ref.schema ?? ''}\u0000${ref.name}`;
|
|
3
|
+
}
|
|
4
|
+
export function objectLabel(ref) {
|
|
5
|
+
return ref.schema ? `${ref.schema}.${ref.name}` : ref.name;
|
|
6
|
+
}
|
|
7
|
+
function statementFacts(program, index) {
|
|
8
|
+
return program.statements[index].facts;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Build the object graph from named programs (typically change name →
|
|
12
|
+
* parsed deploy script).
|
|
13
|
+
*/
|
|
14
|
+
export function buildObjectGraph(programs) {
|
|
15
|
+
const programMap = programs instanceof Map ? programs : new Map(programs);
|
|
16
|
+
const objects = new Map();
|
|
17
|
+
const edges = [];
|
|
18
|
+
const incoming = new Map();
|
|
19
|
+
const addEdge = (edge) => {
|
|
20
|
+
edges.push(edge);
|
|
21
|
+
const list = incoming.get(edge.to);
|
|
22
|
+
if (list)
|
|
23
|
+
list.push(edge);
|
|
24
|
+
else
|
|
25
|
+
incoming.set(edge.to, [edge]);
|
|
26
|
+
};
|
|
27
|
+
for (const [name, program] of programMap) {
|
|
28
|
+
program.statements.forEach((stmt, i) => {
|
|
29
|
+
const id = { program: name, statement: i };
|
|
30
|
+
for (const c of stmt.facts.creates) {
|
|
31
|
+
const key = objectKey(c);
|
|
32
|
+
const node = objects.get(key);
|
|
33
|
+
if (node)
|
|
34
|
+
node.createdBy.push(id);
|
|
35
|
+
else
|
|
36
|
+
objects.set(key, { ref: { schema: c.schema, name: c.name }, createdBy: [id] });
|
|
37
|
+
}
|
|
38
|
+
for (const r of stmt.facts.references) {
|
|
39
|
+
addEdge({ from: id, to: objectKey(r), kind: 'reference' });
|
|
40
|
+
}
|
|
41
|
+
for (const r of stmt.facts.bodyReferences) {
|
|
42
|
+
addEdge({ from: id, to: objectKey(r), kind: 'body' });
|
|
43
|
+
}
|
|
44
|
+
for (const t of stmt.facts.fkTargets) {
|
|
45
|
+
addEdge({ from: id, to: objectKey(t), kind: 'fk' });
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return { programs: programMap, objects, edges, incoming };
|
|
50
|
+
}
|
|
51
|
+
/** Keys of every object created in one of the given schemas. */
|
|
52
|
+
export function objectsInSchemas(graph, schemas) {
|
|
53
|
+
const wanted = new Set(schemas);
|
|
54
|
+
const keys = new Set();
|
|
55
|
+
for (const [key, node] of graph.objects) {
|
|
56
|
+
if (node.ref.schema !== null && wanted.has(node.ref.schema))
|
|
57
|
+
keys.add(key);
|
|
58
|
+
}
|
|
59
|
+
return keys;
|
|
60
|
+
}
|
|
61
|
+
/** Statement ids that create at least one object in `dropped`. */
|
|
62
|
+
function droppedStatements(graph, dropped) {
|
|
63
|
+
const ids = new Set();
|
|
64
|
+
for (const key of dropped) {
|
|
65
|
+
const node = graph.objects.get(key);
|
|
66
|
+
if (!node)
|
|
67
|
+
continue;
|
|
68
|
+
for (const id of node.createdBy)
|
|
69
|
+
ids.add(`${id.program}\u0000${id.statement}`);
|
|
70
|
+
}
|
|
71
|
+
return ids;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Edges into `dropped` objects whose source statement is not itself dropped.
|
|
75
|
+
* Each is a dangling dependency: dropping the object set is safe iff this is
|
|
76
|
+
* empty or every edge's referenced object is rebound to a replacement.
|
|
77
|
+
*/
|
|
78
|
+
export function danglingEdges(graph, dropped) {
|
|
79
|
+
const droppedStmts = droppedStatements(graph, dropped);
|
|
80
|
+
const dangling = [];
|
|
81
|
+
for (const key of dropped) {
|
|
82
|
+
for (const edge of graph.incoming.get(key) ?? []) {
|
|
83
|
+
if (!droppedStmts.has(`${edge.from.program}\u0000${edge.from.statement}`)) {
|
|
84
|
+
dangling.push(edge);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return dangling;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Programs every one of whose object-creating statements creates only
|
|
92
|
+
* `dropped` objects — whole changes owned by the dropped set, prunable from
|
|
93
|
+
* the plan by ownership (read from the graph, not counted from survivors).
|
|
94
|
+
* Programs that create nothing are never pruned.
|
|
95
|
+
*/
|
|
96
|
+
export function prunablePrograms(graph, dropped) {
|
|
97
|
+
const prunable = [];
|
|
98
|
+
for (const [name, program] of graph.programs) {
|
|
99
|
+
let creating = 0;
|
|
100
|
+
let insideOnly = true;
|
|
101
|
+
program.statements.forEach((stmt, i) => {
|
|
102
|
+
if (stmt.facts.creates.length === 0)
|
|
103
|
+
return;
|
|
104
|
+
creating++;
|
|
105
|
+
const facts = statementFacts(program, i);
|
|
106
|
+
if (!facts.creates.every(c => dropped.has(objectKey(c))))
|
|
107
|
+
insideOnly = false;
|
|
108
|
+
});
|
|
109
|
+
if (creating > 0 && insideOnly)
|
|
110
|
+
prunable.push(name);
|
|
111
|
+
}
|
|
112
|
+
return prunable;
|
|
113
|
+
}
|
package/exclude.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { SchemaRouter, StatementFacts } from '@pgpmjs/transform';
|
|
1
|
+
import { SchemaRouter, SqlProgram, StatementFacts } from '@pgpmjs/transform';
|
|
2
|
+
import { SqlObjectGraph } from './object-graph';
|
|
2
3
|
import { SqlObjectRef } from './refs';
|
|
3
4
|
/**
|
|
4
5
|
* Selects a *subsystem*: the set of objects a module owns that a consumer
|
|
@@ -72,6 +73,8 @@ export interface ExcludeResult {
|
|
|
72
73
|
warnings: ExcludeWarning[];
|
|
73
74
|
/** The classified statements, for callers that need the facts. */
|
|
74
75
|
statements: StatementFacts[];
|
|
76
|
+
/** The parsed program the analysis ran over (one parse, shared with strip). */
|
|
77
|
+
program: SqlProgram;
|
|
75
78
|
}
|
|
76
79
|
/**
|
|
77
80
|
* Partition a SQL script into a subsystem (statements to exclude) and its
|
|
@@ -85,6 +88,48 @@ export interface ExcludeResult {
|
|
|
85
88
|
export declare function excludeSubsystem(sql: string, selector: SubsystemSelector, options?: {
|
|
86
89
|
rebinds?: SchemaRouter;
|
|
87
90
|
}): ExcludeResult;
|
|
91
|
+
/** An `UnsatisfiedReference`/`ExcludeWarning` tagged with its owning program. */
|
|
92
|
+
export type ProgramUnsatisfiedReference = UnsatisfiedReference & {
|
|
93
|
+
program: string;
|
|
94
|
+
};
|
|
95
|
+
export type ProgramExcludeWarning = ExcludeWarning & {
|
|
96
|
+
program: string;
|
|
97
|
+
};
|
|
98
|
+
/** Per-program exclusion decisions from {@link excludeSubsystemPrograms}. */
|
|
99
|
+
export interface ProgramExclusion {
|
|
100
|
+
/** Statement indexes removed: subsystem members + opaque subsystem-targeted. */
|
|
101
|
+
dropped: number[];
|
|
102
|
+
/**
|
|
103
|
+
* The whole program consists of subsystem statements (plus transaction
|
|
104
|
+
* control): it should be removed as a unit rather than emptied in place.
|
|
105
|
+
*/
|
|
106
|
+
fullyExcluded: boolean;
|
|
107
|
+
/** The surviving SQL with the dropped statements sliced out. */
|
|
108
|
+
sql: string;
|
|
109
|
+
}
|
|
110
|
+
export interface ExcludeProgramsResult {
|
|
111
|
+
/** The unified object graph over every analyzed program. */
|
|
112
|
+
graph: SqlObjectGraph;
|
|
113
|
+
/** The subsystem's external contract, measured across all programs. */
|
|
114
|
+
contract: SubsystemContract;
|
|
115
|
+
/** Unrebound references into the subsystem, tagged with their program. */
|
|
116
|
+
unsatisfied: ProgramUnsatisfiedReference[];
|
|
117
|
+
warnings: ProgramExcludeWarning[];
|
|
118
|
+
/** Per-program decisions, keyed by program name. */
|
|
119
|
+
programs: Map<string, ProgramExclusion>;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Analyze a subsystem exclusion across a set of already-parsed programs
|
|
123
|
+
* (typically one per pgpm change) in a single pass: the contract and cascade
|
|
124
|
+
* safety are measured over all programs together — which is what catches
|
|
125
|
+
* cross-change dependencies on the subsystem — while drop/strip/prune
|
|
126
|
+
* decisions are returned per program, derived from statement membership and
|
|
127
|
+
* ownership rather than survivor counting. The unified {@link SqlObjectGraph}
|
|
128
|
+
* over the same programs is returned for structural queries.
|
|
129
|
+
*/
|
|
130
|
+
export declare function excludeSubsystemPrograms(programs: Map<string, SqlProgram> | Array<[string, SqlProgram]>, selector: SubsystemSelector, options?: {
|
|
131
|
+
rebinds?: SchemaRouter;
|
|
132
|
+
}): ExcludeProgramsResult;
|
|
88
133
|
/**
|
|
89
134
|
* Blank a script: keep its leading text (pgpm header) and transaction
|
|
90
135
|
* statements, drop everything else. Used for the scripts of a change whose
|
package/exclude.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.excludeSubsystem = excludeSubsystem;
|
|
4
|
+
exports.excludeSubsystemPrograms = excludeSubsystemPrograms;
|
|
4
5
|
exports.blankScriptSql = blankScriptSql;
|
|
5
6
|
exports.stripSubsystemSql = stripSubsystemSql;
|
|
6
7
|
const transform_1 = require("@pgpmjs/transform");
|
|
7
|
-
const
|
|
8
|
+
const object_graph_1 = require("./object-graph");
|
|
8
9
|
function inSubsystem(ref, schemas) {
|
|
9
10
|
return ref.schema !== null && schemas.has(ref.schema);
|
|
10
11
|
}
|
|
@@ -69,14 +70,32 @@ function classifyMembership(facts, schemas) {
|
|
|
69
70
|
* only *decides* and *checks* — exclusion is safe iff `unsatisfied` is empty.
|
|
70
71
|
*/
|
|
71
72
|
function excludeSubsystem(sql, selector, options = {}) {
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
73
|
+
const program = (0, transform_1.parseSqlProgram)(sql);
|
|
74
|
+
const analysis = analyzeProgram(program, new Set(selector.schemas), options.rebinds);
|
|
75
|
+
return {
|
|
76
|
+
excluded: analysis.excluded,
|
|
77
|
+
kept: analysis.kept,
|
|
78
|
+
contract: contractOf(analysis.provides, analysis.required),
|
|
79
|
+
unsatisfied: analysis.unsatisfied,
|
|
80
|
+
warnings: analysis.warnings,
|
|
81
|
+
statements: program.statements.map(s => s.facts),
|
|
82
|
+
program
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function contractOf(provides, required) {
|
|
86
|
+
const internal = [];
|
|
87
|
+
for (const p of provides) {
|
|
88
|
+
if (!required.has(refKey(p)))
|
|
89
|
+
pushUnique(internal, p);
|
|
90
|
+
}
|
|
91
|
+
return { provides, required: [...required.values()], internal };
|
|
92
|
+
}
|
|
93
|
+
function analyzeProgram(program, schemas, router) {
|
|
94
|
+
const statements = program.statements.map(s => s.facts);
|
|
75
95
|
const excluded = [];
|
|
76
96
|
const kept = [];
|
|
77
97
|
const warnings = [];
|
|
78
98
|
const provides = [];
|
|
79
|
-
const internal = [];
|
|
80
99
|
const required = new Map();
|
|
81
100
|
const unsatisfied = [];
|
|
82
101
|
statements.forEach((facts, i) => {
|
|
@@ -135,23 +154,71 @@ function excludeSubsystem(sql, selector, options = {}) {
|
|
|
135
154
|
}
|
|
136
155
|
}
|
|
137
156
|
});
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
157
|
+
return { excluded, kept, warnings, provides, required, unsatisfied };
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Analyze a subsystem exclusion across a set of already-parsed programs
|
|
161
|
+
* (typically one per pgpm change) in a single pass: the contract and cascade
|
|
162
|
+
* safety are measured over all programs together — which is what catches
|
|
163
|
+
* cross-change dependencies on the subsystem — while drop/strip/prune
|
|
164
|
+
* decisions are returned per program, derived from statement membership and
|
|
165
|
+
* ownership rather than survivor counting. The unified {@link SqlObjectGraph}
|
|
166
|
+
* over the same programs is returned for structural queries.
|
|
167
|
+
*/
|
|
168
|
+
function excludeSubsystemPrograms(programs, selector, options = {}) {
|
|
169
|
+
const entries = programs instanceof Map ? [...programs.entries()] : programs;
|
|
170
|
+
const schemas = new Set(selector.schemas);
|
|
171
|
+
const provides = [];
|
|
172
|
+
const required = new Map();
|
|
173
|
+
const unsatisfied = [];
|
|
174
|
+
const warnings = [];
|
|
175
|
+
const perProgram = new Map();
|
|
176
|
+
for (const [name, program] of entries) {
|
|
177
|
+
const analysis = analyzeProgram(program, schemas, options.rebinds);
|
|
178
|
+
for (const p of analysis.provides)
|
|
179
|
+
pushUnique(provides, p);
|
|
180
|
+
for (const [key, dep] of analysis.required) {
|
|
181
|
+
const merged = required.get(key) ?? { object: dep.object, fk: false, dependents: [] };
|
|
182
|
+
merged.fk = merged.fk || dep.fk;
|
|
183
|
+
required.set(key, merged);
|
|
184
|
+
}
|
|
185
|
+
unsatisfied.push(...analysis.unsatisfied.map(u => ({ ...u, program: name })));
|
|
186
|
+
warnings.push(...analysis.warnings.map(w => ({ ...w, program: name })));
|
|
187
|
+
const dropped = new Set(analysis.excluded);
|
|
188
|
+
for (const i of analysis.kept) {
|
|
189
|
+
if (opaqueTargetsSubsystem(program.statements[i].stmt, schemas))
|
|
190
|
+
dropped.add(i);
|
|
191
|
+
}
|
|
192
|
+
const survivors = program.statements.filter((s, i) => !dropped.has(i) && !('TransactionStmt' in s.stmt));
|
|
193
|
+
perProgram.set(name, {
|
|
194
|
+
dropped: [...dropped].sort((a, b) => a - b),
|
|
195
|
+
fullyExcluded: dropped.size > 0 && survivors.length === 0,
|
|
196
|
+
sql: sliceSurvivors(program, dropped)
|
|
197
|
+
});
|
|
141
198
|
}
|
|
142
199
|
return {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
contract: {
|
|
146
|
-
provides,
|
|
147
|
-
required: [...required.values()],
|
|
148
|
-
internal
|
|
149
|
-
},
|
|
200
|
+
graph: (0, object_graph_1.buildObjectGraph)(entries),
|
|
201
|
+
contract: contractOf(provides, required),
|
|
150
202
|
unsatisfied,
|
|
151
203
|
warnings,
|
|
152
|
-
|
|
204
|
+
programs: perProgram
|
|
153
205
|
};
|
|
154
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Slice the surviving statements out of a program's source, preserving the
|
|
209
|
+
* leading text (pgpm headers) and each survivor's original bytes.
|
|
210
|
+
*/
|
|
211
|
+
function sliceSurvivors(program, dropped) {
|
|
212
|
+
const { source, statements } = program;
|
|
213
|
+
const prefix = statements.length > 0 ? source.slice(0, statements[0].span.start) : source;
|
|
214
|
+
const pieces = [];
|
|
215
|
+
statements.forEach((s, i) => {
|
|
216
|
+
if (dropped.has(i))
|
|
217
|
+
return;
|
|
218
|
+
pieces.push(s.raw.trim() + ';');
|
|
219
|
+
});
|
|
220
|
+
return prefix + pieces.join('\n\n') + (pieces.length > 0 ? '\n' : '');
|
|
221
|
+
}
|
|
155
222
|
/**
|
|
156
223
|
* Blank a script: keep its leading text (pgpm header) and transaction
|
|
157
224
|
* statements, drop everything else. Used for the scripts of a change whose
|
|
@@ -159,15 +226,13 @@ function excludeSubsystem(sql, selector, options = {}) {
|
|
|
159
226
|
* target dropped objects, so they must go with it.
|
|
160
227
|
*/
|
|
161
228
|
function blankScriptSql(sql) {
|
|
162
|
-
const
|
|
163
|
-
const prefix =
|
|
229
|
+
const { statements } = (0, transform_1.parseSqlProgram)(sql);
|
|
230
|
+
const prefix = statements.length > 0 ? sql.slice(0, statements[0].span.start) : sql;
|
|
164
231
|
const pieces = [];
|
|
165
|
-
for (const s of
|
|
232
|
+
for (const s of statements) {
|
|
166
233
|
if (!('TransactionStmt' in s.stmt))
|
|
167
234
|
continue;
|
|
168
|
-
|
|
169
|
-
const len = s.stmt_len ?? sql.length - start;
|
|
170
|
-
pieces.push(sql.slice(start, start + len).trim() + ';');
|
|
235
|
+
pieces.push(s.raw.trim() + ';');
|
|
171
236
|
}
|
|
172
237
|
return prefix + pieces.join('\n\n') + (pieces.length > 0 ? '\n' : '');
|
|
173
238
|
}
|
|
@@ -215,29 +280,14 @@ function opaqueTargetsSubsystem(stmt, schemas) {
|
|
|
215
280
|
function stripSubsystemSql(sql, selector, options = {}) {
|
|
216
281
|
const result = excludeSubsystem(sql, selector, options);
|
|
217
282
|
const schemas = new Set(selector.schemas);
|
|
218
|
-
const
|
|
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
|
-
}
|
|
283
|
+
const { statements } = result.program;
|
|
223
284
|
const drop = new Set(result.excluded);
|
|
224
285
|
for (const i of result.kept) {
|
|
225
|
-
if (opaqueTargetsSubsystem(
|
|
286
|
+
if (opaqueTargetsSubsystem(statements[i].stmt, schemas))
|
|
226
287
|
drop.add(i);
|
|
227
288
|
}
|
|
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
289
|
return {
|
|
240
|
-
sql:
|
|
290
|
+
sql: sliceSurvivors(result.program, drop),
|
|
241
291
|
result,
|
|
242
292
|
dropped: [...drop].sort((a, b) => a - b)
|
|
243
293
|
};
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified SQL object graph — the semantic IR over parsed programs.
|
|
3
|
+
*
|
|
4
|
+
* Nodes are database objects (keyed by qualified name) with the statements
|
|
5
|
+
* that create them; edges are the references statements make to objects:
|
|
6
|
+
* plain references, late-bound PL/pgSQL body references, and foreign-key
|
|
7
|
+
* targets. Programs are named (typically one per pgpm change), so ownership
|
|
8
|
+
* queries answer change-level questions directly:
|
|
9
|
+
*
|
|
10
|
+
* - which objects live in a schema set (subsystem selection)
|
|
11
|
+
* - which surviving statements still reach into a dropped object set
|
|
12
|
+
* (cascade safety — every in-edge must originate from a statement that is
|
|
13
|
+
* itself dropped, or be rebound)
|
|
14
|
+
* - which programs create nothing outside a dropped set (whole-change
|
|
15
|
+
* pruning by ownership, not survivor counting)
|
|
16
|
+
*
|
|
17
|
+
* Pure and I/O-free; built from `SqlProgram`s parsed once elsewhere.
|
|
18
|
+
*/
|
|
19
|
+
import type { SqlProgram } from '@pgpmjs/transform';
|
|
20
|
+
import { SqlObjectRef } from './refs';
|
|
21
|
+
/** Identifies one statement inside one named program. */
|
|
22
|
+
export interface StatementId {
|
|
23
|
+
program: string;
|
|
24
|
+
statement: number;
|
|
25
|
+
}
|
|
26
|
+
export type ObjectEdgeKind = 'reference' | 'body' | 'fk';
|
|
27
|
+
/** One reference from a statement to an object. */
|
|
28
|
+
export interface ObjectEdge {
|
|
29
|
+
from: StatementId;
|
|
30
|
+
/** Key of the referenced object (see `objectKey`). */
|
|
31
|
+
to: string;
|
|
32
|
+
kind: ObjectEdgeKind;
|
|
33
|
+
}
|
|
34
|
+
/** A database object and the statements that create it. */
|
|
35
|
+
export interface ObjectNode {
|
|
36
|
+
ref: SqlObjectRef;
|
|
37
|
+
createdBy: StatementId[];
|
|
38
|
+
}
|
|
39
|
+
export interface SqlObjectGraph {
|
|
40
|
+
/** The named programs the graph was built from. */
|
|
41
|
+
programs: Map<string, SqlProgram>;
|
|
42
|
+
/** Objects by key. */
|
|
43
|
+
objects: Map<string, ObjectNode>;
|
|
44
|
+
/** All reference edges. */
|
|
45
|
+
edges: ObjectEdge[];
|
|
46
|
+
/** Edges into each object, by object key. */
|
|
47
|
+
incoming: Map<string, ObjectEdge[]>;
|
|
48
|
+
}
|
|
49
|
+
export declare function objectKey(ref: SqlObjectRef): string;
|
|
50
|
+
export declare function objectLabel(ref: SqlObjectRef): string;
|
|
51
|
+
/**
|
|
52
|
+
* Build the object graph from named programs (typically change name →
|
|
53
|
+
* parsed deploy script).
|
|
54
|
+
*/
|
|
55
|
+
export declare function buildObjectGraph(programs: Map<string, SqlProgram> | Array<[string, SqlProgram]>): SqlObjectGraph;
|
|
56
|
+
/** Keys of every object created in one of the given schemas. */
|
|
57
|
+
export declare function objectsInSchemas(graph: SqlObjectGraph, schemas: Iterable<string>): Set<string>;
|
|
58
|
+
/**
|
|
59
|
+
* Edges into `dropped` objects whose source statement is not itself dropped.
|
|
60
|
+
* Each is a dangling dependency: dropping the object set is safe iff this is
|
|
61
|
+
* empty or every edge's referenced object is rebound to a replacement.
|
|
62
|
+
*/
|
|
63
|
+
export declare function danglingEdges(graph: SqlObjectGraph, dropped: Set<string>): ObjectEdge[];
|
|
64
|
+
/**
|
|
65
|
+
* Programs every one of whose object-creating statements creates only
|
|
66
|
+
* `dropped` objects — whole changes owned by the dropped set, prunable from
|
|
67
|
+
* the plan by ownership (read from the graph, not counted from survivors).
|
|
68
|
+
* Programs that create nothing are never pruned.
|
|
69
|
+
*/
|
|
70
|
+
export declare function prunablePrograms(graph: SqlObjectGraph, dropped: Set<string>): string[];
|
package/object-graph.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.objectKey = objectKey;
|
|
4
|
+
exports.objectLabel = objectLabel;
|
|
5
|
+
exports.buildObjectGraph = buildObjectGraph;
|
|
6
|
+
exports.objectsInSchemas = objectsInSchemas;
|
|
7
|
+
exports.danglingEdges = danglingEdges;
|
|
8
|
+
exports.prunablePrograms = prunablePrograms;
|
|
9
|
+
function objectKey(ref) {
|
|
10
|
+
return `${ref.schema ?? ''}\u0000${ref.name}`;
|
|
11
|
+
}
|
|
12
|
+
function objectLabel(ref) {
|
|
13
|
+
return ref.schema ? `${ref.schema}.${ref.name}` : ref.name;
|
|
14
|
+
}
|
|
15
|
+
function statementFacts(program, index) {
|
|
16
|
+
return program.statements[index].facts;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Build the object graph from named programs (typically change name →
|
|
20
|
+
* parsed deploy script).
|
|
21
|
+
*/
|
|
22
|
+
function buildObjectGraph(programs) {
|
|
23
|
+
const programMap = programs instanceof Map ? programs : new Map(programs);
|
|
24
|
+
const objects = new Map();
|
|
25
|
+
const edges = [];
|
|
26
|
+
const incoming = new Map();
|
|
27
|
+
const addEdge = (edge) => {
|
|
28
|
+
edges.push(edge);
|
|
29
|
+
const list = incoming.get(edge.to);
|
|
30
|
+
if (list)
|
|
31
|
+
list.push(edge);
|
|
32
|
+
else
|
|
33
|
+
incoming.set(edge.to, [edge]);
|
|
34
|
+
};
|
|
35
|
+
for (const [name, program] of programMap) {
|
|
36
|
+
program.statements.forEach((stmt, i) => {
|
|
37
|
+
const id = { program: name, statement: i };
|
|
38
|
+
for (const c of stmt.facts.creates) {
|
|
39
|
+
const key = objectKey(c);
|
|
40
|
+
const node = objects.get(key);
|
|
41
|
+
if (node)
|
|
42
|
+
node.createdBy.push(id);
|
|
43
|
+
else
|
|
44
|
+
objects.set(key, { ref: { schema: c.schema, name: c.name }, createdBy: [id] });
|
|
45
|
+
}
|
|
46
|
+
for (const r of stmt.facts.references) {
|
|
47
|
+
addEdge({ from: id, to: objectKey(r), kind: 'reference' });
|
|
48
|
+
}
|
|
49
|
+
for (const r of stmt.facts.bodyReferences) {
|
|
50
|
+
addEdge({ from: id, to: objectKey(r), kind: 'body' });
|
|
51
|
+
}
|
|
52
|
+
for (const t of stmt.facts.fkTargets) {
|
|
53
|
+
addEdge({ from: id, to: objectKey(t), kind: 'fk' });
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return { programs: programMap, objects, edges, incoming };
|
|
58
|
+
}
|
|
59
|
+
/** Keys of every object created in one of the given schemas. */
|
|
60
|
+
function objectsInSchemas(graph, schemas) {
|
|
61
|
+
const wanted = new Set(schemas);
|
|
62
|
+
const keys = new Set();
|
|
63
|
+
for (const [key, node] of graph.objects) {
|
|
64
|
+
if (node.ref.schema !== null && wanted.has(node.ref.schema))
|
|
65
|
+
keys.add(key);
|
|
66
|
+
}
|
|
67
|
+
return keys;
|
|
68
|
+
}
|
|
69
|
+
/** Statement ids that create at least one object in `dropped`. */
|
|
70
|
+
function droppedStatements(graph, dropped) {
|
|
71
|
+
const ids = new Set();
|
|
72
|
+
for (const key of dropped) {
|
|
73
|
+
const node = graph.objects.get(key);
|
|
74
|
+
if (!node)
|
|
75
|
+
continue;
|
|
76
|
+
for (const id of node.createdBy)
|
|
77
|
+
ids.add(`${id.program}\u0000${id.statement}`);
|
|
78
|
+
}
|
|
79
|
+
return ids;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Edges into `dropped` objects whose source statement is not itself dropped.
|
|
83
|
+
* Each is a dangling dependency: dropping the object set is safe iff this is
|
|
84
|
+
* empty or every edge's referenced object is rebound to a replacement.
|
|
85
|
+
*/
|
|
86
|
+
function danglingEdges(graph, dropped) {
|
|
87
|
+
const droppedStmts = droppedStatements(graph, dropped);
|
|
88
|
+
const dangling = [];
|
|
89
|
+
for (const key of dropped) {
|
|
90
|
+
for (const edge of graph.incoming.get(key) ?? []) {
|
|
91
|
+
if (!droppedStmts.has(`${edge.from.program}\u0000${edge.from.statement}`)) {
|
|
92
|
+
dangling.push(edge);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return dangling;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Programs every one of whose object-creating statements creates only
|
|
100
|
+
* `dropped` objects — whole changes owned by the dropped set, prunable from
|
|
101
|
+
* the plan by ownership (read from the graph, not counted from survivors).
|
|
102
|
+
* Programs that create nothing are never pruned.
|
|
103
|
+
*/
|
|
104
|
+
function prunablePrograms(graph, dropped) {
|
|
105
|
+
const prunable = [];
|
|
106
|
+
for (const [name, program] of graph.programs) {
|
|
107
|
+
let creating = 0;
|
|
108
|
+
let insideOnly = true;
|
|
109
|
+
program.statements.forEach((stmt, i) => {
|
|
110
|
+
if (stmt.facts.creates.length === 0)
|
|
111
|
+
return;
|
|
112
|
+
creating++;
|
|
113
|
+
const facts = statementFacts(program, i);
|
|
114
|
+
if (!facts.creates.every(c => dropped.has(objectKey(c))))
|
|
115
|
+
insideOnly = false;
|
|
116
|
+
});
|
|
117
|
+
if (creating > 0 && insideOnly)
|
|
118
|
+
prunable.push(name);
|
|
119
|
+
}
|
|
120
|
+
return prunable;
|
|
121
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpmjs/slice",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
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.
|
|
46
|
-
"@pgpmjs/transform": "^0.
|
|
45
|
+
"@pgpmjs/ast": "^0.10.0",
|
|
46
|
+
"@pgpmjs/transform": "^0.14.0",
|
|
47
47
|
"minimatch": "^10.2.5",
|
|
48
|
-
"plpgsql-parser": "^18.2.
|
|
48
|
+
"plpgsql-parser": "^18.2.2"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "622be766e8b1d5762b6f0536699d59f745a34c0c"
|
|
51
51
|
}
|