@pgpmjs/slice 0.2.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/LICENSE +23 -0
- package/README.md +112 -0
- package/closure.d.ts +34 -0
- package/closure.js +124 -0
- package/esm/closure.js +120 -0
- package/esm/index.js +5 -0
- package/esm/output.js +117 -0
- package/esm/refs.js +36 -0
- package/esm/slice.js +520 -0
- package/esm/types.js +1 -0
- package/index.d.ts +5 -0
- package/index.js +21 -0
- package/output.d.ts +22 -0
- package/output.js +124 -0
- package/package.json +51 -0
- package/refs.d.ts +43 -0
- package/refs.js +41 -0
- package/slice.d.ts +59 -0
- package/slice.js +536 -0
- package/types.d.ts +204 -0
- package/types.js +2 -0
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pgpmjs/slice",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "PGPM plan slicing — split a monolithic pgpm plan into packages with pattern strategies and opt-in AST-based dependency closure",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/constructive",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/constructive"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/constructive/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"clean": "makage clean",
|
|
24
|
+
"prepack": "npm run build",
|
|
25
|
+
"build": "makage build",
|
|
26
|
+
"build:dev": "makage build --dev",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"sql",
|
|
33
|
+
"postgres",
|
|
34
|
+
"postgresql",
|
|
35
|
+
"pgpm",
|
|
36
|
+
"pgpmjs",
|
|
37
|
+
"slice",
|
|
38
|
+
"migration",
|
|
39
|
+
"plan"
|
|
40
|
+
],
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"makage": "^0.3.0"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@pgpmjs/ast": "^0.2.2",
|
|
46
|
+
"@pgpmjs/transform": "^0.2.0",
|
|
47
|
+
"minimatch": "^10.2.5",
|
|
48
|
+
"plpgsql-parser": "^18.2.1"
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "cc1b2af2c73c23b99a221e6bd8a8ba76c7ce79e6"
|
|
51
|
+
}
|
package/refs.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parser runs on a WASM build of the real PostgreSQL parser; callers must
|
|
3
|
+
* `await loadModule()` once before any synchronous extraction/slicing call.
|
|
4
|
+
*/
|
|
5
|
+
export { loadModule } from 'plpgsql-parser';
|
|
6
|
+
/**
|
|
7
|
+
* A (possibly schema-qualified) database object name extracted from SQL.
|
|
8
|
+
*/
|
|
9
|
+
export interface SqlObjectRef {
|
|
10
|
+
schema: string | null;
|
|
11
|
+
name: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* AST-derived facts about a change's deploy SQL, used to discover
|
|
15
|
+
* dependencies that are not declared in plan `requires` headers.
|
|
16
|
+
*/
|
|
17
|
+
export interface ChangeSqlFacts {
|
|
18
|
+
/** Objects the SQL creates (tables, views, functions, types, triggers, ...). */
|
|
19
|
+
creates: SqlObjectRef[];
|
|
20
|
+
/**
|
|
21
|
+
* Schema-qualified objects the SQL references — tables, functions and types
|
|
22
|
+
* reached anywhere in the statements, including PL/pgSQL function bodies.
|
|
23
|
+
* Unqualified references are omitted (they resolve via search_path and
|
|
24
|
+
* cannot be mapped to a producer without catalog knowledge).
|
|
25
|
+
*/
|
|
26
|
+
references: SqlObjectRef[];
|
|
27
|
+
/**
|
|
28
|
+
* True when a PL/pgSQL body executes dynamic SQL (EXECUTE ...). References
|
|
29
|
+
* inside the dynamic string are invisible to the AST, so the extracted
|
|
30
|
+
* references are incomplete for this change.
|
|
31
|
+
*/
|
|
32
|
+
dynamicSql: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Extract created objects and schema-qualified references from a SQL script,
|
|
36
|
+
* including references reached inside PL/pgSQL function bodies (which are
|
|
37
|
+
* opaque strings in `CREATE FUNCTION` and invisible to a plain SQL parse).
|
|
38
|
+
*
|
|
39
|
+
* Aggregates `@pgsql/transform`'s per-statement `classifyStatements` facts to
|
|
40
|
+
* the change level: references satisfied by objects the same change creates
|
|
41
|
+
* (in any statement) are dropped.
|
|
42
|
+
*/
|
|
43
|
+
export declare function extractSqlFacts(sql: string): ChangeSqlFacts;
|
package/refs.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadModule = void 0;
|
|
4
|
+
exports.extractSqlFacts = extractSqlFacts;
|
|
5
|
+
const transform_1 = require("@pgpmjs/transform");
|
|
6
|
+
/**
|
|
7
|
+
* The parser runs on a WASM build of the real PostgreSQL parser; callers must
|
|
8
|
+
* `await loadModule()` once before any synchronous extraction/slicing call.
|
|
9
|
+
*/
|
|
10
|
+
var plpgsql_parser_1 = require("plpgsql-parser");
|
|
11
|
+
Object.defineProperty(exports, "loadModule", { enumerable: true, get: function () { return plpgsql_parser_1.loadModule; } });
|
|
12
|
+
function pushUnique(list, r) {
|
|
13
|
+
if (list.some(x => x.schema === r.schema && x.name === r.name))
|
|
14
|
+
return;
|
|
15
|
+
list.push(r);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Extract created objects and schema-qualified references from a SQL script,
|
|
19
|
+
* including references reached inside PL/pgSQL function bodies (which are
|
|
20
|
+
* opaque strings in `CREATE FUNCTION` and invisible to a plain SQL parse).
|
|
21
|
+
*
|
|
22
|
+
* Aggregates `@pgsql/transform`'s per-statement `classifyStatements` facts to
|
|
23
|
+
* the change level: references satisfied by objects the same change creates
|
|
24
|
+
* (in any statement) are dropped.
|
|
25
|
+
*/
|
|
26
|
+
function extractSqlFacts(sql) {
|
|
27
|
+
const facts = { creates: [], references: [], dynamicSql: false };
|
|
28
|
+
for (const stmt of (0, transform_1.classifyStatements)(sql)) {
|
|
29
|
+
for (const c of stmt.creates) {
|
|
30
|
+
pushUnique(facts.creates, { schema: c.schema, name: c.name });
|
|
31
|
+
}
|
|
32
|
+
for (const r of stmt.references) {
|
|
33
|
+
pushUnique(facts.references, { schema: r.schema, name: r.name });
|
|
34
|
+
}
|
|
35
|
+
if (stmt.dynamicSql)
|
|
36
|
+
facts.dynamicSql = true;
|
|
37
|
+
}
|
|
38
|
+
// A change does not depend on objects it creates itself.
|
|
39
|
+
facts.references = facts.references.filter(r => !facts.creates.some(c => c.schema === r.schema && c.name === r.name));
|
|
40
|
+
return facts;
|
|
41
|
+
}
|
package/slice.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Change, Tag, ExtendedPlanFile } from '@pgpmjs/ast/files/types';
|
|
2
|
+
import { SliceConfig, SliceResult, DependencyGraph, PackageOutput, GroupingStrategy, PatternStrategy, CrossPackageDepMode } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Build a dependency graph from a parsed plan file
|
|
5
|
+
*/
|
|
6
|
+
export declare function buildDependencyGraph(plan: ExtendedPlanFile): DependencyGraph;
|
|
7
|
+
/**
|
|
8
|
+
* Validate that the dependency graph is a DAG (no cycles)
|
|
9
|
+
*/
|
|
10
|
+
export declare function validateDAG(graph: DependencyGraph): void;
|
|
11
|
+
/**
|
|
12
|
+
* Extract package name from a change path using folder-based strategy
|
|
13
|
+
*/
|
|
14
|
+
export declare function extractPackageFromPath(changeName: string, depth?: number, prefixToStrip?: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Find the matching package for a change using pattern-based strategy.
|
|
17
|
+
* Returns the first matching slice's package name, or undefined if no match.
|
|
18
|
+
*/
|
|
19
|
+
export declare function findMatchingPattern(changeName: string, strategy: PatternStrategy): string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Assign changes to packages based on grouping strategy
|
|
22
|
+
*/
|
|
23
|
+
export declare function assignChangesToPackages(graph: DependencyGraph, strategy: GroupingStrategy, defaultPackage?: string): Map<string, Set<string>>;
|
|
24
|
+
/**
|
|
25
|
+
* Merge small packages into larger ones
|
|
26
|
+
*/
|
|
27
|
+
export declare function mergeSmallPackages(assignments: Map<string, Set<string>>, minChanges: number, defaultPackage?: string): Map<string, Set<string>>;
|
|
28
|
+
/**
|
|
29
|
+
* Build package-level dependency graph
|
|
30
|
+
*/
|
|
31
|
+
export declare function buildPackageDependencies(graph: DependencyGraph, assignments: Map<string, Set<string>>, extraEdges?: Map<string, Map<string, string>>): Map<string, Set<string>>;
|
|
32
|
+
/**
|
|
33
|
+
* Detect cycles in package dependency graph
|
|
34
|
+
*/
|
|
35
|
+
export declare function detectPackageCycle(deps: Map<string, Set<string>>): string[] | null;
|
|
36
|
+
/**
|
|
37
|
+
* Compute deployment order for packages (topological sort)
|
|
38
|
+
*/
|
|
39
|
+
export declare function computeDeployOrder(packageDeps: Map<string, Set<string>>): string[];
|
|
40
|
+
/**
|
|
41
|
+
* Topological sort of changes within a package
|
|
42
|
+
*/
|
|
43
|
+
export declare function topologicalSortWithinPackage(changes: Set<string>, graph: DependencyGraph): string[];
|
|
44
|
+
/**
|
|
45
|
+
* Generate plan file content for a package
|
|
46
|
+
*/
|
|
47
|
+
export declare function generatePlanContent(pkgName: string, entries: Change[], tags?: Tag[]): string;
|
|
48
|
+
/**
|
|
49
|
+
* Generate control file content for a package
|
|
50
|
+
*/
|
|
51
|
+
export declare function generateControlContent(pkgName: string, deps: Set<string>): string;
|
|
52
|
+
/**
|
|
53
|
+
* Generate a single package output
|
|
54
|
+
*/
|
|
55
|
+
export declare function generateSinglePackage(pkgName: string, changes: Set<string>, graph: DependencyGraph, changeToPackage: Map<string, string>, pkgDeps: Set<string>, crossPackageDeps?: boolean | CrossPackageDepMode): PackageOutput;
|
|
56
|
+
/**
|
|
57
|
+
* Main slicing function
|
|
58
|
+
*/
|
|
59
|
+
export declare function slicePlan(config: SliceConfig): SliceResult;
|