@pgpmjs/core 7.14.0 → 7.16.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/apply/apply-spec.js +84 -9
- package/apply/index.d.ts +1 -0
- package/apply/index.js +1 -0
- package/apply/materialize.js +15 -2
- package/apply/reuse.d.ts +52 -0
- package/apply/reuse.js +226 -0
- package/apply/types.d.ts +89 -0
- package/bundle/artifact.d.ts +35 -0
- package/bundle/artifact.js +96 -0
- package/bundle/deploy-bundled.d.ts +71 -0
- package/bundle/deploy-bundled.js +177 -0
- package/bundle/index.d.ts +2 -0
- package/bundle/index.js +4 -0
- package/core/class/pgpm.js +54 -57
- package/esm/apply/apply-spec.js +84 -9
- package/esm/apply/index.js +1 -0
- package/esm/apply/materialize.js +15 -2
- package/esm/apply/reuse.js +221 -0
- package/esm/bundle/artifact.js +89 -0
- package/esm/bundle/deploy-bundled.js +170 -0
- package/esm/bundle/index.js +4 -0
- package/esm/core/class/pgpm.js +54 -57
- package/esm/extensions/compile.js +109 -0
- package/esm/extensions/index.js +4 -0
- package/esm/extensions/manifest.js +139 -0
- package/esm/extensions/resolve-install.js +70 -0
- package/esm/index.js +1 -1
- package/esm/migrate/clean.js +1 -2
- package/esm/packaging/package.js +8 -1
- package/esm/projects/deploy.js +36 -58
- package/extensions/compile.d.ts +35 -0
- package/extensions/compile.js +113 -0
- package/extensions/index.d.ts +4 -0
- package/extensions/index.js +20 -0
- package/extensions/manifest.d.ts +78 -0
- package/extensions/manifest.js +144 -0
- package/extensions/resolve-install.d.ts +20 -0
- package/extensions/resolve-install.js +74 -0
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/migrate/clean.js +1 -2
- package/package.json +10 -10
- package/packaging/package.d.ts +7 -1
- package/packaging/package.js +8 -1
- package/projects/deploy.js +36 -58
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ExtensionProvide } from './manifest';
|
|
2
|
+
/** Result of compiling one `provides` entry into deployable SQL. */
|
|
3
|
+
export interface CompiledExtensionInstall {
|
|
4
|
+
/** Deploy statements, in order (schema, extension, grants). */
|
|
5
|
+
deploy: string[];
|
|
6
|
+
/** Revert statements, in reverse order (drop extension, optionally schema). */
|
|
7
|
+
revert: string[];
|
|
8
|
+
/** Verify statements that error when the install is absent. */
|
|
9
|
+
verify: string[];
|
|
10
|
+
}
|
|
11
|
+
export interface CompileExtensionInstallOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Optional role-name map applied to grant targets (workspace portability
|
|
14
|
+
* profile). Renames identifiers only — never touches privilege semantics.
|
|
15
|
+
* Special roles (`PUBLIC`, `CURRENT_USER`, `SESSION_USER`) pass through.
|
|
16
|
+
*/
|
|
17
|
+
roleMap?: Record<string, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Control-file schema for a fixed-schema (non-relocatable) extension, when
|
|
20
|
+
* known. Used to validate that a `false`-relocatable extension is only
|
|
21
|
+
* "routed" to its own fixed schema.
|
|
22
|
+
*/
|
|
23
|
+
fixedSchema?: string | null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Compile a single `provides` declaration into deploy / revert / verify SQL.
|
|
27
|
+
*
|
|
28
|
+
* Pure and deterministic: given the same input it returns byte-identical SQL,
|
|
29
|
+
* with no dynamic `EXECUTE`. This is the nuts-and-bolts primitive; wiring it
|
|
30
|
+
* into the deploy path (and eventually into real plan changes exempt from
|
|
31
|
+
* `filterStatements` stripping) composes on top of it.
|
|
32
|
+
*/
|
|
33
|
+
export declare function compileExtensionInstall(extname: string, provide: ExtensionProvide, options?: CompileExtensionInstallOptions): CompiledExtensionInstall;
|
|
34
|
+
/** Compile every `provides` entry in a manifest, keyed by extension name. */
|
|
35
|
+
export declare function compileExtensionInstalls(provides: Record<string, ExtensionProvide>, options?: CompileExtensionInstallOptions): Record<string, CompiledExtensionInstall>;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compileExtensionInstall = compileExtensionInstall;
|
|
4
|
+
exports.compileExtensionInstalls = compileExtensionInstalls;
|
|
5
|
+
const SPECIAL_ROLES = new Set(['public', 'current_user', 'session_user']);
|
|
6
|
+
const quoteIdent = (name) => `"${name.replace(/"/g, '""')}"`;
|
|
7
|
+
const routeRole = (role, roleMap) => {
|
|
8
|
+
if (!roleMap)
|
|
9
|
+
return role;
|
|
10
|
+
if (SPECIAL_ROLES.has(role.toLowerCase()))
|
|
11
|
+
return role;
|
|
12
|
+
return roleMap[role] ?? role;
|
|
13
|
+
};
|
|
14
|
+
const grantObjectClause = (on, schema) => {
|
|
15
|
+
const s = quoteIdent(schema);
|
|
16
|
+
switch (on) {
|
|
17
|
+
case 'schema':
|
|
18
|
+
return `SCHEMA ${s}`;
|
|
19
|
+
case 'all-tables':
|
|
20
|
+
return `ALL TABLES IN SCHEMA ${s}`;
|
|
21
|
+
case 'all-sequences':
|
|
22
|
+
return `ALL SEQUENCES IN SCHEMA ${s}`;
|
|
23
|
+
case 'all-functions':
|
|
24
|
+
return `ALL FUNCTIONS IN SCHEMA ${s}`;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const grantSql = (verb, grant, schema, roleMap) => {
|
|
28
|
+
const roles = (Array.isArray(grant.to) ? grant.to : [grant.to])
|
|
29
|
+
.map((r) => routeRole(r, roleMap))
|
|
30
|
+
.join(', ');
|
|
31
|
+
const obj = grantObjectClause(grant.on, schema);
|
|
32
|
+
return verb === 'GRANT'
|
|
33
|
+
? `GRANT ${grant.privileges} ON ${obj} TO ${roles};`
|
|
34
|
+
: `REVOKE ${grant.privileges} ON ${obj} FROM ${roles};`;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Compile a single `provides` declaration into deploy / revert / verify SQL.
|
|
38
|
+
*
|
|
39
|
+
* Pure and deterministic: given the same input it returns byte-identical SQL,
|
|
40
|
+
* with no dynamic `EXECUTE`. This is the nuts-and-bolts primitive; wiring it
|
|
41
|
+
* into the deploy path (and eventually into real plan changes exempt from
|
|
42
|
+
* `filterStatements` stripping) composes on top of it.
|
|
43
|
+
*/
|
|
44
|
+
function compileExtensionInstall(extname, provide, options = {}) {
|
|
45
|
+
const { roleMap, fixedSchema } = options;
|
|
46
|
+
const schema = provide.schema === undefined ? null : provide.schema;
|
|
47
|
+
const relocatable = provide.relocatable ?? true;
|
|
48
|
+
const ifNotExists = provide.ifNotExists ?? true;
|
|
49
|
+
const cascade = provide.cascade ?? false;
|
|
50
|
+
const createSchema = provide.createSchema ?? schema !== null;
|
|
51
|
+
const dropSchema = provide.dropSchema ?? false;
|
|
52
|
+
const grants = provide.grants ?? [];
|
|
53
|
+
// A fixed-schema extension can only be placed at its own fixed schema.
|
|
54
|
+
if (!relocatable && schema !== null && fixedSchema != null && schema !== fixedSchema) {
|
|
55
|
+
throw new Error(`Extension "${extname}" is non-relocatable (fixed schema "${fixedSchema}") and cannot be installed into "${schema}".`);
|
|
56
|
+
}
|
|
57
|
+
if (grants.length > 0 && schema === null) {
|
|
58
|
+
throw new Error(`Extension "${extname}" declares grants but no schema; schema-qualified grants require an install schema.`);
|
|
59
|
+
}
|
|
60
|
+
const ext = quoteIdent(extname);
|
|
61
|
+
const deploy = [];
|
|
62
|
+
const revert = [];
|
|
63
|
+
const verify = [];
|
|
64
|
+
if (schema !== null && createSchema) {
|
|
65
|
+
deploy.push(`CREATE SCHEMA IF NOT EXISTS ${quoteIdent(schema)};`);
|
|
66
|
+
}
|
|
67
|
+
const createParts = ['CREATE EXTENSION'];
|
|
68
|
+
if (ifNotExists)
|
|
69
|
+
createParts.push('IF NOT EXISTS');
|
|
70
|
+
createParts.push(ext);
|
|
71
|
+
if (schema !== null)
|
|
72
|
+
createParts.push(`WITH SCHEMA ${quoteIdent(schema)}`);
|
|
73
|
+
if (cascade)
|
|
74
|
+
createParts.push('CASCADE');
|
|
75
|
+
deploy.push(`${createParts.join(' ')};`);
|
|
76
|
+
for (const g of grants) {
|
|
77
|
+
deploy.push(grantSql('GRANT', g, schema, roleMap));
|
|
78
|
+
}
|
|
79
|
+
// Revert: drop grants (best-effort), then extension, then optionally schema.
|
|
80
|
+
for (const g of [...grants].reverse()) {
|
|
81
|
+
revert.push(grantSql('REVOKE', g, schema, roleMap));
|
|
82
|
+
}
|
|
83
|
+
revert.push(`DROP EXTENSION IF EXISTS ${ext};`);
|
|
84
|
+
if (schema !== null && createSchema && dropSchema) {
|
|
85
|
+
revert.push(`DROP SCHEMA IF EXISTS ${quoteIdent(schema)};`);
|
|
86
|
+
}
|
|
87
|
+
// Verify: assert the extension exists in the expected namespace.
|
|
88
|
+
if (schema !== null) {
|
|
89
|
+
verify.push([
|
|
90
|
+
'SELECT 1 / count(*)::int AS ok',
|
|
91
|
+
'FROM pg_catalog.pg_extension e',
|
|
92
|
+
'JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace',
|
|
93
|
+
`WHERE e.extname = ${quoteLiteral(extname)} AND n.nspname = ${quoteLiteral(schema)};`
|
|
94
|
+
].join('\n'));
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
verify.push([
|
|
98
|
+
'SELECT 1 / count(*)::int AS ok',
|
|
99
|
+
'FROM pg_catalog.pg_extension e',
|
|
100
|
+
`WHERE e.extname = ${quoteLiteral(extname)};`
|
|
101
|
+
].join('\n'));
|
|
102
|
+
}
|
|
103
|
+
return { deploy, revert, verify };
|
|
104
|
+
}
|
|
105
|
+
const quoteLiteral = (s) => `'${s.replace(/'/g, "''")}'`;
|
|
106
|
+
/** Compile every `provides` entry in a manifest, keyed by extension name. */
|
|
107
|
+
function compileExtensionInstalls(provides, options = {}) {
|
|
108
|
+
const out = {};
|
|
109
|
+
for (const [extname, provide] of Object.entries(provides)) {
|
|
110
|
+
out[extname] = compileExtensionInstall(extname, provide, options);
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./compile"), exports);
|
|
18
|
+
__exportStar(require("./extensions"), exports);
|
|
19
|
+
__exportStar(require("./manifest"), exports);
|
|
20
|
+
__exportStar(require("./resolve-install"), exports);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative extension manifest (`extensions.json`) that lives next to a
|
|
3
|
+
* module's `pgpm.plan` / `.control`. It makes a module self-describing about
|
|
4
|
+
* the PostgreSQL extensions it relates to:
|
|
5
|
+
*
|
|
6
|
+
* - `provides` — the module *is* the extension wrapper: it installs the
|
|
7
|
+
* extension into a chosen schema (with grants). This is what replaces the
|
|
8
|
+
* dynamic `DO $$ EXECUTE 'CREATE EXTENSION ... SCHEMA x' $$` hack, since a
|
|
9
|
+
* literal `CREATE EXTENSION` is stripped at package time.
|
|
10
|
+
* - `consumes` — the module merely *uses* symbols from an extension installed
|
|
11
|
+
* elsewhere (e.g. a bare `crypt()` call) and needs them qualified to wherever
|
|
12
|
+
* the extension actually landed. This is the transform/routing side.
|
|
13
|
+
*
|
|
14
|
+
* `.control` `requires` still names the extension (dependency ordering is
|
|
15
|
+
* unchanged); this manifest only adds the *where + grants* that `.control`
|
|
16
|
+
* cannot express.
|
|
17
|
+
*/
|
|
18
|
+
/** Candidate file names, in precedence order. */
|
|
19
|
+
export declare const EXTENSIONS_MANIFEST_FILES: readonly ["pgpm.extensions.json", "extensions.json"];
|
|
20
|
+
/**
|
|
21
|
+
* What a declared grant targets. Kept structural (not free-form SQL) so it can
|
|
22
|
+
* be classified, role-routed, and reverted deterministically.
|
|
23
|
+
*/
|
|
24
|
+
export type ExtensionGrantTarget = 'schema' | 'all-tables' | 'all-sequences' | 'all-functions';
|
|
25
|
+
export interface ExtensionGrant {
|
|
26
|
+
/** Privilege list, e.g. `USAGE`, `ALL`, `EXECUTE`, `SELECT`. */
|
|
27
|
+
privileges: string;
|
|
28
|
+
/** Object class the grant applies to, resolved against the install schema. */
|
|
29
|
+
on: ExtensionGrantTarget;
|
|
30
|
+
/** Role name(s). Routed through the workspace role map when one is supplied. */
|
|
31
|
+
to: string | string[];
|
|
32
|
+
}
|
|
33
|
+
export interface ExtensionProvide {
|
|
34
|
+
/**
|
|
35
|
+
* Target schema for the install. A string routes the extension there; `null`
|
|
36
|
+
* means default/unqualified (control-file location, typically `public`).
|
|
37
|
+
*/
|
|
38
|
+
schema?: string | null;
|
|
39
|
+
/**
|
|
40
|
+
* Author's assertion that the extension is relocatable. Defaults to `true`.
|
|
41
|
+
* A fixed-schema (`false`) extension must not be routed to a schema that
|
|
42
|
+
* differs from its control-file schema — {@link compileExtensionInstall}
|
|
43
|
+
* refuses that combination.
|
|
44
|
+
*/
|
|
45
|
+
relocatable?: boolean;
|
|
46
|
+
/** Emit `IF NOT EXISTS` on `CREATE EXTENSION`. Defaults to `true`. */
|
|
47
|
+
ifNotExists?: boolean;
|
|
48
|
+
/** Emit `CASCADE` on `CREATE EXTENSION`. Defaults to `false`. */
|
|
49
|
+
cascade?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Create `schema` before installing (idempotent). Defaults to `true` when a
|
|
52
|
+
* schema is given. Set `false` when the schema is owned by another module.
|
|
53
|
+
*/
|
|
54
|
+
createSchema?: boolean;
|
|
55
|
+
/** Also `DROP SCHEMA` on revert. Defaults to `false` (only drop the extension). */
|
|
56
|
+
dropSchema?: boolean;
|
|
57
|
+
/** Grants applied after install. */
|
|
58
|
+
grants?: ExtensionGrant[];
|
|
59
|
+
}
|
|
60
|
+
export interface ExtensionConsume {
|
|
61
|
+
/** Bare symbol names this module references (functions/types/operators). */
|
|
62
|
+
symbols?: string[];
|
|
63
|
+
}
|
|
64
|
+
export interface ExtensionsManifest {
|
|
65
|
+
provides?: Record<string, ExtensionProvide>;
|
|
66
|
+
consumes?: Record<string, ExtensionConsume>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Validate + normalize a parsed manifest object. Throws with a precise message
|
|
70
|
+
* on malformed input so authors get actionable errors rather than silent
|
|
71
|
+
* misconfiguration.
|
|
72
|
+
*/
|
|
73
|
+
export declare function validateExtensionsManifest(raw: unknown, source?: string): ExtensionsManifest;
|
|
74
|
+
/**
|
|
75
|
+
* Read + validate the `extensions.json` (or `pgpm.extensions.json`) manifest for
|
|
76
|
+
* a module directory. Returns `undefined` when the module declares no manifest.
|
|
77
|
+
*/
|
|
78
|
+
export declare function readExtensionsManifest(moduleDir: string): ExtensionsManifest | undefined;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EXTENSIONS_MANIFEST_FILES = void 0;
|
|
4
|
+
exports.validateExtensionsManifest = validateExtensionsManifest;
|
|
5
|
+
exports.readExtensionsManifest = readExtensionsManifest;
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
/**
|
|
9
|
+
* Declarative extension manifest (`extensions.json`) that lives next to a
|
|
10
|
+
* module's `pgpm.plan` / `.control`. It makes a module self-describing about
|
|
11
|
+
* the PostgreSQL extensions it relates to:
|
|
12
|
+
*
|
|
13
|
+
* - `provides` — the module *is* the extension wrapper: it installs the
|
|
14
|
+
* extension into a chosen schema (with grants). This is what replaces the
|
|
15
|
+
* dynamic `DO $$ EXECUTE 'CREATE EXTENSION ... SCHEMA x' $$` hack, since a
|
|
16
|
+
* literal `CREATE EXTENSION` is stripped at package time.
|
|
17
|
+
* - `consumes` — the module merely *uses* symbols from an extension installed
|
|
18
|
+
* elsewhere (e.g. a bare `crypt()` call) and needs them qualified to wherever
|
|
19
|
+
* the extension actually landed. This is the transform/routing side.
|
|
20
|
+
*
|
|
21
|
+
* `.control` `requires` still names the extension (dependency ordering is
|
|
22
|
+
* unchanged); this manifest only adds the *where + grants* that `.control`
|
|
23
|
+
* cannot express.
|
|
24
|
+
*/
|
|
25
|
+
/** Candidate file names, in precedence order. */
|
|
26
|
+
exports.EXTENSIONS_MANIFEST_FILES = ['pgpm.extensions.json', 'extensions.json'];
|
|
27
|
+
const isPlainObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
28
|
+
const GRANT_TARGETS = new Set([
|
|
29
|
+
'schema',
|
|
30
|
+
'all-tables',
|
|
31
|
+
'all-sequences',
|
|
32
|
+
'all-functions'
|
|
33
|
+
]);
|
|
34
|
+
/**
|
|
35
|
+
* Validate + normalize a parsed manifest object. Throws with a precise message
|
|
36
|
+
* on malformed input so authors get actionable errors rather than silent
|
|
37
|
+
* misconfiguration.
|
|
38
|
+
*/
|
|
39
|
+
function validateExtensionsManifest(raw, source = 'extensions.json') {
|
|
40
|
+
if (!isPlainObject(raw)) {
|
|
41
|
+
throw new Error(`${source}: manifest must be a JSON object`);
|
|
42
|
+
}
|
|
43
|
+
const manifest = {};
|
|
44
|
+
if (raw.provides !== undefined) {
|
|
45
|
+
if (!isPlainObject(raw.provides)) {
|
|
46
|
+
throw new Error(`${source}: "provides" must be an object keyed by extension name`);
|
|
47
|
+
}
|
|
48
|
+
const provides = {};
|
|
49
|
+
for (const [extname, value] of Object.entries(raw.provides)) {
|
|
50
|
+
provides[extname] = validateProvide(extname, value, source);
|
|
51
|
+
}
|
|
52
|
+
manifest.provides = provides;
|
|
53
|
+
}
|
|
54
|
+
if (raw.consumes !== undefined) {
|
|
55
|
+
if (!isPlainObject(raw.consumes)) {
|
|
56
|
+
throw new Error(`${source}: "consumes" must be an object keyed by extension name`);
|
|
57
|
+
}
|
|
58
|
+
const consumes = {};
|
|
59
|
+
for (const [extname, value] of Object.entries(raw.consumes)) {
|
|
60
|
+
if (!isPlainObject(value)) {
|
|
61
|
+
throw new Error(`${source}: consumes."${extname}" must be an object`);
|
|
62
|
+
}
|
|
63
|
+
const entry = {};
|
|
64
|
+
if (value.symbols !== undefined) {
|
|
65
|
+
if (!Array.isArray(value.symbols) || value.symbols.some((s) => typeof s !== 'string' || !s.trim())) {
|
|
66
|
+
throw new Error(`${source}: consumes."${extname}".symbols must be an array of non-empty strings`);
|
|
67
|
+
}
|
|
68
|
+
entry.symbols = value.symbols;
|
|
69
|
+
}
|
|
70
|
+
consumes[extname] = entry;
|
|
71
|
+
}
|
|
72
|
+
manifest.consumes = consumes;
|
|
73
|
+
}
|
|
74
|
+
return manifest;
|
|
75
|
+
}
|
|
76
|
+
function validateProvide(extname, value, source) {
|
|
77
|
+
if (!isPlainObject(value)) {
|
|
78
|
+
throw new Error(`${source}: provides."${extname}" must be an object`);
|
|
79
|
+
}
|
|
80
|
+
const provide = {};
|
|
81
|
+
if (value.schema !== undefined) {
|
|
82
|
+
if (value.schema !== null && (typeof value.schema !== 'string' || !value.schema.trim())) {
|
|
83
|
+
throw new Error(`${source}: provides."${extname}".schema must be a non-empty string or null`);
|
|
84
|
+
}
|
|
85
|
+
provide.schema = value.schema;
|
|
86
|
+
}
|
|
87
|
+
for (const flag of ['relocatable', 'ifNotExists', 'cascade', 'createSchema', 'dropSchema']) {
|
|
88
|
+
if (value[flag] !== undefined) {
|
|
89
|
+
if (typeof value[flag] !== 'boolean') {
|
|
90
|
+
throw new Error(`${source}: provides."${extname}".${flag} must be a boolean`);
|
|
91
|
+
}
|
|
92
|
+
provide[flag] = value[flag];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (value.grants !== undefined) {
|
|
96
|
+
if (!Array.isArray(value.grants)) {
|
|
97
|
+
throw new Error(`${source}: provides."${extname}".grants must be an array`);
|
|
98
|
+
}
|
|
99
|
+
provide.grants = value.grants.map((g, i) => validateGrant(extname, g, i, source));
|
|
100
|
+
}
|
|
101
|
+
return provide;
|
|
102
|
+
}
|
|
103
|
+
function validateGrant(extname, value, index, source) {
|
|
104
|
+
const where = `provides."${extname}".grants[${index}]`;
|
|
105
|
+
if (!isPlainObject(value)) {
|
|
106
|
+
throw new Error(`${source}: ${where} must be an object`);
|
|
107
|
+
}
|
|
108
|
+
if (typeof value.privileges !== 'string' || !value.privileges.trim()) {
|
|
109
|
+
throw new Error(`${source}: ${where}.privileges must be a non-empty string`);
|
|
110
|
+
}
|
|
111
|
+
if (typeof value.on !== 'string' || !GRANT_TARGETS.has(value.on)) {
|
|
112
|
+
throw new Error(`${source}: ${where}.on must be one of ${[...GRANT_TARGETS].join(', ')}`);
|
|
113
|
+
}
|
|
114
|
+
const toValid = (typeof value.to === 'string' && value.to.trim()) ||
|
|
115
|
+
(Array.isArray(value.to) && value.to.length > 0 && value.to.every((r) => typeof r === 'string' && r.trim()));
|
|
116
|
+
if (!toValid) {
|
|
117
|
+
throw new Error(`${source}: ${where}.to must be a role name or non-empty array of role names`);
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
privileges: value.privileges,
|
|
121
|
+
on: value.on,
|
|
122
|
+
to: value.to
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Read + validate the `extensions.json` (or `pgpm.extensions.json`) manifest for
|
|
127
|
+
* a module directory. Returns `undefined` when the module declares no manifest.
|
|
128
|
+
*/
|
|
129
|
+
function readExtensionsManifest(moduleDir) {
|
|
130
|
+
for (const file of exports.EXTENSIONS_MANIFEST_FILES) {
|
|
131
|
+
const p = (0, path_1.join)(moduleDir, file);
|
|
132
|
+
if ((0, fs_1.existsSync)(p)) {
|
|
133
|
+
let parsed;
|
|
134
|
+
try {
|
|
135
|
+
parsed = JSON.parse((0, fs_1.readFileSync)(p, 'utf-8'));
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
throw new Error(`Failed to parse ${file} at ${moduleDir}: ${e.message}`);
|
|
139
|
+
}
|
|
140
|
+
return validateExtensionsManifest(parsed, file);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { RoleMapping } from '@pgpmjs/types';
|
|
2
|
+
import { CompiledExtensionInstall, CompileExtensionInstallOptions } from './compile';
|
|
3
|
+
/**
|
|
4
|
+
* Build a grant role-name map (workspace portability profile) from the
|
|
5
|
+
* configured {@link RoleMapping}. Keys are the conceptual role names a module
|
|
6
|
+
* author writes in its manifest (`anonymous`/`authenticated`/`administrator`);
|
|
7
|
+
* values are the actual role names for this database.
|
|
8
|
+
*/
|
|
9
|
+
export declare function roleMapFromRoles(roles?: RoleMapping): Record<string, string> | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Find a declarative install for an external extension by scanning the resolved
|
|
12
|
+
* local modules for an `extensions.json` that `provides` it, and compiling it.
|
|
13
|
+
*
|
|
14
|
+
* Returns `undefined` when no module declares the extension — the caller then
|
|
15
|
+
* falls back to the legacy `CREATE EXTENSION ... CASCADE` (no schema, no grants).
|
|
16
|
+
* The first declaring module wins; a later contradictory declaration throws.
|
|
17
|
+
*/
|
|
18
|
+
export declare function findExtensionInstall(extname: string, localModules: string[], modules: Record<string, {
|
|
19
|
+
path: string;
|
|
20
|
+
}>, workspacePath: string, options?: CompileExtensionInstallOptions): CompiledExtensionInstall | undefined;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.roleMapFromRoles = roleMapFromRoles;
|
|
4
|
+
exports.findExtensionInstall = findExtensionInstall;
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const compile_1 = require("./compile");
|
|
7
|
+
const manifest_1 = require("./manifest");
|
|
8
|
+
/**
|
|
9
|
+
* Build a grant role-name map (workspace portability profile) from the
|
|
10
|
+
* configured {@link RoleMapping}. Keys are the conceptual role names a module
|
|
11
|
+
* author writes in its manifest (`anonymous`/`authenticated`/`administrator`);
|
|
12
|
+
* values are the actual role names for this database.
|
|
13
|
+
*/
|
|
14
|
+
function roleMapFromRoles(roles) {
|
|
15
|
+
if (!roles)
|
|
16
|
+
return undefined;
|
|
17
|
+
const map = {};
|
|
18
|
+
for (const key of ['anonymous', 'authenticated', 'administrator', 'authenticatedClient']) {
|
|
19
|
+
const value = roles[key];
|
|
20
|
+
if (value)
|
|
21
|
+
map[key] = value;
|
|
22
|
+
}
|
|
23
|
+
return Object.keys(map).length > 0 ? map : undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Find a declarative install for an external extension by scanning the resolved
|
|
27
|
+
* local modules for an `extensions.json` that `provides` it, and compiling it.
|
|
28
|
+
*
|
|
29
|
+
* Returns `undefined` when no module declares the extension — the caller then
|
|
30
|
+
* falls back to the legacy `CREATE EXTENSION ... CASCADE` (no schema, no grants).
|
|
31
|
+
* The first declaring module wins; a later contradictory declaration throws.
|
|
32
|
+
*/
|
|
33
|
+
function findExtensionInstall(extname, localModules, modules, workspacePath, options = {}) {
|
|
34
|
+
let winner;
|
|
35
|
+
for (const moduleName of localModules) {
|
|
36
|
+
const mod = modules[moduleName];
|
|
37
|
+
if (!mod)
|
|
38
|
+
continue;
|
|
39
|
+
const manifest = (0, manifest_1.readExtensionsManifest)((0, path_1.resolve)(workspacePath, mod.path));
|
|
40
|
+
const provide = manifest?.provides?.[extname];
|
|
41
|
+
if (!provide)
|
|
42
|
+
continue;
|
|
43
|
+
if (winner && !sameProvide(winner.provide, provide)) {
|
|
44
|
+
throw new Error(`Conflicting extensions.json "provides" for "${extname}": ` +
|
|
45
|
+
`module "${winner.moduleName}" and module "${moduleName}" declare incompatible ` +
|
|
46
|
+
`install schema/grants. Exactly one provider (or identical declarations) is required.`);
|
|
47
|
+
}
|
|
48
|
+
if (!winner)
|
|
49
|
+
winner = { moduleName, provide };
|
|
50
|
+
}
|
|
51
|
+
return winner ? (0, compile_1.compileExtensionInstall)(extname, winner.provide, options) : undefined;
|
|
52
|
+
}
|
|
53
|
+
/** Structural equality of two provide declarations (order-insensitive grants). */
|
|
54
|
+
function sameProvide(a, b) {
|
|
55
|
+
return normalizeProvide(a) === normalizeProvide(b);
|
|
56
|
+
}
|
|
57
|
+
function normalizeProvide(p) {
|
|
58
|
+
const grants = (p.grants ?? [])
|
|
59
|
+
.map(g => ({
|
|
60
|
+
privileges: g.privileges,
|
|
61
|
+
on: g.on,
|
|
62
|
+
to: (Array.isArray(g.to) ? [...g.to] : [g.to]).sort()
|
|
63
|
+
}))
|
|
64
|
+
.sort((x, y) => JSON.stringify(x).localeCompare(JSON.stringify(y)));
|
|
65
|
+
return JSON.stringify({
|
|
66
|
+
schema: p.schema ?? null,
|
|
67
|
+
relocatable: p.relocatable ?? null,
|
|
68
|
+
ifNotExists: p.ifNotExists ?? null,
|
|
69
|
+
cascade: p.cascade ?? null,
|
|
70
|
+
createSchema: p.createSchema ?? null,
|
|
71
|
+
dropSchema: p.dropSchema ?? null,
|
|
72
|
+
grants
|
|
73
|
+
});
|
|
74
|
+
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -17,7 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
exports.withTransaction = exports.executeQuery = exports.hashString = exports.hashFile = exports.parseAuthor = exports.PgpmInit = exports.PgpmMigrate = exports.cleanSql = void 0;
|
|
18
18
|
__exportStar(require("./core/class/pgpm"), exports);
|
|
19
19
|
__exportStar(require("./rebundle"), exports);
|
|
20
|
-
__exportStar(require("./extensions
|
|
20
|
+
__exportStar(require("./extensions"), exports);
|
|
21
21
|
__exportStar(require("./modules/modules"), exports);
|
|
22
22
|
__exportStar(require("./packaging/package"), exports);
|
|
23
23
|
__exportStar(require("./packaging/sync-versions"), exports);
|
package/migrate/clean.js
CHANGED
|
@@ -5,8 +5,7 @@ const pgsql_parser_1 = require("pgsql-parser");
|
|
|
5
5
|
const filterStatements = (stmts) => {
|
|
6
6
|
const filteredStmts = stmts.filter(node => {
|
|
7
7
|
const stmt = node.stmt;
|
|
8
|
-
return stmt && !stmt.hasOwnProperty('TransactionStmt')
|
|
9
|
-
!stmt.hasOwnProperty('CreateExtensionStmt');
|
|
8
|
+
return stmt && !stmt.hasOwnProperty('TransactionStmt');
|
|
10
9
|
});
|
|
11
10
|
const hasFiltered = filteredStmts.length !== stmts.length;
|
|
12
11
|
return { filteredStmts, hasFiltered };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpmjs/core",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.16.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "PGPM Package and Migration Tools",
|
|
6
6
|
"main": "index.js",
|
|
@@ -48,24 +48,24 @@
|
|
|
48
48
|
"makage": "^0.3.0"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@pgpmjs/ast": "^0.9.
|
|
52
|
-
"@pgpmjs/bundle": "^0.
|
|
53
|
-
"@pgpmjs/env": "^2.
|
|
51
|
+
"@pgpmjs/ast": "^0.9.3",
|
|
52
|
+
"@pgpmjs/bundle": "^0.12.1",
|
|
53
|
+
"@pgpmjs/env": "^2.39.0",
|
|
54
54
|
"@pgpmjs/logger": "^2.22.0",
|
|
55
|
-
"@pgpmjs/server-utils": "^3.23.
|
|
56
|
-
"@pgpmjs/slice": "^0.10.
|
|
57
|
-
"@pgpmjs/transform": "^0.
|
|
58
|
-
"@pgpmjs/types": "^2.
|
|
55
|
+
"@pgpmjs/server-utils": "^3.23.2",
|
|
56
|
+
"@pgpmjs/slice": "^0.10.2",
|
|
57
|
+
"@pgpmjs/transform": "^0.11.1",
|
|
58
|
+
"@pgpmjs/types": "^2.47.0",
|
|
59
59
|
"csv-to-pg": "^4.8.0",
|
|
60
60
|
"genomic": "^5.6.2",
|
|
61
61
|
"glob": "^13.0.6",
|
|
62
62
|
"parse-package-name": "^1.0.0",
|
|
63
63
|
"pg": "^8.21.0",
|
|
64
|
-
"pg-cache": "^3.23.
|
|
64
|
+
"pg-cache": "^3.23.2",
|
|
65
65
|
"pg-env": "^1.26.0",
|
|
66
66
|
"pgsql-deparser": "^18.1.1",
|
|
67
67
|
"pgsql-parser": "^18.1.1",
|
|
68
68
|
"yanse": "^0.2.1"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "0286d994d98717cb9b28842233d2db6d1e37c48e"
|
|
71
71
|
}
|
package/packaging/package.d.ts
CHANGED
|
@@ -9,6 +9,12 @@ interface PackageModuleOptions {
|
|
|
9
9
|
interface WritePackageOptions extends PackageModuleOptions {
|
|
10
10
|
version: string;
|
|
11
11
|
packageDir: string;
|
|
12
|
+
/**
|
|
13
|
+
* Also emit the content-addressed bundle artifact
|
|
14
|
+
* `sql/<name>--<version>.bundle.tar.gz` (default true). It is what
|
|
15
|
+
* `deploy --bundled` consumes.
|
|
16
|
+
*/
|
|
17
|
+
bundle?: boolean;
|
|
12
18
|
}
|
|
13
19
|
export interface MergeSqlOptions {
|
|
14
20
|
/** Strip `TransactionStmt`/`CreateExtensionStmt` before deparsing (extension mode) */
|
|
@@ -38,5 +44,5 @@ export declare const packageModule: (packageDir: string, { usePlan, extension, p
|
|
|
38
44
|
tree1?: string;
|
|
39
45
|
tree2?: string;
|
|
40
46
|
}>;
|
|
41
|
-
export declare const writePackage: ({ version, extension, usePlan, packageDir, outputDiff, }: WritePackageOptions) => Promise<void>;
|
|
47
|
+
export declare const writePackage: ({ version, extension, usePlan, packageDir, outputDiff, bundle, }: WritePackageOptions) => Promise<void>;
|
|
42
48
|
export {};
|
package/packaging/package.js
CHANGED
|
@@ -7,6 +7,7 @@ const path_1 = require("path");
|
|
|
7
7
|
const pgsql_deparser_1 = require("pgsql-deparser");
|
|
8
8
|
const pgsql_parser_1 = require("pgsql-parser");
|
|
9
9
|
const files_1 = require("@pgpmjs/ast/files");
|
|
10
|
+
const artifact_1 = require("../bundle/artifact");
|
|
10
11
|
const resolve_1 = require("../resolution/resolve");
|
|
11
12
|
const transform_1 = require("./transform");
|
|
12
13
|
const log = new logger_1.Logger('package');
|
|
@@ -84,7 +85,7 @@ const packageModule = async (packageDir, { usePlan = true, extension = true, pre
|
|
|
84
85
|
}
|
|
85
86
|
};
|
|
86
87
|
exports.packageModule = packageModule;
|
|
87
|
-
const writePackage = async ({ version, extension = true, usePlan = true, packageDir, outputDiff = false, }) => {
|
|
88
|
+
const writePackage = async ({ version, extension = true, usePlan = true, packageDir, outputDiff = false, bundle = true, }) => {
|
|
88
89
|
const pkgPath = `${packageDir}/package.json`;
|
|
89
90
|
const pkg = require(pkgPath);
|
|
90
91
|
const extname = await (0, files_1.getExtensionName)(packageDir);
|
|
@@ -121,5 +122,11 @@ const writePackage = async ({ version, extension = true, usePlan = true, package
|
|
|
121
122
|
const writePath = `${outPath}/${sqlFileName}`;
|
|
122
123
|
(0, fs_1.writeFileSync)(writePath, sql);
|
|
123
124
|
log.success(`${(0, path_1.relative)(packageDir, writePath)} written`);
|
|
125
|
+
if (bundle) {
|
|
126
|
+
const bundlePath = await (0, artifact_1.writeBundleArtifact)(packageDir, version, {
|
|
127
|
+
createdWith: '@pgpmjs/core'
|
|
128
|
+
});
|
|
129
|
+
log.success(`${(0, path_1.relative)(packageDir, bundlePath)} written`);
|
|
130
|
+
}
|
|
124
131
|
};
|
|
125
132
|
exports.writePackage = writePackage;
|