@pgpmjs/transform 0.19.0 → 0.20.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/constraint-names.d.ts +36 -0
- package/constraint-names.js +75 -0
- package/esm/constraint-names.js +71 -0
- package/esm/granularity-driver.js +84 -21
- package/esm/index.js +4 -2
- package/esm/module-emit.js +71 -1
- package/esm/restructure.js +6 -1
- package/esm/semantic-diff-driver.js +97 -6
- package/esm/sub-object.js +80 -0
- package/granularity-driver.d.ts +57 -0
- package/granularity-driver.js +86 -21
- package/index.d.ts +7 -4
- package/index.js +10 -1
- package/module-emit.d.ts +26 -0
- package/module-emit.js +72 -1
- package/package.json +3 -3
- package/restructure.d.ts +7 -0
- package/restructure.js +6 -1
- package/semantic-diff-driver.d.ts +3 -1
- package/semantic-diff-driver.js +97 -6
- package/sub-object.d.ts +31 -0
- package/sub-object.js +84 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Deparser, parseSync } from 'plpgsql-parser';
|
|
2
|
+
import { defaultConstraintName } from './constraint-names';
|
|
3
|
+
/** The single AlterTableCmd of a one-command ALTER TABLE, or null. */
|
|
4
|
+
function singleCmd(stmt) {
|
|
5
|
+
const cmds = stmt.cmds ?? [];
|
|
6
|
+
if (cmds.length !== 1)
|
|
7
|
+
return null;
|
|
8
|
+
return cmds[0].AlterTableCmd ?? null;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The column or constraint a one-command `ALTER TABLE .. ADD COLUMN` /
|
|
12
|
+
* `ADD CONSTRAINT` statement adds, as a naming-spec identity
|
|
13
|
+
* (`kind: 'column' | 'constraint'`, `table` set). Unnamed constraints get
|
|
14
|
+
* their Postgres default name. Returns `null` for anything else — multi-
|
|
15
|
+
* command ALTERs, other subtypes, non-ALTER statements — so callers fall
|
|
16
|
+
* back to object-level grouping.
|
|
17
|
+
*/
|
|
18
|
+
export function subObjectIdentityOf(facts) {
|
|
19
|
+
const stmt = facts.stmt?.AlterTableStmt;
|
|
20
|
+
if (!stmt?.relation?.relname)
|
|
21
|
+
return null;
|
|
22
|
+
const cmd = singleCmd(stmt);
|
|
23
|
+
if (!cmd)
|
|
24
|
+
return null;
|
|
25
|
+
const schema = stmt.relation.schemaname ?? null;
|
|
26
|
+
const table = stmt.relation.relname;
|
|
27
|
+
if (cmd.subtype === 'AT_AddColumn' && cmd.def?.ColumnDef?.colname) {
|
|
28
|
+
return { kind: 'column', schema, name: cmd.def.ColumnDef.colname, table };
|
|
29
|
+
}
|
|
30
|
+
if (cmd.subtype === 'AT_AddConstraint' && cmd.def?.Constraint) {
|
|
31
|
+
const constraint = cmd.def.Constraint;
|
|
32
|
+
const name = constraint.conname ?? defaultConstraintName(table, constraint);
|
|
33
|
+
if (!name)
|
|
34
|
+
return null;
|
|
35
|
+
return { kind: 'constraint', schema, name, table };
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Rewrite a script so every unnamed `ALTER TABLE .. ADD <constraint>` carries
|
|
41
|
+
* its Postgres default name (`ADD PRIMARY KEY (id)` becomes `ADD CONSTRAINT
|
|
42
|
+
* {table}_pkey PRIMARY KEY (id)`) — the exact name the catalog would assign
|
|
43
|
+
* anyway. Naming them makes each constraint change independently revertible
|
|
44
|
+
* (`DROP CONSTRAINT <name>`) and verifiable. Statements that need no rewrite
|
|
45
|
+
* keep their original text.
|
|
46
|
+
*/
|
|
47
|
+
export function nameUnnamedConstraints(sql) {
|
|
48
|
+
const parsed = parseSync(sql);
|
|
49
|
+
const pieces = [];
|
|
50
|
+
for (const raw of parsed.sql.stmts) {
|
|
51
|
+
const node = raw.stmt;
|
|
52
|
+
const stmt = node.AlterTableStmt;
|
|
53
|
+
const table = stmt?.relation?.relname;
|
|
54
|
+
let rewritten = false;
|
|
55
|
+
if (stmt && table) {
|
|
56
|
+
for (const cmd of stmt.cmds ?? []) {
|
|
57
|
+
const at = cmd.AlterTableCmd;
|
|
58
|
+
const constraint = at?.subtype === 'AT_AddConstraint' ? at.def?.Constraint : undefined;
|
|
59
|
+
if (constraint && !constraint.conname) {
|
|
60
|
+
const name = defaultConstraintName(table, constraint);
|
|
61
|
+
if (name) {
|
|
62
|
+
constraint.conname = name;
|
|
63
|
+
rewritten = true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (rewritten) {
|
|
69
|
+
pieces.push(`${Deparser.deparse(node, { pretty: true })};`);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
const start = raw.stmt_location ?? 0;
|
|
73
|
+
const len = raw.stmt_len;
|
|
74
|
+
const text = len === undefined ? sql.slice(start) : sql.slice(start, start + len);
|
|
75
|
+
const trimmed = text.trim();
|
|
76
|
+
pieces.push(trimmed.endsWith(';') ? trimmed : `${trimmed};`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return pieces.join('\n\n');
|
|
80
|
+
}
|
package/granularity-driver.d.ts
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Granularity driver: restructure a pgpm module's deploy surface between the
|
|
3
|
+
* atomic, object, and consolidated shapes.
|
|
4
|
+
*
|
|
5
|
+
* The upstream pass (`restructureSql` in `@pgsql/transform`) rewrites one SQL
|
|
6
|
+
* script between equivalent shapes, guarded by the statement dependency
|
|
7
|
+
* graph. This driver lifts that to the pgpm change model: it flattens a
|
|
8
|
+
* module's deploy scripts in plan order into one program, restructures it to
|
|
9
|
+
* the target granularity, then re-slices the result into changes — one change
|
|
10
|
+
* per created object — with change dependencies recomputed from the statement
|
|
11
|
+
* graph. Like the other drivers in this package it is structurally typed on
|
|
12
|
+
* the bundle seams: no dependency on `@pgpmjs/bundle` or `@pgpmjs/core`.
|
|
13
|
+
*
|
|
14
|
+
* - `atomic` — the machine-emitted shape: bare CREATE TABLE plus one
|
|
15
|
+
* ALTER per column/constraint.
|
|
16
|
+
* - `object` — each table fully baked; cross-object statements
|
|
17
|
+
* (FKs, indexes, triggers, policies) stay separate.
|
|
18
|
+
* - `consolidated` — additionally inlines FKs proven safe by the graph.
|
|
19
|
+
*/
|
|
20
|
+
import type { ObjectIdentity as NamingIdentity } from '@pgpmjs/naming-spec';
|
|
1
21
|
import type { Granularity, StatementFacts } from '@pgsql/transform';
|
|
2
22
|
export type { Granularity } from '@pgsql/transform';
|
|
23
|
+
/**
|
|
24
|
+
* How statements are distributed across pgpm changes — orthogonal to
|
|
25
|
+
* `Granularity`, which shapes the SQL *within* a change.
|
|
26
|
+
*
|
|
27
|
+
* - `alteration` — one change per alteration: every `ADD COLUMN` /
|
|
28
|
+
* `ADD CONSTRAINT` becomes its own plan entry with its own
|
|
29
|
+
* deploy/revert/verify and graph-derived requires, so a
|
|
30
|
+
* single column can deploy or revert independently.
|
|
31
|
+
* - `object` — one change per created object (the default): a table's
|
|
32
|
+
* CREATE and all its ALTERs share one plan entry.
|
|
33
|
+
* - `single` — one change for the whole module: every statement lands in
|
|
34
|
+
* a single plan entry with one deploy/revert/verify.
|
|
35
|
+
*/
|
|
36
|
+
export type ChangeGranularity = 'alteration' | 'object' | 'single';
|
|
37
|
+
export declare const CHANGE_GRANULARITIES: readonly ChangeGranularity[];
|
|
38
|
+
export declare const isChangeGranularity: (value: string) => value is ChangeGranularity;
|
|
3
39
|
/** A change's deploy surface going into or out of the restructure. */
|
|
4
40
|
export interface GranularityChange {
|
|
5
41
|
/** Change name (plan token, e.g. `schemas/app/tables/users/table`). */
|
|
@@ -26,6 +62,27 @@ export interface RestructuredChange extends GranularityChange {
|
|
|
26
62
|
}
|
|
27
63
|
export interface RestructureModuleOptions {
|
|
28
64
|
granularity: Granularity;
|
|
65
|
+
/**
|
|
66
|
+
* Change-level distribution (default `object`). With `alteration`, every
|
|
67
|
+
* single-command `ALTER TABLE ADD COLUMN` / `ADD CONSTRAINT` in the
|
|
68
|
+
* restructured script becomes its own change, named by the sub-object's
|
|
69
|
+
* naming-spec path (`.../columns/{name}/column`,
|
|
70
|
+
* `.../constraints/{name}/constraint`); unnamed constraints are first
|
|
71
|
+
* given their Postgres default name so each change stays revertible.
|
|
72
|
+
* With `single`, the whole restructured script becomes one change.
|
|
73
|
+
*/
|
|
74
|
+
changeGranularity?: ChangeGranularity;
|
|
75
|
+
/**
|
|
76
|
+
* Plan token for the single change (used only with `changeGranularity:
|
|
77
|
+
* 'single'`). Defaults to `module/init`.
|
|
78
|
+
*/
|
|
79
|
+
singleChangeName?: string;
|
|
80
|
+
/**
|
|
81
|
+
* Derive a change name for an alteration group from its sub-object
|
|
82
|
+
* identity (used only with `changeGranularity: 'alteration'`). Defaults to
|
|
83
|
+
* `pathFor` in `directory` style.
|
|
84
|
+
*/
|
|
85
|
+
subObjectName?: (identity: NamingIdentity) => string;
|
|
29
86
|
/**
|
|
30
87
|
* Derive a change name for a statement group from the facts of its primary
|
|
31
88
|
* (creating) statement. Defaults to {@link defaultChangeName}: naming spec
|
package/granularity-driver.js
CHANGED
|
@@ -1,29 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isChangeGranularity = exports.CHANGE_GRANULARITIES = void 0;
|
|
3
4
|
exports.defaultChangeName = defaultChangeName;
|
|
4
5
|
exports.restructureChanges = restructureChanges;
|
|
5
|
-
/**
|
|
6
|
-
* Granularity driver: restructure a pgpm module's deploy surface between the
|
|
7
|
-
* atomic, object, and consolidated shapes.
|
|
8
|
-
*
|
|
9
|
-
* The upstream pass (`restructureSql` in `@pgsql/transform`) rewrites one SQL
|
|
10
|
-
* script between equivalent shapes, guarded by the statement dependency
|
|
11
|
-
* graph. This driver lifts that to the pgpm change model: it flattens a
|
|
12
|
-
* module's deploy scripts in plan order into one program, restructures it to
|
|
13
|
-
* the target granularity, then re-slices the result into changes — one change
|
|
14
|
-
* per created object — with change dependencies recomputed from the statement
|
|
15
|
-
* graph. Like the other drivers in this package it is structurally typed on
|
|
16
|
-
* the bundle seams: no dependency on `@pgpmjs/bundle` or `@pgpmjs/core`.
|
|
17
|
-
*
|
|
18
|
-
* - `atomic` — the machine-emitted shape: bare CREATE TABLE plus one
|
|
19
|
-
* ALTER per column/constraint.
|
|
20
|
-
* - `object` — each table fully baked; cross-object statements
|
|
21
|
-
* (FKs, indexes, triggers, policies) stay separate.
|
|
22
|
-
* - `consolidated` — additionally inlines FKs proven safe by the graph.
|
|
23
|
-
*/
|
|
24
6
|
const naming_spec_1 = require("@pgpmjs/naming-spec");
|
|
25
7
|
const scripts_1 = require("@pgsql/scripts");
|
|
26
8
|
const transform_1 = require("@pgsql/transform");
|
|
9
|
+
const sub_object_1 = require("./sub-object");
|
|
10
|
+
exports.CHANGE_GRANULARITIES = ['alteration', 'object', 'single'];
|
|
11
|
+
const isChangeGranularity = (value) => exports.CHANGE_GRANULARITIES.includes(value);
|
|
12
|
+
exports.isChangeGranularity = isChangeGranularity;
|
|
27
13
|
const KIND_DIRS = {
|
|
28
14
|
table: 'tables',
|
|
29
15
|
view: 'views',
|
|
@@ -57,22 +43,50 @@ function defaultChangeName(facts) {
|
|
|
57
43
|
*/
|
|
58
44
|
function restructureChanges(changes, options) {
|
|
59
45
|
const nameFor = options.changeName ?? defaultChangeName;
|
|
46
|
+
const subNameFor = options.subObjectName ?? ((identity) => (0, naming_spec_1.pathFor)(identity));
|
|
47
|
+
const alteration = options.changeGranularity === 'alteration';
|
|
60
48
|
const flattened = changes
|
|
61
49
|
.map(c => c.deploy.trim())
|
|
62
50
|
.filter(Boolean)
|
|
63
51
|
.join('\n\n');
|
|
64
|
-
const
|
|
52
|
+
const restructured = (0, transform_1.restructureSql)(flattened, {
|
|
65
53
|
granularity: options.granularity
|
|
66
54
|
});
|
|
55
|
+
const warnings = restructured.warnings;
|
|
56
|
+
const sql = alteration ? (0, sub_object_1.nameUnnamedConstraints)(restructured.sql) : restructured.sql;
|
|
67
57
|
// Re-classify the emitted script; group statements by the object they
|
|
68
58
|
// target (creates[0]), so a table's CREATE and its remaining ALTERs land
|
|
69
59
|
// in the same change regardless of statement kind.
|
|
70
60
|
const facts = (0, transform_1.classifyStatements)(sql);
|
|
61
|
+
if (options.changeGranularity === 'single') {
|
|
62
|
+
const name = options.singleChangeName ?? 'module/init';
|
|
63
|
+
const inputNames = new Set(changes.map(c => c.name));
|
|
64
|
+
const dependencies = [...new Set(changes.flatMap(c => c.dependencies).filter(d => !inputNames.has(d)))].sort();
|
|
65
|
+
const statements = facts
|
|
66
|
+
.map(f => sql.slice(f.span.start, f.span.start + f.span.len).trim())
|
|
67
|
+
.filter(Boolean)
|
|
68
|
+
.map(text => (text.endsWith(';') ? text : `${text};`));
|
|
69
|
+
const revert = (0, scripts_1.revertFor)(facts);
|
|
70
|
+
const verify = (0, scripts_1.verifyFor)(facts);
|
|
71
|
+
for (const warning of [...revert.warnings, ...verify.warnings]) {
|
|
72
|
+
warnings.push(`${name}: ${warning}`);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
changes: statements.length > 0
|
|
76
|
+
? [{ name, dependencies, deploy: statements.join('\n\n'), revert: revert.sql, verify: verify.sql }]
|
|
77
|
+
: [],
|
|
78
|
+
warnings
|
|
79
|
+
};
|
|
80
|
+
}
|
|
71
81
|
const graph = (0, transform_1.buildStatementGraph)(facts);
|
|
72
82
|
const groupOf = new Array(facts.length).fill(-1);
|
|
73
83
|
const groupKeys = [];
|
|
74
84
|
const groupFacts = [];
|
|
75
85
|
const groupKeyToIndex = new Map();
|
|
86
|
+
/** Sub-object path for alteration groups; null means name via `nameFor`. */
|
|
87
|
+
const groupSubName = [];
|
|
88
|
+
/** For alteration groups: the object-group key of the owning table. */
|
|
89
|
+
const groupOwnerKey = [];
|
|
76
90
|
facts.forEach((f, i) => {
|
|
77
91
|
const created = f.creates[0];
|
|
78
92
|
if (!created) {
|
|
@@ -82,12 +96,31 @@ function restructureChanges(changes, options) {
|
|
|
82
96
|
groupOf[i] = groupOf[i - 1];
|
|
83
97
|
return;
|
|
84
98
|
}
|
|
99
|
+
if (alteration) {
|
|
100
|
+
const sub = (0, sub_object_1.subObjectIdentityOf)(f);
|
|
101
|
+
if (sub) {
|
|
102
|
+
const key = `sub\u0000${sub.kind}\u0000${sub.schema ?? ''}\u0000${sub.table}\u0000${sub.name}`;
|
|
103
|
+
let g = groupKeyToIndex.get(key);
|
|
104
|
+
if (g === undefined) {
|
|
105
|
+
g = groupKeys.length;
|
|
106
|
+
groupKeys.push(key);
|
|
107
|
+
groupFacts.push(f);
|
|
108
|
+
groupSubName.push(subNameFor(sub));
|
|
109
|
+
groupOwnerKey.push(`${sub.schema ?? ''}.${sub.table}`);
|
|
110
|
+
groupKeyToIndex.set(key, g);
|
|
111
|
+
}
|
|
112
|
+
groupOf[i] = g;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
85
116
|
const key = `${created.schema ?? ''}.${created.name}`;
|
|
86
117
|
let g = groupKeyToIndex.get(key);
|
|
87
118
|
if (g === undefined) {
|
|
88
119
|
g = groupKeys.length;
|
|
89
120
|
groupKeys.push(key);
|
|
90
121
|
groupFacts.push(f);
|
|
122
|
+
groupSubName.push(null);
|
|
123
|
+
groupOwnerKey.push(null);
|
|
91
124
|
groupKeyToIndex.set(key, g);
|
|
92
125
|
}
|
|
93
126
|
else if (!(groupFacts[g].kind in KIND_DIRS) && groupFacts[g].kind !== 'schema' && (f.kind in KIND_DIRS || f.kind === 'schema')) {
|
|
@@ -96,7 +129,7 @@ function restructureChanges(changes, options) {
|
|
|
96
129
|
}
|
|
97
130
|
groupOf[i] = g;
|
|
98
131
|
});
|
|
99
|
-
const groupNames = groupFacts.map(nameFor);
|
|
132
|
+
const groupNames = groupFacts.map((f, g) => groupSubName[g] ?? nameFor(f));
|
|
100
133
|
// Schema producers, for schema-level change dependencies.
|
|
101
134
|
const schemaGroup = new Map();
|
|
102
135
|
facts.forEach((f, i) => {
|
|
@@ -142,6 +175,38 @@ function restructureChanges(changes, options) {
|
|
|
142
175
|
groupDeps[from].add(to);
|
|
143
176
|
}
|
|
144
177
|
});
|
|
178
|
+
// Alteration groups depend on their table's own change, and constraint
|
|
179
|
+
// groups additionally on the column changes they key on — the statement
|
|
180
|
+
// graph keys everything by the table, so these finer edges are added here.
|
|
181
|
+
if (alteration) {
|
|
182
|
+
const columnGroup = new Map();
|
|
183
|
+
groupKeys.forEach((key, g) => {
|
|
184
|
+
const parts = key.split('\u0000');
|
|
185
|
+
if (parts[0] === 'sub' && parts[1] === 'column') {
|
|
186
|
+
columnGroup.set(`${parts[2]}.${parts[3]}.${parts[4]}`, g);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
groupKeys.forEach((key, g) => {
|
|
190
|
+
const owner = groupOwnerKey[g];
|
|
191
|
+
if (owner === null)
|
|
192
|
+
return;
|
|
193
|
+
const tableGroup = groupKeyToIndex.get(owner);
|
|
194
|
+
if (tableGroup !== undefined && tableGroup !== g)
|
|
195
|
+
groupDeps[g].add(tableGroup);
|
|
196
|
+
const parts = key.split('\u0000');
|
|
197
|
+
if (parts[1] !== 'constraint')
|
|
198
|
+
return;
|
|
199
|
+
const constraint = groupFacts[g].stmt?.AlterTableStmt?.cmds?.[0]?.AlterTableCmd?.def?.Constraint;
|
|
200
|
+
const referenced = [...(constraint?.keys ?? []), ...(constraint?.fk_attrs ?? [])]
|
|
201
|
+
.map(k => k.String?.sval)
|
|
202
|
+
.filter((s) => typeof s === 'string');
|
|
203
|
+
for (const col of referenced) {
|
|
204
|
+
const to = columnGroup.get(`${owner}.${col}`);
|
|
205
|
+
if (to !== undefined && to !== g)
|
|
206
|
+
groupDeps[g].add(to);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
145
210
|
// Emit groups in first-statement order (already topological).
|
|
146
211
|
const order = [...groupNames.keys()].sort((a, b) => {
|
|
147
212
|
const firstA = groupOf.indexOf(a);
|
package/index.d.ts
CHANGED
|
@@ -4,12 +4,14 @@ export type { CatalogQueryable, CatalogSnapshot } from './catalog-check';
|
|
|
4
4
|
export { diffCatalogSnapshots, snapshotCatalog, withoutColumnOrder } from './catalog-check';
|
|
5
5
|
export type { CategoryProfile, ChangeCategory, } from './categorize';
|
|
6
6
|
export { buildCategoryOf, categorizeChange, TIER_PROFILE, } from './categorize';
|
|
7
|
+
export type { ConstraintNode } from './constraint-names';
|
|
8
|
+
export { defaultConstraintName, firstColumnRef } from './constraint-names';
|
|
7
9
|
export type { ClosureChange, ClosureInputChange, ClosureReason, FixtureClosure, ResolveFixtureClosureOptions, } from './fixture-closure';
|
|
8
10
|
export { resolveFixtureClosure } from './fixture-closure';
|
|
9
|
-
export type { GranularityChange, RestructuredChange, RestructureModuleOptions, RestructureModuleResult, } from './granularity-driver';
|
|
10
|
-
export { defaultChangeName, restructureChanges, } from './granularity-driver';
|
|
11
|
-
export type { PgpmModuleModel } from './module-emit';
|
|
12
|
-
export { checkOverwrite, writeControlFile, writeModule } from './module-emit';
|
|
11
|
+
export type { ChangeGranularity, GranularityChange, RestructuredChange, RestructureModuleOptions, RestructureModuleResult, } from './granularity-driver';
|
|
12
|
+
export { CHANGE_GRANULARITIES, defaultChangeName, isChangeGranularity, restructureChanges, } from './granularity-driver';
|
|
13
|
+
export type { AppendModuleResult, PgpmModuleModel } from './module-emit';
|
|
14
|
+
export { appendModule, checkOverwrite, writeControlFile, writeModule } from './module-emit';
|
|
13
15
|
export type { ModuleSource, ModuleSourceChange } from './module-source';
|
|
14
16
|
export { loadModuleSource, stripTransactionWrapper } from './module-source';
|
|
15
17
|
export type { PartitionedPackageRows, PartitionExportRowsResult } from './partition';
|
|
@@ -24,5 +26,6 @@ export type { ExportGranularity, RestructureExportRowsOptions, RestructureExport
|
|
|
24
26
|
export { EXPORT_GRANULARITIES, isExportGranularity, restructureExportRows } from './restructure';
|
|
25
27
|
export type { DiffInputChange, ObjectDelta, SemanticDeltaChange, SemanticDiffOptions, SemanticDiffResult, SemanticObjectDiff, } from './semantic-diff-driver';
|
|
26
28
|
export { diffChangeSets, diffSchemas, } from './semantic-diff-driver';
|
|
29
|
+
export { nameUnnamedConstraints, subObjectIdentityOf } from './sub-object';
|
|
27
30
|
export * from '@pgsql/transform';
|
|
28
31
|
export { loadModule } from 'plpgsql-parser';
|
package/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.loadModule = exports.diffSchemas = exports.diffChangeSets = exports.restructureExportRows = exports.isExportGranularity = exports.EXPORT_GRANULARITIES = exports.regenerateScripts = exports.isStubScript = exports.classifyScript = exports.parseSqlProgram = exports.emitSqlProgram = exports.RESIDUAL_UNIT_PATH = exports.partitionUnits = exports.PartitionCycleError = exports.partitionExportRows = exports.parsePartitionConfig = exports.stripTransactionWrapper = exports.loadModuleSource = exports.writeModule = exports.writeControlFile = exports.checkOverwrite = exports.restructureChanges = exports.defaultChangeName = exports.resolveFixtureClosure = exports.TIER_PROFILE = exports.categorizeChange = exports.buildCategoryOf = exports.withoutColumnOrder = exports.snapshotCatalog = exports.diffCatalogSnapshots = exports.makeSchemaTranspiler = exports.makeNamespaceValidator = exports.buildSchemaRouter = exports.buildExtensionRouter = void 0;
|
|
17
|
+
exports.loadModule = exports.subObjectIdentityOf = exports.nameUnnamedConstraints = exports.diffSchemas = exports.diffChangeSets = exports.restructureExportRows = exports.isExportGranularity = exports.EXPORT_GRANULARITIES = exports.regenerateScripts = exports.isStubScript = exports.classifyScript = exports.parseSqlProgram = exports.emitSqlProgram = exports.RESIDUAL_UNIT_PATH = exports.partitionUnits = exports.PartitionCycleError = exports.partitionExportRows = exports.parsePartitionConfig = exports.stripTransactionWrapper = exports.loadModuleSource = exports.writeModule = exports.writeControlFile = exports.checkOverwrite = exports.appendModule = exports.restructureChanges = exports.isChangeGranularity = exports.defaultChangeName = exports.CHANGE_GRANULARITIES = exports.resolveFixtureClosure = exports.firstColumnRef = exports.defaultConstraintName = exports.TIER_PROFILE = exports.categorizeChange = exports.buildCategoryOf = exports.withoutColumnOrder = exports.snapshotCatalog = exports.diffCatalogSnapshots = exports.makeSchemaTranspiler = exports.makeNamespaceValidator = exports.buildSchemaRouter = exports.buildExtensionRouter = void 0;
|
|
18
18
|
var bundle_driver_1 = require("./bundle-driver");
|
|
19
19
|
Object.defineProperty(exports, "buildExtensionRouter", { enumerable: true, get: function () { return bundle_driver_1.buildExtensionRouter; } });
|
|
20
20
|
Object.defineProperty(exports, "buildSchemaRouter", { enumerable: true, get: function () { return bundle_driver_1.buildSchemaRouter; } });
|
|
@@ -28,12 +28,18 @@ var categorize_1 = require("./categorize");
|
|
|
28
28
|
Object.defineProperty(exports, "buildCategoryOf", { enumerable: true, get: function () { return categorize_1.buildCategoryOf; } });
|
|
29
29
|
Object.defineProperty(exports, "categorizeChange", { enumerable: true, get: function () { return categorize_1.categorizeChange; } });
|
|
30
30
|
Object.defineProperty(exports, "TIER_PROFILE", { enumerable: true, get: function () { return categorize_1.TIER_PROFILE; } });
|
|
31
|
+
var constraint_names_1 = require("./constraint-names");
|
|
32
|
+
Object.defineProperty(exports, "defaultConstraintName", { enumerable: true, get: function () { return constraint_names_1.defaultConstraintName; } });
|
|
33
|
+
Object.defineProperty(exports, "firstColumnRef", { enumerable: true, get: function () { return constraint_names_1.firstColumnRef; } });
|
|
31
34
|
var fixture_closure_1 = require("./fixture-closure");
|
|
32
35
|
Object.defineProperty(exports, "resolveFixtureClosure", { enumerable: true, get: function () { return fixture_closure_1.resolveFixtureClosure; } });
|
|
33
36
|
var granularity_driver_1 = require("./granularity-driver");
|
|
37
|
+
Object.defineProperty(exports, "CHANGE_GRANULARITIES", { enumerable: true, get: function () { return granularity_driver_1.CHANGE_GRANULARITIES; } });
|
|
34
38
|
Object.defineProperty(exports, "defaultChangeName", { enumerable: true, get: function () { return granularity_driver_1.defaultChangeName; } });
|
|
39
|
+
Object.defineProperty(exports, "isChangeGranularity", { enumerable: true, get: function () { return granularity_driver_1.isChangeGranularity; } });
|
|
35
40
|
Object.defineProperty(exports, "restructureChanges", { enumerable: true, get: function () { return granularity_driver_1.restructureChanges; } });
|
|
36
41
|
var module_emit_1 = require("./module-emit");
|
|
42
|
+
Object.defineProperty(exports, "appendModule", { enumerable: true, get: function () { return module_emit_1.appendModule; } });
|
|
37
43
|
Object.defineProperty(exports, "checkOverwrite", { enumerable: true, get: function () { return module_emit_1.checkOverwrite; } });
|
|
38
44
|
Object.defineProperty(exports, "writeControlFile", { enumerable: true, get: function () { return module_emit_1.writeControlFile; } });
|
|
39
45
|
Object.defineProperty(exports, "writeModule", { enumerable: true, get: function () { return module_emit_1.writeModule; } });
|
|
@@ -61,6 +67,9 @@ Object.defineProperty(exports, "restructureExportRows", { enumerable: true, get:
|
|
|
61
67
|
var semantic_diff_driver_1 = require("./semantic-diff-driver");
|
|
62
68
|
Object.defineProperty(exports, "diffChangeSets", { enumerable: true, get: function () { return semantic_diff_driver_1.diffChangeSets; } });
|
|
63
69
|
Object.defineProperty(exports, "diffSchemas", { enumerable: true, get: function () { return semantic_diff_driver_1.diffSchemas; } });
|
|
70
|
+
var sub_object_1 = require("./sub-object");
|
|
71
|
+
Object.defineProperty(exports, "nameUnnamedConstraints", { enumerable: true, get: function () { return sub_object_1.nameUnnamedConstraints; } });
|
|
72
|
+
Object.defineProperty(exports, "subObjectIdentityOf", { enumerable: true, get: function () { return sub_object_1.subObjectIdentityOf; } });
|
|
64
73
|
__exportStar(require("@pgsql/transform"), exports);
|
|
65
74
|
var plpgsql_parser_1 = require("plpgsql-parser");
|
|
66
75
|
Object.defineProperty(exports, "loadModule", { enumerable: true, get: function () { return plpgsql_parser_1.loadModule; } });
|
package/module-emit.d.ts
CHANGED
|
@@ -32,6 +32,32 @@ export declare const writeControlFile: (dir: string, name: string, requires: str
|
|
|
32
32
|
* leaves stale scripts behind. Returns the module directory.
|
|
33
33
|
*/
|
|
34
34
|
export declare const writeModule: (outBase: string, model: PgpmModuleModel, extraRequires?: string[]) => string;
|
|
35
|
+
/** Result of appending changes into an existing module (see {@link appendModule}). */
|
|
36
|
+
export interface AppendModuleResult {
|
|
37
|
+
/** The module directory that was appended to. */
|
|
38
|
+
dir: string;
|
|
39
|
+
/** Change names that were added to the plan. */
|
|
40
|
+
added: string[];
|
|
41
|
+
/** Change names skipped because they already exist in the plan. */
|
|
42
|
+
skipped: string[];
|
|
43
|
+
/** Non-fatal notices (skips, dropped dangling dependencies). */
|
|
44
|
+
warnings: string[];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Append plan-ordered {@link PgpmRow}s into an *existing* module rather than
|
|
48
|
+
* writing a fresh package. Existing changes, their scripts, and the `.control`
|
|
49
|
+
* file are left untouched; only the new changes are written (deploy/revert/
|
|
50
|
+
* verify) and appended to `pgpm.plan` after the current changes.
|
|
51
|
+
*
|
|
52
|
+
* Rows whose change name already exists are skipped (never overwritten).
|
|
53
|
+
* A new change's dependency bracket is filtered to names that resolve within
|
|
54
|
+
* the plan (existing changes, other appended changes, or `pkg:`-external
|
|
55
|
+
* references); a dangling internal dependency is dropped with a warning
|
|
56
|
+
* (plan order still sequences it after the current changes).
|
|
57
|
+
*/
|
|
58
|
+
export declare const appendModule: (moduleDir: string, rows: PgpmRow[], options?: {
|
|
59
|
+
author?: string;
|
|
60
|
+
}) => AppendModuleResult;
|
|
35
61
|
/**
|
|
36
62
|
* Guard against clobbering: writing into the source directory requires
|
|
37
63
|
* `--write`, as does overwriting any existing package directory.
|
package/module-emit.js
CHANGED
|
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.checkOverwrite = exports.writeModule = exports.writeControlFile = void 0;
|
|
36
|
+
exports.checkOverwrite = exports.appendModule = exports.writeModule = exports.writeControlFile = void 0;
|
|
37
37
|
/**
|
|
38
38
|
* Module emission — the single seam every dials command renders through.
|
|
39
39
|
*
|
|
@@ -86,6 +86,77 @@ const writeModule = (outBase, model, extraRequires = []) => {
|
|
|
86
86
|
return dir;
|
|
87
87
|
};
|
|
88
88
|
exports.writeModule = writeModule;
|
|
89
|
+
/**
|
|
90
|
+
* Append plan-ordered {@link PgpmRow}s into an *existing* module rather than
|
|
91
|
+
* writing a fresh package. Existing changes, their scripts, and the `.control`
|
|
92
|
+
* file are left untouched; only the new changes are written (deploy/revert/
|
|
93
|
+
* verify) and appended to `pgpm.plan` after the current changes.
|
|
94
|
+
*
|
|
95
|
+
* Rows whose change name already exists are skipped (never overwritten).
|
|
96
|
+
* A new change's dependency bracket is filtered to names that resolve within
|
|
97
|
+
* the plan (existing changes, other appended changes, or `pkg:`-external
|
|
98
|
+
* references); a dangling internal dependency is dropped with a warning
|
|
99
|
+
* (plan order still sequences it after the current changes).
|
|
100
|
+
*/
|
|
101
|
+
const appendModule = (moduleDir, rows, options = {}) => {
|
|
102
|
+
const planPath = path.join(moduleDir, 'pgpm.plan');
|
|
103
|
+
if (!fs.existsSync(planPath)) {
|
|
104
|
+
throw new Error(`No pgpm.plan found at ${planPath}; append mode expects an existing pgpm module directory.`);
|
|
105
|
+
}
|
|
106
|
+
const parsed = (0, ast_1.parsePlanFile)(planPath);
|
|
107
|
+
if (!parsed.data) {
|
|
108
|
+
throw new Error(`Failed to parse ${planPath}: ${parsed.errors
|
|
109
|
+
.map(e => `line ${e.line}: ${e.message}`)
|
|
110
|
+
.join('; ')}`);
|
|
111
|
+
}
|
|
112
|
+
const plan = parsed.data;
|
|
113
|
+
const existingNames = new Set(plan.changes.map(c => c.name));
|
|
114
|
+
const incomingNames = new Set(rows.map(r => r.deploy));
|
|
115
|
+
const appended = new Set();
|
|
116
|
+
const added = [];
|
|
117
|
+
const skipped = [];
|
|
118
|
+
const warnings = [];
|
|
119
|
+
const newRows = [];
|
|
120
|
+
const { fullName, email } = (0, ast_1.parseAuthor)(options.author || 'constructive');
|
|
121
|
+
const timestamp = new Date().toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
122
|
+
for (const row of rows) {
|
|
123
|
+
if (existingNames.has(row.deploy) || appended.has(row.deploy)) {
|
|
124
|
+
skipped.push(row.deploy);
|
|
125
|
+
warnings.push(`change ${row.deploy} already exists in ${plan.package}; left untouched`);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const deps = (row.deps ?? []).filter(dep => {
|
|
129
|
+
if (dep.includes(':'))
|
|
130
|
+
return true; // cross-package (pkg:change) external
|
|
131
|
+
if (existingNames.has(dep) || incomingNames.has(dep))
|
|
132
|
+
return true;
|
|
133
|
+
warnings.push(`change ${row.deploy}: dropped dependency ${dep} (not present in ${plan.package})`);
|
|
134
|
+
return false;
|
|
135
|
+
});
|
|
136
|
+
plan.changes.push({
|
|
137
|
+
name: row.deploy,
|
|
138
|
+
dependencies: deps,
|
|
139
|
+
timestamp,
|
|
140
|
+
planner: fullName,
|
|
141
|
+
email: email || `${fullName}@constructive.io`,
|
|
142
|
+
comment: `add ${row.name ?? row.deploy}`
|
|
143
|
+
});
|
|
144
|
+
newRows.push({ ...row, deps });
|
|
145
|
+
appended.add(row.deploy);
|
|
146
|
+
added.push(row.deploy);
|
|
147
|
+
}
|
|
148
|
+
if (newRows.length) {
|
|
149
|
+
const opts = {
|
|
150
|
+
outdir: path.dirname(moduleDir),
|
|
151
|
+
name: path.basename(moduleDir),
|
|
152
|
+
replacer: (str) => str
|
|
153
|
+
};
|
|
154
|
+
(0, ast_1.writePgpmFiles)(newRows, opts);
|
|
155
|
+
(0, ast_1.writePlanFile)(planPath, plan);
|
|
156
|
+
}
|
|
157
|
+
return { dir: moduleDir, added, skipped, warnings };
|
|
158
|
+
};
|
|
159
|
+
exports.appendModule = appendModule;
|
|
89
160
|
/**
|
|
90
161
|
* Guard against clobbering: writing into the source directory requires
|
|
91
162
|
* `--write`, as does overwriting any existing package directory.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpmjs/transform",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "Change-aware SQL transformation for PGPM — superset of @pgsql/transform plus change categorization, fixture closure, and bundle transpile/apply drivers",
|
|
6
6
|
"main": "index.js",
|
|
@@ -44,10 +44,10 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@pgpmjs/ast": "^0.10.3",
|
|
47
|
-
"@pgpmjs/naming-spec": "^0.
|
|
47
|
+
"@pgpmjs/naming-spec": "^0.2.0",
|
|
48
48
|
"@pgsql/scripts": "^18.3.1",
|
|
49
49
|
"@pgsql/transform": "^18.14.1",
|
|
50
50
|
"plpgsql-parser": "^18.5.1"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "e6a7a57f88c9deb2a4acecaba2451320d1be8203"
|
|
53
53
|
}
|
package/restructure.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { PgpmRow } from '@pgpmjs/ast';
|
|
9
9
|
import { PathStyle } from '@pgpmjs/naming-spec';
|
|
10
10
|
import { Granularity } from '@pgsql/transform';
|
|
11
|
+
import { ChangeGranularity } from './granularity-driver';
|
|
11
12
|
export type ExportGranularity = Granularity;
|
|
12
13
|
export declare const EXPORT_GRANULARITIES: readonly ExportGranularity[];
|
|
13
14
|
export declare const isExportGranularity: (value: unknown) => value is ExportGranularity;
|
|
@@ -18,6 +19,12 @@ export interface RestructureExportRowsResult {
|
|
|
18
19
|
export interface RestructureExportRowsOptions {
|
|
19
20
|
/** Naming-spec rendering style for derived paths (default `directory`). */
|
|
20
21
|
naming?: PathStyle;
|
|
22
|
+
/**
|
|
23
|
+
* Change-level distribution (default `object`): with `alteration`, each
|
|
24
|
+
* `ADD COLUMN` / `ADD CONSTRAINT` becomes its own change. See
|
|
25
|
+
* {@link ChangeGranularity}.
|
|
26
|
+
*/
|
|
27
|
+
changeGranularity?: ChangeGranularity;
|
|
21
28
|
}
|
|
22
29
|
/**
|
|
23
30
|
* Restructure exported migration rows to the target granularity.
|
package/restructure.js
CHANGED
|
@@ -37,7 +37,12 @@ const restructureExportRows = async (rows, granularity, options = {}) => {
|
|
|
37
37
|
name: row.deploy,
|
|
38
38
|
dependencies: row.deps ?? [],
|
|
39
39
|
deploy: row.content
|
|
40
|
-
})), {
|
|
40
|
+
})), {
|
|
41
|
+
granularity,
|
|
42
|
+
changeGranularity: options.changeGranularity,
|
|
43
|
+
changeName,
|
|
44
|
+
subObjectName: identity => (0, naming_spec_1.pathFor)(identity, { style })
|
|
45
|
+
});
|
|
41
46
|
const counters = new Map();
|
|
42
47
|
const taken = new Set();
|
|
43
48
|
const restructured = changes.map(change => {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
import { PathStyle } from '@pgpmjs/naming-spec';
|
|
30
30
|
import type { ObjectIdentity } from '@pgsql/transform';
|
|
31
31
|
import { Granularity } from '@pgsql/transform';
|
|
32
|
-
import { GranularityChange } from './granularity-driver';
|
|
32
|
+
import { ChangeGranularity, GranularityChange } from './granularity-driver';
|
|
33
33
|
/** How one object differs between the two sides. */
|
|
34
34
|
export type ObjectDelta = 'added' | 'removed' | 'modified';
|
|
35
35
|
export interface SemanticObjectDiff {
|
|
@@ -45,6 +45,8 @@ export interface SemanticObjectDiff {
|
|
|
45
45
|
export interface SemanticDiffOptions {
|
|
46
46
|
/** Granularity for the emitted CREATE/ALTER changes (default `object`). */
|
|
47
47
|
granularity?: Granularity;
|
|
48
|
+
/** Change-level distribution for emitted changes (default `object`). */
|
|
49
|
+
changeGranularity?: ChangeGranularity;
|
|
48
50
|
/** Naming-spec rendering style (default `directory`). */
|
|
49
51
|
style?: PathStyle;
|
|
50
52
|
}
|