@prisma-next/target-postgres 0.14.0-dev.5 → 0.14.0-dev.50
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-DzHOdWTl.mjs +364 -0
- package/dist/authoring-DzHOdWTl.mjs.map +1 -0
- package/dist/canonicalize-Bn3BM1Cn.mjs +46 -0
- package/dist/canonicalize-Bn3BM1Cn.mjs.map +1 -0
- package/dist/canonicalize-CAbXtVtB.d.mts +37 -0
- package/dist/canonicalize-CAbXtVtB.d.mts.map +1 -0
- package/dist/{codec-ids-CnXu9Qy3.d.mts → codec-ids-BzrFF-I4.d.mts} +3 -2
- package/dist/codec-ids-BzrFF-I4.d.mts.map +1 -0
- package/dist/{codec-ids-BvytN2P8.mjs → codec-ids-C_-Hj6bL.mjs} +3 -2
- package/dist/codec-ids-C_-Hj6bL.mjs.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-BDVmvKAW.d.mts} +3 -2
- package/dist/codec-types-BDVmvKAW.d.mts.map +1 -0
- package/dist/codec-types.d.mts +1 -1
- package/dist/{codecs--0A5_4Bq.d.mts → codecs-Dk2sxGLv.d.mts} +77 -4
- package/dist/codecs-Dk2sxGLv.d.mts.map +1 -0
- package/dist/codecs.d.mts +1 -1
- package/dist/codecs.mjs +117 -2
- package/dist/codecs.mjs.map +1 -1
- package/dist/contract-free.d.mts +8 -3
- package/dist/contract-free.d.mts.map +1 -1
- package/dist/contract-free.mjs +4 -4
- package/dist/control-policy-DUFgy9jO.mjs +1103 -0
- package/dist/control-policy-DUFgy9jO.mjs.map +1 -0
- package/dist/control.d.mts +2 -2
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +757 -35
- 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-ujxH2A7r.mjs} +63 -6
- package/dist/ddl-ujxH2A7r.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-CuGDsrG4.mjs +19 -0
- package/dist/descriptor-meta-CuGDsrG4.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-BmSmWcaY.mjs +313 -0
- package/dist/diff-database-schema-BmSmWcaY.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/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-CSkPX3rI.d.mts +120 -0
- package/dist/nodes-CSkPX3rI.d.mts.map +1 -0
- package/dist/{nodes-Bbhs2rwj.mjs → nodes-Dh__NS58.mjs} +56 -2
- package/dist/nodes-Dh__NS58.mjs.map +1 -0
- package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-BKRnMgrB.mjs} +205 -31
- package/dist/op-factory-call-BKRnMgrB.mjs.map +1 -0
- package/dist/{op-factory-call-DmQEc3XV.d.mts → op-factory-call-Dt-Jx5JS.d.mts} +46 -15
- package/dist/op-factory-call-Dt-Jx5JS.d.mts.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 +115 -3
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +1 -1
- package/dist/planner-BMBRPzsv.mjs +367 -0
- package/dist/planner-BMBRPzsv.mjs.map +1 -0
- package/dist/{planner-ddl-builders-B2wOwLqI.mjs → planner-ddl-builders-B8Nn6nHN.mjs} +12 -27
- package/dist/planner-ddl-builders-B8Nn6nHN.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-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-C0a7I1-e.d.mts} +4 -4
- package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-C0a7I1-e.d.mts.map} +1 -1
- package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-Dp6IO_gj.mjs} +3 -3
- package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-Dp6IO_gj.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-Cze3vMfe.mjs} +2 -2
- package/dist/{planner-sql-checks-jqUUGyQR.mjs.map → planner-sql-checks-Cze3vMfe.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 +55 -17
- package/dist/planner.d.mts.map +1 -1
- package/dist/planner.mjs +3 -2
- package/dist/{postgres-contract-serializer-k3TAcPMY.mjs → postgres-contract-view-3xfUgjnC.mjs} +44 -41
- package/dist/postgres-contract-view-3xfUgjnC.mjs.map +1 -0
- package/dist/postgres-contract-view-C9j5cqP0.d.mts +34 -0
- package/dist/postgres-contract-view-C9j5cqP0.d.mts.map +1 -0
- package/dist/postgres-database-schema-node-C9bTuMrd.d.mts +190 -0
- package/dist/postgres-database-schema-node-C9bTuMrd.d.mts.map +1 -0
- package/dist/{postgres-migration-B5jKrXv3.mjs → postgres-migration-D0jwzYDT.mjs} +21 -2
- package/dist/postgres-migration-D0jwzYDT.mjs.map +1 -0
- package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-DFaxuRyI.d.mts} +20 -7
- package/dist/postgres-migration-DFaxuRyI.d.mts.map +1 -0
- package/dist/postgres-native-enum-xdiqmBf_.mjs +41 -0
- package/dist/postgres-native-enum-xdiqmBf_.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-schema-COGZ1ark.mjs → postgres-schema-CajYAwny.mjs} +135 -7
- package/dist/postgres-schema-CajYAwny.mjs.map +1 -0
- package/dist/postgres-schema-DqnhQRvR.d.mts +226 -0
- package/dist/postgres-schema-DqnhQRvR.d.mts.map +1 -0
- package/dist/postgres-table-schema-node-BgaSrxYN.mjs +249 -0
- package/dist/postgres-table-schema-node-BgaSrxYN.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-Bv1ZvjX8.mjs +22 -0
- package/dist/resolve-ddl-schema-Bv1ZvjX8.mjs.map +1 -0
- package/dist/rls-canonicalize.d.mts +2 -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/schema-node-kinds-ClScchhi.mjs +76 -0
- package/dist/schema-node-kinds-ClScchhi.mjs.map +1 -0
- package/dist/types.d.mts +4 -140
- package/dist/types.mjs +3 -2
- package/package.json +23 -17
- package/src/contract-free/checks.ts +73 -0
- package/src/contract-free/ddl.ts +45 -1
- package/src/core/authoring.ts +287 -2
- package/src/core/codec-ids.ts +1 -0
- package/src/core/codec-type-map.ts +2 -0
- package/src/core/codecs.ts +141 -1
- package/src/core/ddl/nodes.ts +94 -2
- package/src/core/default-normalizer.ts +122 -2
- package/src/core/descriptor-meta.ts +6 -1
- package/src/core/entity-kinds.ts +30 -0
- 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 +149 -0
- package/src/core/migrations/control-policy.ts +70 -47
- package/src/core/migrations/diff-database-schema.ts +280 -0
- package/src/core/migrations/issue-planner.ts +647 -652
- package/src/core/migrations/op-factory-call.ts +109 -21
- 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/rls.ts +106 -0
- package/src/core/migrations/operations/shared.ts +0 -30
- package/src/core/migrations/planner-ddl-builders.ts +20 -58
- 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 +297 -59
- package/src/core/migrations/postgres-migration.ts +34 -4
- 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 +10 -16
- package/src/core/migrations/verify-postgres-namespaces.ts +47 -30
- package/src/core/namespace-ids.ts +3 -0
- package/src/core/postgres-contract-serializer.ts +41 -71
- package/src/core/postgres-contract-view.ts +48 -0
- package/src/core/postgres-native-enum.ts +51 -0
- package/src/core/postgres-rls-policy.ts +64 -0
- package/src/core/postgres-role.ts +37 -0
- package/src/core/postgres-schema-verifier.ts +10 -7
- package/src/core/postgres-schema.ts +62 -14
- package/src/core/postgres-validators.ts +27 -0
- package/src/core/psl-infer/infer-psl-contract.ts +1068 -0
- package/src/core/psl-infer/postgres-default-mapping.ts +16 -0
- package/src/core/psl-infer/postgres-type-map.ts +157 -0
- package/src/core/rls/canonicalize.ts +48 -0
- package/src/core/schema-ir/postgres-database-schema-node.ts +60 -0
- package/src/core/schema-ir/postgres-namespace-schema-node.ts +60 -0
- package/src/core/schema-ir/postgres-policy-schema-node.ts +90 -0
- package/src/core/schema-ir/postgres-role-schema-node.ts +63 -0
- package/src/core/schema-ir/postgres-table-schema-node.ts +118 -0
- package/src/core/schema-ir/schema-node-kinds.ts +106 -0
- package/src/exports/contract-free.ts +7 -1
- package/src/exports/control.ts +27 -33
- package/src/exports/ddl.ts +4 -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 +1 -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 +6 -0
- package/src/exports/runtime.ts +1 -0
- package/src/exports/schema-ir-annotations.ts +1 -0
- package/src/exports/types.ts +28 -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/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.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
package/src/core/ddl/nodes.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
DdlNode,
|
|
4
4
|
type DdlTableConstraint,
|
|
5
5
|
} from '@prisma-next/sql-relational-core/ast';
|
|
6
|
+
import type { RlsPolicyOperation } from '../rls/canonicalize';
|
|
6
7
|
|
|
7
8
|
// ---------------------------------------------------------------------------
|
|
8
9
|
// AlterTableAction — nested polymorphic hierarchy
|
|
@@ -10,6 +11,7 @@ import {
|
|
|
10
11
|
|
|
11
12
|
export interface AlterTableActionVisitor<R> {
|
|
12
13
|
addColumn(action: AddColumnAction): R;
|
|
14
|
+
dropDefault(action: DropDefaultAction): R;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
export abstract class AlterTableAction {
|
|
@@ -32,7 +34,31 @@ export class AddColumnAction extends AlterTableAction {
|
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
export
|
|
37
|
+
export class DropDefaultAction extends AlterTableAction {
|
|
38
|
+
readonly kind = 'drop-default' as const;
|
|
39
|
+
readonly columnName: string;
|
|
40
|
+
|
|
41
|
+
constructor(columnName: string) {
|
|
42
|
+
super();
|
|
43
|
+
this.columnName = columnName;
|
|
44
|
+
Object.freeze(this);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override accept<R>(visitor: AlterTableActionVisitor<R>): R {
|
|
48
|
+
return visitor.dropDefault(this);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The set of ALTER TABLE subactions currently expressible as typed DDL.
|
|
54
|
+
*
|
|
55
|
+
* The remaining actions — SetDefault, SetNotNull, DropNotNull,
|
|
56
|
+
* AlterColumnType — are still emitted as raw SQL by `operations/columns.ts`
|
|
57
|
+
* and join this union as they are converted to typed DDL. Until then it is
|
|
58
|
+
* intentionally partial: only the ALTER subactions used by the
|
|
59
|
+
* already-converted ops (AddColumn, DropDefault) appear here.
|
|
60
|
+
*/
|
|
61
|
+
export type AnyAlterTableAction = AddColumnAction | DropDefaultAction;
|
|
36
62
|
|
|
37
63
|
// ---------------------------------------------------------------------------
|
|
38
64
|
// Top-level DDL visitor
|
|
@@ -42,6 +68,8 @@ export interface PostgresDdlVisitor<R> {
|
|
|
42
68
|
createTable(node: PostgresCreateTable): R;
|
|
43
69
|
createSchema(node: PostgresCreateSchema): R;
|
|
44
70
|
alterTable(node: PostgresAlterTable): R;
|
|
71
|
+
createPolicy(node: PostgresCreatePolicy): R;
|
|
72
|
+
dropPolicy(node: PostgresDropPolicy): R;
|
|
45
73
|
}
|
|
46
74
|
|
|
47
75
|
export abstract class PostgresDdlNode extends DdlNode {
|
|
@@ -127,4 +155,68 @@ export class PostgresAlterTable extends PostgresDdlNode {
|
|
|
127
155
|
}
|
|
128
156
|
}
|
|
129
157
|
|
|
130
|
-
export type
|
|
158
|
+
export type { RlsPolicyOperation };
|
|
159
|
+
|
|
160
|
+
export class PostgresCreatePolicy extends PostgresDdlNode {
|
|
161
|
+
readonly kind = 'create-policy' as const;
|
|
162
|
+
readonly schema: string;
|
|
163
|
+
readonly table: string;
|
|
164
|
+
readonly name: string;
|
|
165
|
+
readonly permissive: boolean;
|
|
166
|
+
readonly operation: RlsPolicyOperation;
|
|
167
|
+
readonly roles: ReadonlyArray<string>;
|
|
168
|
+
readonly using: string | undefined;
|
|
169
|
+
readonly withCheck: string | undefined;
|
|
170
|
+
|
|
171
|
+
constructor(options: {
|
|
172
|
+
readonly schema: string;
|
|
173
|
+
readonly table: string;
|
|
174
|
+
readonly name: string;
|
|
175
|
+
readonly permissive: boolean;
|
|
176
|
+
readonly operation: RlsPolicyOperation;
|
|
177
|
+
readonly roles: readonly string[];
|
|
178
|
+
readonly using?: string;
|
|
179
|
+
readonly withCheck?: string;
|
|
180
|
+
}) {
|
|
181
|
+
super();
|
|
182
|
+
this.schema = options.schema;
|
|
183
|
+
this.table = options.table;
|
|
184
|
+
this.name = options.name;
|
|
185
|
+
this.permissive = options.permissive;
|
|
186
|
+
this.operation = options.operation;
|
|
187
|
+
this.roles = Object.freeze([...options.roles]);
|
|
188
|
+
this.using = options.using;
|
|
189
|
+
this.withCheck = options.withCheck;
|
|
190
|
+
this.freeze();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
override accept<R>(visitor: PostgresDdlVisitor<R>): R {
|
|
194
|
+
return visitor.createPolicy(this);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export class PostgresDropPolicy extends PostgresDdlNode {
|
|
199
|
+
readonly kind = 'drop-policy' as const;
|
|
200
|
+
readonly schema: string;
|
|
201
|
+
readonly table: string;
|
|
202
|
+
readonly name: string;
|
|
203
|
+
|
|
204
|
+
constructor(options: { readonly schema: string; readonly table: string; readonly name: string }) {
|
|
205
|
+
super();
|
|
206
|
+
this.schema = options.schema;
|
|
207
|
+
this.table = options.table;
|
|
208
|
+
this.name = options.name;
|
|
209
|
+
this.freeze();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
override accept<R>(visitor: PostgresDdlVisitor<R>): R {
|
|
213
|
+
return visitor.dropPolicy(this);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export type AnyPostgresDdlNode =
|
|
218
|
+
| PostgresCreateTable
|
|
219
|
+
| PostgresCreateSchema
|
|
220
|
+
| PostgresAlterTable
|
|
221
|
+
| PostgresCreatePolicy
|
|
222
|
+
| PostgresDropPolicy;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ColumnDefault } from '@prisma-next/contract/types';
|
|
1
|
+
import type { ColumnDefault, JsonValue } from '@prisma-next/contract/types';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Pre-compiled regex patterns for performance.
|
|
@@ -18,6 +18,13 @@ const FALSE_PATTERN = /^false$/i;
|
|
|
18
18
|
const NUMERIC_PATTERN = /^-?\d+(\.\d+)?$/;
|
|
19
19
|
const STRING_LITERAL_PATTERN = /^'((?:[^']|'')*)'(?:::(?:"[^"]+"|[\w\s]+)(?:\(\d+\))?)?$/;
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Matches a Postgres array literal default of the form `'{...}'::elemtype[]`.
|
|
23
|
+
* The literal body is captured in group 1; the cast (including `[]`) is optional.
|
|
24
|
+
* Examples: `'{}'::text[]`, `'{1,2}'::integer[]`, `'{}'`
|
|
25
|
+
*/
|
|
26
|
+
const ARRAY_LITERAL_PATTERN = /^'(\{.*\})'(?:::.+\[\])?$/;
|
|
27
|
+
|
|
21
28
|
/**
|
|
22
29
|
* Returns the canonical expression for a timestamp default function, or undefined
|
|
23
30
|
* if the expression is not a recognized timestamp default.
|
|
@@ -51,6 +58,108 @@ function canonicalizeTimestampDefault(expr: string): string | undefined {
|
|
|
51
58
|
return undefined;
|
|
52
59
|
}
|
|
53
60
|
|
|
61
|
+
type ArrayElementToken = { readonly value: string; readonly quoted: boolean };
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Splits a Postgres array literal body (without the enclosing braces) into its
|
|
65
|
+
* element tokens, honouring quoting. A comma only separates elements when it is
|
|
66
|
+
* outside double quotes; inside a quoted element a doubled quote (`""`) or a
|
|
67
|
+
* backslash-escaped quote (`\"`) is a literal quote, and a backslash escapes the
|
|
68
|
+
* next character. Returns undefined if the body is malformed (e.g. an unbalanced
|
|
69
|
+
* quote).
|
|
70
|
+
*/
|
|
71
|
+
function splitArrayElements(inner: string): readonly ArrayElementToken[] | undefined {
|
|
72
|
+
const tokens: ArrayElementToken[] = [];
|
|
73
|
+
let current = '';
|
|
74
|
+
let inQuotes = false;
|
|
75
|
+
let quoted = false;
|
|
76
|
+
|
|
77
|
+
for (let i = 0; i < inner.length; i++) {
|
|
78
|
+
const char = inner[i];
|
|
79
|
+
if (inQuotes) {
|
|
80
|
+
if (char === '\\') {
|
|
81
|
+
const next = inner[i + 1];
|
|
82
|
+
if (next === undefined) return undefined;
|
|
83
|
+
current += next;
|
|
84
|
+
i++;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (char === '"') {
|
|
88
|
+
if (inner[i + 1] === '"') {
|
|
89
|
+
current += '"';
|
|
90
|
+
i++;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
inQuotes = false;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
current += char;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (char === '"') {
|
|
100
|
+
inQuotes = true;
|
|
101
|
+
quoted = true;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (char === ',') {
|
|
105
|
+
tokens.push({ value: current, quoted });
|
|
106
|
+
current = '';
|
|
107
|
+
quoted = false;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
current += char;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (inQuotes) return undefined;
|
|
114
|
+
tokens.push({ value: current, quoted });
|
|
115
|
+
return tokens;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Parses a Postgres array literal body (`{...}`) into a JS array of primitives.
|
|
120
|
+
* Returns undefined if the body cannot be reliably parsed.
|
|
121
|
+
*
|
|
122
|
+
* Handles:
|
|
123
|
+
* - `{}` → `[]`
|
|
124
|
+
* - `{elem1,elem2,...}` → `[elem1, elem2, ...]` with numeric and string element coercion
|
|
125
|
+
* - quoted elements that contain commas, doubled/escaped quotes, and the literal
|
|
126
|
+
* strings `NULL`/`true`/`false` (a quoted token is always a string)
|
|
127
|
+
*/
|
|
128
|
+
function parseArrayLiteralBody(body: string): readonly JsonValue[] | undefined {
|
|
129
|
+
const inner = body.slice(1, -1).trim();
|
|
130
|
+
if (inner === '') return [];
|
|
131
|
+
const tokens = splitArrayElements(inner);
|
|
132
|
+
if (tokens === undefined) return undefined;
|
|
133
|
+
const result: JsonValue[] = [];
|
|
134
|
+
for (const token of tokens) {
|
|
135
|
+
if (token.quoted) {
|
|
136
|
+
// A quoted token is always a string — `"NULL"`, `"true"`, `"1"` are the
|
|
137
|
+
// literal text, never the keyword/number.
|
|
138
|
+
result.push(token.value);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const el = token.value.trim();
|
|
142
|
+
if (el.toUpperCase() === 'NULL') {
|
|
143
|
+
result.push(null);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (el === 'true') {
|
|
147
|
+
result.push(true);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (el === 'false') {
|
|
151
|
+
result.push(false);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (NUMERIC_PATTERN.test(el)) {
|
|
155
|
+
result.push(Number(el));
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
|
|
54
163
|
/**
|
|
55
164
|
* Parses a raw Postgres column default expression into a normalized ColumnDefault.
|
|
56
165
|
* This enables semantic comparison between contract defaults and introspected schema defaults.
|
|
@@ -59,7 +168,7 @@ function canonicalizeTimestampDefault(expr: string): string | undefined {
|
|
|
59
168
|
* keeping the introspection layer focused on faithful data capture.
|
|
60
169
|
*
|
|
61
170
|
* @param rawDefault - Raw default expression from information_schema.columns.column_default
|
|
62
|
-
* @param nativeType - Native column type, used for type-aware parsing (bigint
|
|
171
|
+
* @param nativeType - Native column type, used for type-aware parsing (array, bigint, JSON)
|
|
63
172
|
* @returns Normalized ColumnDefault or undefined if the expression cannot be parsed
|
|
64
173
|
*/
|
|
65
174
|
export function parsePostgresDefault(
|
|
@@ -69,11 +178,22 @@ export function parsePostgresDefault(
|
|
|
69
178
|
const trimmed = rawDefault.trim();
|
|
70
179
|
const normalizedType = nativeType?.toLowerCase();
|
|
71
180
|
const isBigInt = normalizedType === 'bigint' || normalizedType === 'int8';
|
|
181
|
+
const isArrayType = normalizedType?.endsWith('[]') ?? false;
|
|
72
182
|
|
|
73
183
|
if (NEXTVAL_PATTERN.test(trimmed)) {
|
|
74
184
|
return { kind: 'function', expression: 'autoincrement()' };
|
|
75
185
|
}
|
|
76
186
|
|
|
187
|
+
if (isArrayType) {
|
|
188
|
+
const arrayMatch = trimmed.match(ARRAY_LITERAL_PATTERN);
|
|
189
|
+
if (arrayMatch?.[1] !== undefined) {
|
|
190
|
+
const parsed = parseArrayLiteralBody(arrayMatch[1]);
|
|
191
|
+
if (parsed !== undefined) {
|
|
192
|
+
return { kind: 'literal', value: parsed };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
77
197
|
const canonicalTimestamp = canonicalizeTimestampDefault(trimmed);
|
|
78
198
|
if (canonicalTimestamp) {
|
|
79
199
|
return { kind: 'function', expression: canonicalTimestamp };
|
|
@@ -2,17 +2,22 @@ import type { CodecTypes } from '../exports/codec-types';
|
|
|
2
2
|
import {
|
|
3
3
|
postgresAuthoringEntityTypes,
|
|
4
4
|
postgresAuthoringFieldPresets,
|
|
5
|
+
postgresAuthoringPslBlockDescriptors,
|
|
5
6
|
postgresAuthoringTypes,
|
|
6
7
|
} from './authoring';
|
|
7
8
|
import { postgresTargetDescriptorMetaRuntime } from './descriptor-meta-runtime';
|
|
9
|
+
import { DEFAULT_NAMESPACE_ID } from './namespace-ids';
|
|
10
|
+
import { postgresCreateNamespace } from './postgres-schema';
|
|
8
11
|
|
|
9
12
|
const postgresTargetDescriptorMetaBase = {
|
|
10
13
|
...postgresTargetDescriptorMetaRuntime,
|
|
11
|
-
defaultNamespaceId:
|
|
14
|
+
defaultNamespaceId: DEFAULT_NAMESPACE_ID,
|
|
12
15
|
authoring: {
|
|
13
16
|
type: postgresAuthoringTypes,
|
|
14
17
|
field: postgresAuthoringFieldPresets,
|
|
15
18
|
entityTypes: postgresAuthoringEntityTypes,
|
|
19
|
+
pslBlockDescriptors: postgresAuthoringPslBlockDescriptors,
|
|
20
|
+
createNamespace: postgresCreateNamespace,
|
|
16
21
|
},
|
|
17
22
|
} as const;
|
|
18
23
|
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { EntityKindDescriptor } from '@prisma-next/framework-components/ir';
|
|
2
|
+
import { PostgresNativeEnum, type PostgresNativeEnumInput } from './postgres-native-enum';
|
|
3
|
+
import { PostgresRlsPolicy, type PostgresRlsPolicyInput } from './postgres-rls-policy';
|
|
4
|
+
import { PostgresRole, type PostgresRoleInput } from './postgres-role';
|
|
5
|
+
import {
|
|
6
|
+
PostgresNativeEnumSchema,
|
|
7
|
+
PostgresRlsPolicySchema,
|
|
8
|
+
PostgresRoleSchema,
|
|
9
|
+
} from './postgres-validators';
|
|
10
|
+
|
|
11
|
+
export const policyEntityKind: EntityKindDescriptor<PostgresRlsPolicyInput, PostgresRlsPolicy> = {
|
|
12
|
+
kind: 'policy',
|
|
13
|
+
schema: PostgresRlsPolicySchema,
|
|
14
|
+
construct: (input) => new PostgresRlsPolicy(input),
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const roleEntityKind: EntityKindDescriptor<PostgresRoleInput, PostgresRole> = {
|
|
18
|
+
kind: 'role',
|
|
19
|
+
schema: PostgresRoleSchema,
|
|
20
|
+
construct: (input) => new PostgresRole(input),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const nativeEnumEntityKind: EntityKindDescriptor<
|
|
24
|
+
PostgresNativeEnumInput,
|
|
25
|
+
PostgresNativeEnum
|
|
26
|
+
> = {
|
|
27
|
+
kind: 'native_enum',
|
|
28
|
+
schema: PostgresNativeEnumSchema,
|
|
29
|
+
construct: (input) => new PostgresNativeEnum(input),
|
|
30
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Maps a planner schema name to the contract-free DDL builder's optional
|
|
5
|
+
* `schema` field: the unbound sentinel becomes `undefined` (the builder omits
|
|
6
|
+
* the schema qualifier), every other name passes through.
|
|
7
|
+
*/
|
|
8
|
+
export function boundSchema(schemaName: string): string | undefined {
|
|
9
|
+
return schemaName === UNBOUND_NAMESPACE_ID ? undefined : schemaName;
|
|
10
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { CodecControlHooks } from '@prisma-next/family-sql/control';
|
|
2
|
+
import type { StorageColumn } from '@prisma-next/sql-contract/types';
|
|
3
|
+
import type { DdlColumn } from '@prisma-next/sql-relational-core/ast';
|
|
4
|
+
import * as contractFree from '@prisma-next/sql-relational-core/contract-free';
|
|
5
|
+
import type { SqlColumnDefaultIR, SqlColumnIR } from '@prisma-next/sql-schema-ir/types';
|
|
6
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
7
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
8
|
+
import { postgresDefaultToDdlColumnDefault } from './op-factory-call';
|
|
9
|
+
import { buildColumnDefaultSql, buildColumnTypeSql } from './planner-ddl-builders';
|
|
10
|
+
import { resolveIdentityValue } from './planner-identity-values';
|
|
11
|
+
import { buildExpectedFormatType } from './planner-sql-checks';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Reconstructs the `StorageColumn`-shaped fields the DDL builder functions
|
|
15
|
+
* (`buildColumnTypeSql`, `buildExpectedFormatType`, `resolveIdentityValue`)
|
|
16
|
+
* expect, from a column node's own stamped codec identity (`codecRef` /
|
|
17
|
+
* `codecBaseNativeType` / `codecNamedType`, Decision 5) — never the
|
|
18
|
+
* contract. The builders were written against `StorageColumn` and are
|
|
19
|
+
* unchanged here; only the shape feeding them moves from the contract to
|
|
20
|
+
* the node. An empty `storageTypes` catalog is passed alongside: the
|
|
21
|
+
* node's fields are already resolved past any `typeRef` indirection, so no
|
|
22
|
+
* live lookup is needed, and passing a non-empty catalog would risk a
|
|
23
|
+
* false `typeRef` hit against an unrelated storage type.
|
|
24
|
+
*/
|
|
25
|
+
function columnLike(
|
|
26
|
+
column: SqlColumnIR,
|
|
27
|
+
): Pick<
|
|
28
|
+
StorageColumn,
|
|
29
|
+
'nativeType' | 'codecId' | 'nullable' | 'many' | 'typeParams' | 'typeRef' | 'default'
|
|
30
|
+
> {
|
|
31
|
+
if (column.codecRef === undefined || column.codecBaseNativeType === undefined) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`columnLike: expected column "${column.name}" carries no codec identity — the expected tree must be derived via contractToSchemaIR for planning`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
nativeType: column.codecBaseNativeType,
|
|
38
|
+
codecId: column.codecRef.codecId,
|
|
39
|
+
nullable: column.nullable,
|
|
40
|
+
// `column.many` is unset on contract-derived columns (array-ness rides
|
|
41
|
+
// on the `nativeType` `[]` suffix there instead) — `codecRef.many`
|
|
42
|
+
// carries it. Hand-built/introspected columns set `column.many` directly.
|
|
43
|
+
...ifDefined('many', column.many ?? column.codecRef.many),
|
|
44
|
+
...ifDefined(
|
|
45
|
+
'typeParams',
|
|
46
|
+
column.codecRef.typeParams !== undefined
|
|
47
|
+
? blindCast<
|
|
48
|
+
Record<string, unknown>,
|
|
49
|
+
'CodecRef.typeParams is JsonValue-shaped; the DDL builders only ever read it as the Record the contract column originally carried'
|
|
50
|
+
>(column.codecRef.typeParams)
|
|
51
|
+
: undefined,
|
|
52
|
+
),
|
|
53
|
+
...(column.codecNamedType ? { typeRef: '<resolved>' } : {}),
|
|
54
|
+
...ifDefined('default', column.resolvedDefault),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Builds the `CREATE TABLE` / `ADD COLUMN` DDL column for an expected column
|
|
60
|
+
* node, resolving type rendering from the node's codec identity against the
|
|
61
|
+
* codec hooks the caller holds — the same builder the pre-`plan(start, end)`
|
|
62
|
+
* op-path called, so the output is byte-identical.
|
|
63
|
+
*/
|
|
64
|
+
export function renderColumnDdl(
|
|
65
|
+
name: string,
|
|
66
|
+
column: SqlColumnIR,
|
|
67
|
+
codecHooks: ReadonlyMap<string, CodecControlHooks>,
|
|
68
|
+
): DdlColumn {
|
|
69
|
+
const like = columnLike(column);
|
|
70
|
+
const typeSql = buildColumnTypeSql(like, codecHooks, {});
|
|
71
|
+
const ddlDefault = postgresDefaultToDdlColumnDefault(like.default);
|
|
72
|
+
return contractFree.col(name, typeSql, {
|
|
73
|
+
...(!column.nullable ? { notNull: true } : {}),
|
|
74
|
+
...ifDefined('default', ddlDefault),
|
|
75
|
+
...ifDefined('codecRef', column.codecRef),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Builds the `ALTER COLUMN … TYPE` operands for an expected column node.
|
|
81
|
+
*/
|
|
82
|
+
export function renderColumnAlterType(
|
|
83
|
+
column: SqlColumnIR,
|
|
84
|
+
codecHooks: ReadonlyMap<string, CodecControlHooks>,
|
|
85
|
+
): { readonly qualifiedTargetType: string; readonly formatTypeExpected: string } {
|
|
86
|
+
const like = columnLike(column);
|
|
87
|
+
return {
|
|
88
|
+
qualifiedTargetType: buildColumnTypeSql(like, codecHooks, {}, false),
|
|
89
|
+
formatTypeExpected: buildExpectedFormatType(like, codecHooks, {}),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Resolves the identity value (monoid neutral element) SQL literal used as
|
|
95
|
+
* the temporary default when adding a NOT-NULL column with no contract
|
|
96
|
+
* default (`notNullAddColumnCallStrategy`'s shared-temp-default backfill).
|
|
97
|
+
* `null` when the column's type has no built-in/codec-provided identity
|
|
98
|
+
* value.
|
|
99
|
+
*/
|
|
100
|
+
export function resolveColumnTemporaryDefault(
|
|
101
|
+
column: SqlColumnIR,
|
|
102
|
+
codecHooks: ReadonlyMap<string, CodecControlHooks>,
|
|
103
|
+
): string | null {
|
|
104
|
+
return resolveIdentityValue(columnLike(column), codecHooks, {});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The column's `SET DEFAULT` clause SQL, resolved from a column-default
|
|
109
|
+
* diff node. `''` when the node carries no resolved default.
|
|
110
|
+
*/
|
|
111
|
+
export function renderColumnDefaultSql(defaultNode: SqlColumnDefaultIR): string {
|
|
112
|
+
if (defaultNode.resolved === undefined) return '';
|
|
113
|
+
return buildColumnDefaultSql(defaultNode.resolved, {
|
|
114
|
+
nativeType: defaultNode.nativeTypeContext ?? '',
|
|
115
|
+
...ifDefined('many', defaultNode.many),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { ContractToSchemaIROptions } from '@prisma-next/family-sql/control';
|
|
2
|
+
import { contractNamespaceToSchemaIR } from '@prisma-next/family-sql/control';
|
|
3
|
+
import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
|
|
4
|
+
import { SqlForeignKeyIR } from '@prisma-next/sql-schema-ir/types';
|
|
5
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
6
|
+
import type { PostgresRlsPolicy } from '../postgres-rls-policy';
|
|
7
|
+
import type { PostgresContract } from '../postgres-schema';
|
|
8
|
+
import { isPostgresSchema } from '../postgres-schema';
|
|
9
|
+
import { PostgresDatabaseSchemaNode } from '../schema-ir/postgres-database-schema-node';
|
|
10
|
+
import { PostgresNamespaceSchemaNode } from '../schema-ir/postgres-namespace-schema-node';
|
|
11
|
+
import { PostgresPolicySchemaNode } from '../schema-ir/postgres-policy-schema-node';
|
|
12
|
+
import { PostgresRoleSchemaNode } from '../schema-ir/postgres-role-schema-node';
|
|
13
|
+
import { PostgresTableSchemaNode } from '../schema-ir/postgres-table-schema-node';
|
|
14
|
+
import { resolveDdlSchemaForNamespaceStorage } from './resolve-ddl-schema';
|
|
15
|
+
|
|
16
|
+
function toPolicyNode(policy: PostgresRlsPolicy, namespaceId: string): PostgresPolicySchemaNode {
|
|
17
|
+
return new PostgresPolicySchemaNode({
|
|
18
|
+
name: policy.name,
|
|
19
|
+
prefix: policy.prefix,
|
|
20
|
+
tableName: policy.tableName,
|
|
21
|
+
namespaceId,
|
|
22
|
+
operation: policy.operation,
|
|
23
|
+
roles: [...policy.roles],
|
|
24
|
+
...ifDefined('using', policy.using),
|
|
25
|
+
...ifDefined('withCheck', policy.withCheck),
|
|
26
|
+
permissive: policy.permissive,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Projects a Postgres contract into the expected schema-diff tree: a
|
|
32
|
+
* `PostgresDatabaseSchemaNode` root holding one `PostgresNamespaceSchemaNode`
|
|
33
|
+
* per Postgres namespace, each holding its `PostgresTableSchemaNode`s with
|
|
34
|
+
* their `PostgresPolicySchemaNode`s, plus the database roles on the root.
|
|
35
|
+
*
|
|
36
|
+
* Not a duplicate of the family's `contractToSchemaIR`: that builds a flat,
|
|
37
|
+
* single `{ tables }` map (and throws on cross-namespace name collisions, with
|
|
38
|
+
* no RLS/role concept) for SQLite's single-schema world. This is the
|
|
39
|
+
* Postgres-specific *tree* shape — multi-schema, RLS-policy-aware, role-aware.
|
|
40
|
+
* It reuses the family's per-namespace table conversion (`contractNamespaceToSchemaIR`)
|
|
41
|
+
* for column/FK/index building and only adds the Postgres tree/policy/role shape.
|
|
42
|
+
*
|
|
43
|
+
* Tables are grouped by their owning namespace (resolved DDL schema name) so
|
|
44
|
+
* the tree mirrors Postgres's object hierarchy. The DDL schema name is
|
|
45
|
+
* resolved once per namespace.
|
|
46
|
+
*
|
|
47
|
+
* A policy that references a table absent from its namespace is a malformed
|
|
48
|
+
* contract — the loop throws rather than fabricating a stub table.
|
|
49
|
+
*/
|
|
50
|
+
export function contractToPostgresDatabaseSchemaNode(
|
|
51
|
+
contract: PostgresContract | null,
|
|
52
|
+
options: ContractToSchemaIROptions,
|
|
53
|
+
): PostgresDatabaseSchemaNode {
|
|
54
|
+
if (contract === null) {
|
|
55
|
+
return new PostgresDatabaseSchemaNode({
|
|
56
|
+
namespaces: {},
|
|
57
|
+
roles: [],
|
|
58
|
+
existingSchemas: [],
|
|
59
|
+
pgVersion: '',
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const namespaces: Record<string, PostgresNamespaceSchemaNode> = {};
|
|
64
|
+
const roles: PostgresRoleSchemaNode[] = [];
|
|
65
|
+
const ownedSchemas: string[] = [];
|
|
66
|
+
|
|
67
|
+
for (const ns of Object.values(contract.storage.namespaces)) {
|
|
68
|
+
if (!isPostgresSchema(ns)) continue;
|
|
69
|
+
const ddlSchema = resolveDdlSchemaForNamespaceStorage(contract.storage, ns.id);
|
|
70
|
+
ownedSchemas.push(ddlSchema);
|
|
71
|
+
|
|
72
|
+
// Convert only THIS namespace's tables (passing the full storage for
|
|
73
|
+
// type/value-set/enum resolution that spans namespaces), so the same table
|
|
74
|
+
// name can exist in two schemas without colliding in a bare-keyed record.
|
|
75
|
+
const sqlTables = contractNamespaceToSchemaIR(contract.storage, ns.id, options).tables;
|
|
76
|
+
|
|
77
|
+
const policiesByTable = new Map<string, PostgresPolicySchemaNode[]>();
|
|
78
|
+
for (const policy of Object.values(ns.policy)) {
|
|
79
|
+
const list = policiesByTable.get(policy.tableName) ?? [];
|
|
80
|
+
list.push(toPolicyNode(policy, ddlSchema));
|
|
81
|
+
policiesByTable.set(policy.tableName, list);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const tables: Record<string, PostgresTableSchemaNode> = {};
|
|
85
|
+
for (const tableName of Object.keys(ns.table)) {
|
|
86
|
+
const sqlTable = sqlTables[tableName];
|
|
87
|
+
if (sqlTable === undefined) continue;
|
|
88
|
+
// The family conversion stamps `referencedSchema` with the FK target's
|
|
89
|
+
// namespace id verbatim, which can be the unbound sentinel. Resolve it
|
|
90
|
+
// to the real live DDL schema here — introspected FKs already carry the
|
|
91
|
+
// live schema, so this is what lets an expected FK pair (by diff-node
|
|
92
|
+
// id) with its introspected counterpart.
|
|
93
|
+
const foreignKeys = sqlTable.foreignKeys.map(
|
|
94
|
+
(fk) =>
|
|
95
|
+
new SqlForeignKeyIR({
|
|
96
|
+
columns: fk.columns,
|
|
97
|
+
referencedTable: fk.referencedTable,
|
|
98
|
+
referencedColumns: fk.referencedColumns,
|
|
99
|
+
...ifDefined('referencedSchema', fk.referencedSchema),
|
|
100
|
+
...ifDefined('name', fk.name),
|
|
101
|
+
...ifDefined('onDelete', fk.onDelete),
|
|
102
|
+
...ifDefined('onUpdate', fk.onUpdate),
|
|
103
|
+
...ifDefined('annotations', fk.annotations),
|
|
104
|
+
resolvedReferencedNamespace: resolveDdlSchemaForNamespaceStorage(
|
|
105
|
+
contract.storage,
|
|
106
|
+
fk.referencedSchema ?? UNBOUND_NAMESPACE_ID,
|
|
107
|
+
),
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
tables[tableName] = new PostgresTableSchemaNode({
|
|
111
|
+
name: sqlTable.name,
|
|
112
|
+
columns: sqlTable.columns,
|
|
113
|
+
foreignKeys,
|
|
114
|
+
uniques: sqlTable.uniques,
|
|
115
|
+
indexes: sqlTable.indexes,
|
|
116
|
+
...ifDefined('primaryKey', sqlTable.primaryKey),
|
|
117
|
+
...ifDefined('annotations', sqlTable.annotations),
|
|
118
|
+
...ifDefined('checks', sqlTable.checks),
|
|
119
|
+
policies: policiesByTable.get(tableName) ?? [],
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
for (const [tableName, tablePolicies] of policiesByTable) {
|
|
124
|
+
if (!(tableName in tables)) {
|
|
125
|
+
const policyName = tablePolicies[0]?.name ?? '(unknown)';
|
|
126
|
+
throw new Error(
|
|
127
|
+
`contract-to-postgres-database-schema-node: policy "${policyName}" references table "${tableName}" not present in namespace "${ddlSchema}"`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
namespaces[ddlSchema] = new PostgresNamespaceSchemaNode({
|
|
133
|
+
schemaName: ddlSchema,
|
|
134
|
+
tables,
|
|
135
|
+
nativeEnumTypeNames: [],
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
for (const role of Object.values(ns.role)) {
|
|
139
|
+
roles.push(new PostgresRoleSchemaNode({ name: role.name, namespaceId: role.namespaceId }));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return new PostgresDatabaseSchemaNode({
|
|
144
|
+
namespaces,
|
|
145
|
+
roles,
|
|
146
|
+
existingSchemas: ownedSchemas,
|
|
147
|
+
pgVersion: '',
|
|
148
|
+
});
|
|
149
|
+
}
|