@prisma-next/target-postgres 0.14.0-dev.9 → 0.14.0-dev.90
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/dist/authoring-BtsOWgI3.mjs +674 -0
- package/dist/authoring-BtsOWgI3.mjs.map +1 -0
- package/dist/canonicalize-D1F4vUnP.d.mts +48 -0
- package/dist/canonicalize-D1F4vUnP.d.mts.map +1 -0
- package/dist/{codec-ids-BvytN2P8.mjs → codec-ids-B10ZZHzw.mjs} +4 -2
- package/dist/codec-ids-B10ZZHzw.mjs.map +1 -0
- package/dist/{codec-ids-CnXu9Qy3.d.mts → codec-ids-CI144z7g.d.mts} +4 -2
- package/dist/codec-ids-CI144z7g.d.mts.map +1 -0
- package/dist/codec-ids.d.mts +2 -2
- package/dist/codec-ids.mjs +2 -2
- package/dist/{codec-types-DHCkwPKE.d.mts → codec-types-CTfT4U4O.d.mts} +4 -2
- package/dist/codec-types-CTfT4U4O.d.mts.map +1 -0
- package/dist/codec-types.d.mts +1 -1
- package/dist/{codecs--0A5_4Bq.d.mts → codecs-D7W9O0Xx.d.mts} +134 -4
- package/dist/codecs-D7W9O0Xx.d.mts.map +1 -0
- package/dist/codecs-DRbb26Or.mjs +955 -0
- package/dist/codecs-DRbb26Or.mjs.map +1 -0
- package/dist/codecs.d.mts +2 -2
- package/dist/codecs.mjs +3 -757
- package/dist/codecs.mjs.map +1 -1
- package/dist/contract-free.d.mts +33 -3
- package/dist/contract-free.d.mts.map +1 -1
- package/dist/contract-free.mjs +23 -7
- package/dist/contract-free.mjs.map +1 -1
- package/dist/control-policy-DtbkLLGv.mjs +1285 -0
- package/dist/control-policy-DtbkLLGv.mjs.map +1 -0
- package/dist/control.d.mts +2 -2
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +880 -36
- package/dist/control.mjs.map +1 -1
- package/dist/data-transform-BOWpliq8.mjs.map +1 -1
- package/dist/{data-transform-DDgWdB5o.d.mts → data-transform-DPzwBMhW.d.mts} +3 -3
- package/dist/{data-transform-DDgWdB5o.d.mts.map → data-transform-DPzwBMhW.d.mts.map} +1 -1
- package/dist/data-transform.d.mts +1 -1
- package/dist/{ddl-QDyOSeLc.mjs → ddl-GTTOvjx_.mjs} +128 -6
- package/dist/ddl-GTTOvjx_.mjs.map +1 -0
- package/dist/ddl.d.mts +3 -2
- package/dist/ddl.mjs +2 -2
- package/dist/default-normalizer.d.mts +1 -1
- package/dist/default-normalizer.d.mts.map +1 -1
- package/dist/default-normalizer.mjs +247 -1
- package/dist/default-normalizer.mjs.map +1 -0
- package/dist/descriptor-meta-CFQKLXl_.mjs +23 -0
- package/dist/descriptor-meta-CFQKLXl_.mjs.map +1 -0
- package/dist/descriptor-meta-runtime-UwYdV8fD.mjs +12 -0
- package/dist/descriptor-meta-runtime-UwYdV8fD.mjs.map +1 -0
- package/dist/diff-database-schema-GncqMuSI.mjs +305 -0
- package/dist/diff-database-schema-GncqMuSI.mjs.map +1 -0
- package/dist/diff-database-schema.d.mts +46 -0
- package/dist/diff-database-schema.d.mts.map +1 -0
- package/dist/diff-database-schema.mjs +2 -0
- package/dist/errors-DO-w0EUS.mjs +35 -0
- package/dist/errors-DO-w0EUS.mjs.map +1 -0
- package/dist/errors.d.mts +11 -9
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +1 -1
- package/dist/issue-planner.d.mts +35 -11
- package/dist/issue-planner.d.mts.map +1 -1
- package/dist/issue-planner.mjs +2 -2
- package/dist/migration.d.mts +5 -5
- package/dist/migration.d.mts.map +1 -1
- package/dist/migration.mjs +4 -4
- package/dist/namespace-ids-xtp_ZY86.mjs +7 -0
- package/dist/namespace-ids-xtp_ZY86.mjs.map +1 -0
- package/dist/native-type-normalizer.mjs +37 -1
- package/dist/native-type-normalizer.mjs.map +1 -0
- package/dist/nodes-DJC5B8Id.mjs +192 -0
- package/dist/nodes-DJC5B8Id.mjs.map +1 -0
- package/dist/nodes-Dv5eCSq9.d.mts +170 -0
- package/dist/nodes-Dv5eCSq9.d.mts.map +1 -0
- package/dist/{op-factory-call-DmQEc3XV.d.mts → op-factory-call-Bn9zDx4_.d.mts} +114 -15
- package/dist/op-factory-call-Bn9zDx4_.d.mts.map +1 -0
- package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-C38NzvQD.mjs} +469 -32
- package/dist/op-factory-call-C38NzvQD.mjs.map +1 -0
- package/dist/op-factory-call.d.mts +2 -2
- package/dist/op-factory-call.mjs +2 -2
- package/dist/pack.d.mts +321 -3
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +1 -1
- package/dist/{planner-ddl-builders-B2wOwLqI.mjs → planner-ddl-builders-BKlmGDJl.mjs} +17 -28
- package/dist/planner-ddl-builders-BKlmGDJl.mjs.map +1 -0
- package/dist/planner-ddl-builders.d.mts +4 -6
- package/dist/planner-ddl-builders.d.mts.map +1 -1
- package/dist/planner-ddl-builders.mjs +2 -2
- package/dist/planner-identity-values-CJPha2Sz.mjs.map +1 -1
- package/dist/planner-identity-values.d.mts +8 -2
- package/dist/planner-identity-values.d.mts.map +1 -1
- package/dist/planner-identity-values.mjs +2 -2
- package/dist/planner-kql24i_F.mjs +390 -0
- package/dist/planner-kql24i_F.mjs.map +1 -0
- package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-BEY1T0Xm.d.mts} +4 -4
- package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-BEY1T0Xm.d.mts.map} +1 -1
- package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-D7y-PEHK.mjs} +3 -3
- package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-D7y-PEHK.mjs.map} +1 -1
- package/dist/planner-produced-postgres-migration.d.mts +1 -1
- package/dist/planner-produced-postgres-migration.mjs +1 -1
- package/dist/{planner-sql-checks-jqUUGyQR.mjs → planner-sql-checks-zqNfs2XZ.mjs} +5 -5
- package/dist/{planner-sql-checks-jqUUGyQR.mjs.map → planner-sql-checks-zqNfs2XZ.mjs.map} +1 -1
- package/dist/planner-sql-checks.d.mts +2 -2
- package/dist/planner-sql-checks.d.mts.map +1 -1
- package/dist/planner-sql-checks.mjs +1 -1
- package/dist/{planner-target-details-CIY6tLeo.d.mts → planner-target-details-HkP4RrRO.d.mts} +2 -2
- package/dist/planner-target-details-HkP4RrRO.d.mts.map +1 -0
- package/dist/planner-target-details.d.mts +1 -1
- package/dist/planner.d.mts +72 -17
- package/dist/planner.d.mts.map +1 -1
- package/dist/planner.mjs +3 -2
- package/dist/postgres-contract-view-C9j5cqP0.d.mts +34 -0
- package/dist/postgres-contract-view-C9j5cqP0.d.mts.map +1 -0
- package/dist/{postgres-contract-serializer-k3TAcPMY.mjs → postgres-contract-view-DM8sN1Ot.mjs} +45 -41
- package/dist/postgres-contract-view-DM8sN1Ot.mjs.map +1 -0
- package/dist/postgres-database-schema-node-CJv5bfbs.d.mts +262 -0
- package/dist/postgres-database-schema-node-CJv5bfbs.d.mts.map +1 -0
- package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-DbckK1su.d.mts} +58 -9
- package/dist/postgres-migration-DbckK1su.d.mts.map +1 -0
- package/dist/{postgres-migration-B5jKrXv3.mjs → postgres-migration-arMSWU9b.mjs} +78 -45
- package/dist/postgres-migration-arMSWU9b.mjs.map +1 -0
- package/dist/postgres-native-enum-BwVNf0Ku.mjs +45 -0
- package/dist/postgres-native-enum-BwVNf0Ku.mjs.map +1 -0
- package/dist/postgres-rls-policy--CSddvMR.d.mts +52 -0
- package/dist/postgres-rls-policy--CSddvMR.d.mts.map +1 -0
- package/dist/postgres-role-schema-node-Cm-j0ZGM.mjs +190 -0
- package/dist/postgres-role-schema-node-Cm-j0ZGM.mjs.map +1 -0
- package/dist/{postgres-schema-COGZ1ark.mjs → postgres-schema-Cm4DtiGA.mjs} +200 -11
- package/dist/postgres-schema-Cm4DtiGA.mjs.map +1 -0
- package/dist/postgres-schema-DKq6s1P3.d.mts +269 -0
- package/dist/postgres-schema-DKq6s1P3.d.mts.map +1 -0
- package/dist/postgres-table-schema-node-D6LvInCe.mjs +216 -0
- package/dist/postgres-table-schema-node-D6LvInCe.mjs.map +1 -0
- package/dist/render-ops.d.mts +1 -1
- package/dist/{render-typescript-KMgosran.mjs → render-typescript-7yqILcwr.mjs} +45 -16
- package/dist/render-typescript-7yqILcwr.mjs.map +1 -0
- package/dist/render-typescript.mjs +1 -1
- package/dist/resolve-ddl-schema-XyBaaenM.mjs +22 -0
- package/dist/resolve-ddl-schema-XyBaaenM.mjs.map +1 -0
- package/dist/rls-canonicalize.d.mts +27 -0
- package/dist/rls-canonicalize.d.mts.map +1 -0
- package/dist/rls-canonicalize.mjs +2 -0
- package/dist/runtime.d.mts +6 -8
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +3 -3
- package/dist/runtime.mjs.map +1 -1
- package/dist/schema-ir-annotations.d.mts +15 -0
- package/dist/schema-ir-annotations.d.mts.map +1 -0
- package/dist/schema-ir-annotations.mjs +2 -0
- package/dist/{sql-utils-DcfMz4MQ.mjs → sql-utils-SU4FDvIV.mjs} +12 -3
- package/dist/{sql-utils-DcfMz4MQ.mjs.map → sql-utils-SU4FDvIV.mjs.map} +1 -1
- package/dist/sql-utils.d.mts +8 -1
- package/dist/sql-utils.d.mts.map +1 -1
- package/dist/sql-utils.mjs +2 -2
- package/dist/types.d.mts +4 -140
- package/dist/types.mjs +5 -2
- package/dist/wire-name-DbQ9-2RC.mjs +104 -0
- package/dist/wire-name-DbQ9-2RC.mjs.map +1 -0
- package/package.json +26 -20
- package/src/contract-free/checks.ts +153 -0
- package/src/contract-free/control-bootstrap.ts +20 -3
- package/src/contract-free/ddl.ts +98 -1
- package/src/core/authoring.ts +726 -2
- package/src/core/codec-helpers.ts +33 -7
- package/src/core/codec-ids.ts +2 -0
- package/src/core/codec-type-map.ts +4 -0
- package/src/core/codecs.ts +236 -12
- package/src/core/ddl/nodes.ts +185 -2
- package/src/core/default-normalizer.ts +122 -2
- package/src/core/descriptor-meta.ts +12 -1
- package/src/core/entity-kinds.ts +41 -0
- package/src/core/errors.ts +14 -12
- package/src/core/migrations/bound-schema.ts +10 -0
- package/src/core/migrations/column-ddl-rendering.ts +117 -0
- package/src/core/migrations/contract-to-postgres-database-schema-node.ts +189 -0
- package/src/core/migrations/control-policy.ts +217 -65
- package/src/core/migrations/diff-database-schema.ts +252 -0
- package/src/core/migrations/issue-planner.ts +817 -656
- package/src/core/migrations/op-factory-call.ts +383 -23
- package/src/core/migrations/operations/columns.ts +24 -12
- package/src/core/migrations/operations/constraints.ts +1 -1
- package/src/core/migrations/operations/data-transform.ts +6 -0
- package/src/core/migrations/operations/dependencies.ts +2 -1
- package/src/core/migrations/operations/native-enum-types.ts +62 -0
- package/src/core/migrations/operations/rls.ts +187 -0
- package/src/core/migrations/operations/shared.ts +0 -30
- package/src/core/migrations/planner-ddl-builders.ts +31 -59
- package/src/core/migrations/planner-identity-values.ts +1 -1
- package/src/core/migrations/planner-recipes.ts +31 -13
- package/src/core/migrations/planner-sql-checks.ts +1 -1
- package/src/core/migrations/planner-strategies.ts +248 -216
- package/src/core/migrations/planner-target-details.ts +3 -1
- package/src/core/migrations/planner.ts +357 -67
- package/src/core/migrations/postgres-migration.ts +118 -86
- package/src/core/migrations/render-typescript.ts +42 -15
- package/src/core/migrations/resolve-ddl-schema.ts +26 -0
- package/src/core/migrations/runner.ts +11 -16
- package/src/core/migrations/verify-postgres-namespaces.ts +46 -31
- package/src/core/namespace-ids.ts +3 -0
- package/src/core/postgres-contract-serializer.ts +46 -71
- package/src/core/postgres-contract-view.ts +48 -0
- package/src/core/postgres-native-enum.ts +61 -0
- package/src/core/postgres-rls-enablement.ts +34 -0
- package/src/core/postgres-rls-policy.ts +64 -0
- package/src/core/postgres-role.ts +59 -0
- package/src/core/postgres-schema-verifier.ts +10 -7
- package/src/core/postgres-schema.ts +85 -15
- package/src/core/postgres-validators.ts +34 -0
- package/src/core/psl-infer/infer-psl-contract.ts +1290 -0
- package/src/core/psl-infer/postgres-default-mapping.ts +16 -0
- package/src/core/psl-infer/postgres-type-map.ts +129 -0
- package/src/core/rls/canonicalize.ts +65 -0
- package/src/core/rls/wire-name.ts +33 -0
- package/src/core/schema-ir/node-storage-coordinate.ts +26 -0
- package/src/core/schema-ir/postgres-database-schema-node.ts +61 -0
- package/src/core/schema-ir/postgres-namespace-schema-node.ts +70 -0
- package/src/core/schema-ir/postgres-native-enum-schema-node.ts +83 -0
- package/src/core/schema-ir/postgres-policy-schema-node.ts +90 -0
- package/src/core/schema-ir/postgres-role-schema-node.ts +65 -0
- package/src/core/schema-ir/postgres-table-schema-node.ts +140 -0
- package/src/core/schema-ir/schema-node-kinds.ts +122 -0
- package/src/core/sql-utils.ts +15 -2
- package/src/exports/codecs.ts +5 -0
- package/src/exports/contract-free.ts +11 -1
- package/src/exports/control.ts +27 -33
- package/src/exports/ddl.ts +8 -0
- package/src/exports/diff-database-schema.ts +7 -0
- package/src/exports/issue-planner.ts +1 -1
- package/src/exports/migration.ts +1 -0
- package/src/exports/op-factory-call.ts +4 -0
- package/src/exports/planner-ddl-builders.ts +0 -2
- package/src/exports/planner-identity-values.ts +4 -1
- package/src/exports/planner.ts +1 -0
- package/src/exports/rls-canonicalize.ts +12 -0
- package/src/exports/runtime.ts +1 -0
- package/src/exports/schema-ir-annotations.ts +1 -0
- package/src/exports/sql-utils.ts +1 -0
- package/src/exports/types.ts +37 -0
- package/dist/codec-ids-BvytN2P8.mjs.map +0 -1
- package/dist/codec-ids-CnXu9Qy3.d.mts.map +0 -1
- package/dist/codec-types-DHCkwPKE.d.mts.map +0 -1
- package/dist/codecs--0A5_4Bq.d.mts.map +0 -1
- package/dist/ddl-QDyOSeLc.mjs.map +0 -1
- package/dist/default-normalizer-DyyCHQWs.mjs +0 -131
- package/dist/default-normalizer-DyyCHQWs.mjs.map +0 -1
- package/dist/descriptor-meta-CpGygXpI.mjs +0 -140
- package/dist/descriptor-meta-CpGygXpI.mjs.map +0 -1
- package/dist/errors-CUk87ByX.mjs +0 -33
- package/dist/errors-CUk87ByX.mjs.map +0 -1
- package/dist/issue-planner-DL6g3CmE.mjs +0 -824
- package/dist/issue-planner-DL6g3CmE.mjs.map +0 -1
- package/dist/native-type-normalizer-Bc9XJzWC.mjs +0 -38
- package/dist/native-type-normalizer-Bc9XJzWC.mjs.map +0 -1
- package/dist/nodes-Bbhs2rwj.mjs +0 -76
- package/dist/nodes-Bbhs2rwj.mjs.map +0 -1
- package/dist/nodes-pLeLgdis.d.mts +0 -67
- package/dist/nodes-pLeLgdis.d.mts.map +0 -1
- package/dist/op-factory-call-D_p5vxwt.mjs.map +0 -1
- package/dist/op-factory-call-DmQEc3XV.d.mts.map +0 -1
- package/dist/planner-Bs_baQax.mjs +0 -324
- package/dist/planner-Bs_baQax.mjs.map +0 -1
- package/dist/planner-ddl-builders-B2wOwLqI.mjs.map +0 -1
- package/dist/planner-target-details-CIY6tLeo.d.mts.map +0 -1
- package/dist/postgres-contract-serializer-k3TAcPMY.mjs.map +0 -1
- package/dist/postgres-migration-B5jKrXv3.mjs.map +0 -1
- package/dist/postgres-migration-Y4YBJqkS.d.mts.map +0 -1
- package/dist/postgres-schema-COGZ1ark.mjs.map +0 -1
- package/dist/render-typescript-KMgosran.mjs.map +0 -1
- package/dist/types.d.mts.map +0 -1
- package/src/core/postgres-enum-type-schema.ts +0 -17
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import type { Contract, ControlPolicy } from '@prisma-next/contract/types';
|
|
2
|
+
import type { SqlSchemaDiffResult } from '@prisma-next/family-sql/control';
|
|
3
|
+
import { buildNativeTypeExpander } from '@prisma-next/family-sql/control';
|
|
4
|
+
import { classifyDiffSubjectGranularity } from '@prisma-next/family-sql/diff';
|
|
5
|
+
import type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';
|
|
6
|
+
import type { DiffableNode, SchemaDiffIssue } from '@prisma-next/framework-components/control';
|
|
7
|
+
import { diffSchemas } from '@prisma-next/framework-components/control';
|
|
8
|
+
import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
|
|
9
|
+
import type { SqlStorage } from '@prisma-next/sql-contract/types';
|
|
10
|
+
import type { SqlSchemaIRNode } from '@prisma-next/sql-schema-ir/types';
|
|
11
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
12
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
13
|
+
import type { PostgresContract } from '../postgres-schema';
|
|
14
|
+
import { PostgresDatabaseSchemaNode } from '../schema-ir/postgres-database-schema-node';
|
|
15
|
+
import { PostgresNamespaceSchemaNode } from '../schema-ir/postgres-namespace-schema-node';
|
|
16
|
+
import {
|
|
17
|
+
postgresDiffSubjectGranularity,
|
|
18
|
+
type SqlSchemaDiffNode,
|
|
19
|
+
} from '../schema-ir/schema-node-kinds';
|
|
20
|
+
import { contractToPostgresDatabaseSchemaNode } from './contract-to-postgres-database-schema-node';
|
|
21
|
+
import { resolvePostgresNodeIssueControlPolicySubject } from './control-policy';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether a diff issue's subject node is cluster-scoped — it carries its own
|
|
25
|
+
* `namespaceId` field (only role and policy diff nodes do) and that
|
|
26
|
+
* coordinate is the unbound sentinel, meaning the node is not owned by any
|
|
27
|
+
* schema/namespace. A role node is always cluster-scoped (roles are
|
|
28
|
+
* cluster-level objects); a policy node's `namespaceId` is always its
|
|
29
|
+
* table's resolved DDL schema, never the sentinel. Namespace-ownership
|
|
30
|
+
* scoping (which compares `issue.path[1]` against the set of DDL schemas
|
|
31
|
+
* the contract owns) makes no sense for a cluster-scoped subject — its path
|
|
32
|
+
* segment at that index is the object's own name, not a schema name — so
|
|
33
|
+
* such an issue bypasses that scoping entirely.
|
|
34
|
+
*/
|
|
35
|
+
function isClusterScopedIssue(issue: SchemaDiffIssue): boolean {
|
|
36
|
+
const node = issue.expected ?? issue.actual;
|
|
37
|
+
return node !== undefined && nodeNamespaceId(node) === UNBOUND_NAMESPACE_ID;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function nodeNamespaceId(node: DiffableNode): string | undefined {
|
|
41
|
+
if (!Object.hasOwn(node, 'namespaceId')) return undefined;
|
|
42
|
+
return blindCast<
|
|
43
|
+
{ namespaceId: string },
|
|
44
|
+
'presence of an own namespaceId field was just checked via Object.hasOwn'
|
|
45
|
+
>(node).namespaceId;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function ownedSchemaNames(expected: PostgresDatabaseSchemaNode): ReadonlySet<string> {
|
|
49
|
+
const policyNamespaces = Object.values(expected.namespaces).flatMap((ns) =>
|
|
50
|
+
Object.values(ns.tables).flatMap((t) => t.policies.map((p) => p.namespaceId)),
|
|
51
|
+
);
|
|
52
|
+
return new Set([...policyNamespaces, ...expected.existingSchemas]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Drops contract namespaces that declare no tables from the verdict-diff
|
|
57
|
+
* expected tree and the relational owned-schema set. The legacy relational
|
|
58
|
+
* walk skipped a table-less namespace (e.g. an enums-only schema) before
|
|
59
|
+
* pairing, so neither its DDL schema's absence nor that schema's live
|
|
60
|
+
* relational contents ever reached the verdict — the pruned tree
|
|
61
|
+
* reproduces that. The prune loses no expected policies: policies attach
|
|
62
|
+
* to tables, so a table-less namespace carries none (the projection
|
|
63
|
+
* throws on a policy referencing an absent table). Live policies in a
|
|
64
|
+
* pruned schema remain governed via the full owned set (see
|
|
65
|
+
* {@link diffPostgresSchema}).
|
|
66
|
+
*/
|
|
67
|
+
function pruneTableLessNamespaces(
|
|
68
|
+
expected: PostgresDatabaseSchemaNode,
|
|
69
|
+
): PostgresDatabaseSchemaNode {
|
|
70
|
+
const namespaces = Object.fromEntries(
|
|
71
|
+
Object.entries(expected.namespaces).filter(([, ns]) => Object.keys(ns.tables).length > 0),
|
|
72
|
+
);
|
|
73
|
+
return new PostgresDatabaseSchemaNode({
|
|
74
|
+
namespaces,
|
|
75
|
+
roles: [...expected.roles],
|
|
76
|
+
existingSchemas: expected.existingSchemas.filter((s) => namespaces[s] !== undefined),
|
|
77
|
+
pgVersion: expected.pgVersion,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Resolves a verdict-diff issue's subject's declared control policy directly
|
|
83
|
+
* from the contract, by delegating to the same node-typed resolver
|
|
84
|
+
* ({@link resolvePostgresNodeIssueControlPolicySubject}) the planner uses to
|
|
85
|
+
* gate DDL calls. `undefined` when the issue resolves to no contract subject.
|
|
86
|
+
*
|
|
87
|
+
* A role issue resolves through that same resolver to `external`
|
|
88
|
+
* unconditionally (see its role branch), regardless of the contract's own
|
|
89
|
+
* default policy: a role is referenced by the contract but not owned, and
|
|
90
|
+
* `external`'s existing semantics — a missing declared subject still fails,
|
|
91
|
+
* every extra is suppressed — are exactly the wanted asymmetric grading for
|
|
92
|
+
* a cluster object the framework does not own.
|
|
93
|
+
*/
|
|
94
|
+
function resolveControlPolicy(
|
|
95
|
+
issue: SchemaDiffIssue,
|
|
96
|
+
contract: Contract<SqlStorage>,
|
|
97
|
+
): ControlPolicy | undefined {
|
|
98
|
+
const nodeIssue = blindCast<
|
|
99
|
+
SchemaDiffIssue<SqlSchemaDiffNode>,
|
|
100
|
+
'every node in a Postgres schema diff tree is a SqlSchemaDiffNode'
|
|
101
|
+
>(issue);
|
|
102
|
+
return resolvePostgresNodeIssueControlPolicySubject(nodeIssue, contract)
|
|
103
|
+
?.explicitNodeControlPolicy;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The Postgres full-tree node diff for the family verify verdict: derive
|
|
108
|
+
* the expected tree (resolved leaf values, expander threaded, FK schemas
|
|
109
|
+
* resolved, table-less namespaces pruned), run the generic
|
|
110
|
+
* differ over the trees as derived, and scope out `not-expected` findings under namespaces the
|
|
111
|
+
* contract does not own. Ownership scoping bypasses cluster-scoped subjects
|
|
112
|
+
* (roles today), mirroring the legacy decomposition: relational extras check
|
|
113
|
+
* the PRUNED owned set (the legacy
|
|
114
|
+
* per-namespace walk never visited a table-less namespace, so its live
|
|
115
|
+
* relational contents are invisible), while `structural` extras (RLS
|
|
116
|
+
* policies) check the FULL owned set (the legacy policy diff governed
|
|
117
|
+
* every contract schema regardless of tables — RLS governance does not
|
|
118
|
+
* shrink because a namespace declares no tables). The codec `verifyType`
|
|
119
|
+
* hooks run once per contract namespace with tables against that
|
|
120
|
+
* namespace's paired actual node (the hooks read namespace-scoped state
|
|
121
|
+
* such as `nativeEnums`).
|
|
122
|
+
*/
|
|
123
|
+
export function diffPostgresSchema(input: {
|
|
124
|
+
readonly contract: Contract<SqlStorage>;
|
|
125
|
+
readonly schema: SqlSchemaIRNode;
|
|
126
|
+
readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'sql', string>>;
|
|
127
|
+
}): SqlSchemaDiffResult {
|
|
128
|
+
const postgresContract = blindCast<
|
|
129
|
+
PostgresContract,
|
|
130
|
+
'diffPostgresSchema is only called with a postgres contract'
|
|
131
|
+
>(input.contract);
|
|
132
|
+
PostgresDatabaseSchemaNode.assert(input.schema);
|
|
133
|
+
const actual = input.schema;
|
|
134
|
+
const expandNativeType = buildNativeTypeExpander(input.frameworkComponents);
|
|
135
|
+
const fullExpected = contractToPostgresDatabaseSchemaNode(postgresContract, {
|
|
136
|
+
annotationNamespace: 'pg',
|
|
137
|
+
...ifDefined('expandNativeType', expandNativeType),
|
|
138
|
+
});
|
|
139
|
+
const expected = pruneTableLessNamespaces(fullExpected);
|
|
140
|
+
const relationalOwned = ownedSchemaNames(expected);
|
|
141
|
+
const structuralOwned = ownedSchemaNames(fullExpected);
|
|
142
|
+
const issues = diffSchemas(expected, actual).filter((issue) => {
|
|
143
|
+
if (issue.reason !== 'not-expected') return true;
|
|
144
|
+
if (isClusterScopedIssue(issue)) return true;
|
|
145
|
+
const granularity = classifyDiffSubjectGranularity(issue, postgresDiffSubjectGranularity);
|
|
146
|
+
const namespaceSegment = issue.path[1];
|
|
147
|
+
if (namespaceSegment === undefined) return true;
|
|
148
|
+
const owned = granularity === 'structural' ? structuralOwned : relationalOwned;
|
|
149
|
+
return owned.has(namespaceSegment);
|
|
150
|
+
});
|
|
151
|
+
const namespacePairs = Object.values(expected.namespaces).map((ns) => ({
|
|
152
|
+
actual: actual.namespaces[ns.schemaName],
|
|
153
|
+
}));
|
|
154
|
+
return {
|
|
155
|
+
issues,
|
|
156
|
+
resolveControlPolicy: (issue) => resolveControlPolicy(issue, postgresContract),
|
|
157
|
+
namespacePairs,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Adds an empty namespace node to the actual tree for every expected namespace
|
|
163
|
+
* absent from it. The relational plan diff pairs on namespace: a contract
|
|
164
|
+
* namespace whose live schema does not exist yet must surface each of its
|
|
165
|
+
* tables as `not-found` (→ `CREATE TABLE`), NOT as a single namespace
|
|
166
|
+
* `not-found` that subtree-coalescing would collapse (leaving `CREATE SCHEMA`
|
|
167
|
+
* with no tables). Padding makes the namespaces pair, so only table/column/
|
|
168
|
+
* policy drift surfaces; `CREATE SCHEMA` comes separately from the synthesized
|
|
169
|
+
* namespace-presence stitch (`verifyPostgresNamespacePresence`), never from the
|
|
170
|
+
* tree diff — matching the retired per-namespace-paired relational walk, which
|
|
171
|
+
* paired a missing schema against an empty namespace node.
|
|
172
|
+
*/
|
|
173
|
+
function padActualNamespaces(
|
|
174
|
+
expected: PostgresDatabaseSchemaNode,
|
|
175
|
+
actual: PostgresDatabaseSchemaNode,
|
|
176
|
+
): PostgresDatabaseSchemaNode {
|
|
177
|
+
const namespaces: Record<string, PostgresNamespaceSchemaNode> = { ...actual.namespaces };
|
|
178
|
+
let padded = false;
|
|
179
|
+
for (const schemaName of Object.keys(expected.namespaces)) {
|
|
180
|
+
if (namespaces[schemaName] === undefined) {
|
|
181
|
+
namespaces[schemaName] = new PostgresNamespaceSchemaNode({
|
|
182
|
+
schemaName,
|
|
183
|
+
tables: {},
|
|
184
|
+
});
|
|
185
|
+
padded = true;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (!padded) return actual;
|
|
189
|
+
return new PostgresDatabaseSchemaNode({
|
|
190
|
+
namespaces,
|
|
191
|
+
roles: [...actual.roles],
|
|
192
|
+
existingSchemas: [...actual.existingSchemas],
|
|
193
|
+
pgVersion: actual.pgVersion,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export interface PostgresPlanDiff {
|
|
198
|
+
/** The desired ("end") tree — resolved leaf values (incl. `codecRef`) on every column, table-less namespaces pruned. */
|
|
199
|
+
readonly expected: PostgresDatabaseSchemaNode;
|
|
200
|
+
/** The live ("start") tree, padded with empty namespaces so a missing schema's tables pair. */
|
|
201
|
+
readonly actual: PostgresDatabaseSchemaNode;
|
|
202
|
+
/** The one node diff over the two trees: relational + policy drift, cluster-scope-aware ownership filtered. */
|
|
203
|
+
readonly issues: readonly SchemaDiffIssue<SqlSchemaDiffNode>[];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The Postgres planner's diff input: the SAME tree-building
|
|
208
|
+
* `diffPostgresSchema` uses (expander threaded, FK schemas resolved,
|
|
209
|
+
* table-less namespaces pruned, cluster-scope-aware ownership filter) plus
|
|
210
|
+
* actual namespace padding (so a missing
|
|
211
|
+
* schema's tables surface as `not-found` instead of a swallowed namespace
|
|
212
|
+
* `not-found`). One differ drives both verify and plan; this is the
|
|
213
|
+
* plan-side derivation. The single issue list covers tables / columns /
|
|
214
|
+
* constraints / indexes / defaults AND policies — the caller splits it
|
|
215
|
+
* (relational → `mapNodeIssueToCall`; policy → RLS ops) and stitches in
|
|
216
|
+
* `CREATE SCHEMA` separately.
|
|
217
|
+
*/
|
|
218
|
+
export function buildPostgresPlanDiff(input: {
|
|
219
|
+
readonly contract: Contract<SqlStorage>;
|
|
220
|
+
readonly actualSchema: SqlSchemaIRNode;
|
|
221
|
+
readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'sql', string>>;
|
|
222
|
+
}): PostgresPlanDiff {
|
|
223
|
+
const postgresContract = blindCast<
|
|
224
|
+
PostgresContract,
|
|
225
|
+
'buildPostgresPlanDiff is only called with a postgres contract'
|
|
226
|
+
>(input.contract);
|
|
227
|
+
PostgresDatabaseSchemaNode.assert(input.actualSchema);
|
|
228
|
+
const actual = input.actualSchema;
|
|
229
|
+
const expandNativeType = buildNativeTypeExpander(input.frameworkComponents);
|
|
230
|
+
const projectionOptions = {
|
|
231
|
+
annotationNamespace: 'pg',
|
|
232
|
+
...ifDefined('expandNativeType', expandNativeType),
|
|
233
|
+
};
|
|
234
|
+
const fullExpected = contractToPostgresDatabaseSchemaNode(postgresContract, projectionOptions);
|
|
235
|
+
const expected = pruneTableLessNamespaces(fullExpected);
|
|
236
|
+
const paddedActual = padActualNamespaces(expected, actual);
|
|
237
|
+
const relationalOwned = ownedSchemaNames(expected);
|
|
238
|
+
const structuralOwned = ownedSchemaNames(fullExpected);
|
|
239
|
+
const issues = blindCast<
|
|
240
|
+
readonly SchemaDiffIssue<SqlSchemaDiffNode>[],
|
|
241
|
+
'both trees are PostgresDatabaseSchemaNodes, so every diff-issue node is a SqlSchemaDiffNode'
|
|
242
|
+
>(diffSchemas(expected, paddedActual)).filter((issue) => {
|
|
243
|
+
if (issue.reason !== 'not-expected') return true;
|
|
244
|
+
if (isClusterScopedIssue(issue)) return true;
|
|
245
|
+
const granularity = classifyDiffSubjectGranularity(issue, postgresDiffSubjectGranularity);
|
|
246
|
+
const namespaceSegment = issue.path[1];
|
|
247
|
+
if (namespaceSegment === undefined) return true;
|
|
248
|
+
const owned = granularity === 'structural' ? structuralOwned : relationalOwned;
|
|
249
|
+
return owned.has(namespaceSegment);
|
|
250
|
+
});
|
|
251
|
+
return { expected, actual: paddedActual, issues };
|
|
252
|
+
}
|