@prisma-next/target-postgres 0.14.0 → 0.15.0-dev.10
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-DaLdbcYE.mjs +1254 -0
- package/dist/control-policy-DaLdbcYE.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-oY3brcLj.mjs +368 -0
- package/dist/diff-database-schema-oY3brcLj.mjs.map +1 -0
- package/dist/diff-database-schema.d.mts +37 -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-CvH0JeBW.d.mts} +119 -17
- package/dist/op-factory-call-CvH0JeBW.d.mts.map +1 -0
- package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-tBcLo_B8.mjs} +496 -38
- package/dist/op-factory-call-tBcLo_B8.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-ootbxGty.mjs +390 -0
- package/dist/planner-ootbxGty.mjs.map +1 -0
- package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-DVsaobdV.mjs} +3 -3
- package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-DVsaobdV.mjs.map} +1 -1
- package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-k33bb9Sm.d.mts} +4 -4
- package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-k33bb9Sm.d.mts.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-DJOTywwQ.d.mts +271 -0
- package/dist/postgres-database-schema-node-DJOTywwQ.d.mts.map +1 -0
- package/dist/postgres-migration-Bt3sXoID.mjs +194 -0
- package/dist/postgres-migration-Bt3sXoID.mjs.map +1 -0
- package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-ulfqRGA1.d.mts} +83 -9
- package/dist/postgres-migration-ulfqRGA1.d.mts.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-bg32e7I-.mjs +191 -0
- package/dist/postgres-role-schema-node-bg32e7I-.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/schema-node-kinds-CrYzCR1i.d.mts +30 -0
- package/dist/schema-node-kinds-CrYzCR1i.d.mts.map +1 -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 +5 -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 +269 -0
- package/src/core/migrations/control-policy.ts +218 -65
- package/src/core/migrations/diff-database-schema.ts +252 -0
- package/src/core/migrations/issue-planner.ts +782 -660
- package/src/core/migrations/op-factory-call.ts +415 -31
- 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 +250 -218
- package/src/core/migrations/planner-target-details.ts +3 -1
- package/src/core/migrations/planner.ts +358 -67
- package/src/core/migrations/postgres-migration.ts +178 -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 +45 -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 +100 -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 +9 -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 +38 -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 +0 -145
- 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
|
|
@@ -41,7 +67,13 @@ export type AnyAlterTableAction = AddColumnAction;
|
|
|
41
67
|
export interface PostgresDdlVisitor<R> {
|
|
42
68
|
createTable(node: PostgresCreateTable): R;
|
|
43
69
|
createSchema(node: PostgresCreateSchema): R;
|
|
70
|
+
createType(node: PostgresCreateType): R;
|
|
71
|
+
dropType(node: PostgresDropType): R;
|
|
44
72
|
alterTable(node: PostgresAlterTable): R;
|
|
73
|
+
createPolicy(node: PostgresCreatePolicy): R;
|
|
74
|
+
dropPolicy(node: PostgresDropPolicy): R;
|
|
75
|
+
alterPolicyRename(node: PostgresAlterPolicyRename): R;
|
|
76
|
+
disableRowLevelSecurity(node: PostgresDisableRowLevelSecurity): R;
|
|
45
77
|
}
|
|
46
78
|
|
|
47
79
|
export abstract class PostgresDdlNode extends DdlNode {
|
|
@@ -104,6 +136,46 @@ export class PostgresCreateSchema extends PostgresDdlNode {
|
|
|
104
136
|
}
|
|
105
137
|
}
|
|
106
138
|
|
|
139
|
+
export class PostgresCreateType extends PostgresDdlNode {
|
|
140
|
+
readonly kind = 'create-type' as const;
|
|
141
|
+
readonly schema: string | undefined;
|
|
142
|
+
readonly name: string;
|
|
143
|
+
readonly values: ReadonlyArray<string>;
|
|
144
|
+
|
|
145
|
+
constructor(options: {
|
|
146
|
+
readonly schema?: string;
|
|
147
|
+
readonly name: string;
|
|
148
|
+
readonly values: readonly string[];
|
|
149
|
+
}) {
|
|
150
|
+
super();
|
|
151
|
+
this.schema = options.schema;
|
|
152
|
+
this.name = options.name;
|
|
153
|
+
this.values = Object.freeze([...options.values]);
|
|
154
|
+
this.freeze();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
override accept<R>(visitor: PostgresDdlVisitor<R>): R {
|
|
158
|
+
return visitor.createType(this);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export class PostgresDropType extends PostgresDdlNode {
|
|
163
|
+
readonly kind = 'drop-type' as const;
|
|
164
|
+
readonly schema: string | undefined;
|
|
165
|
+
readonly name: string;
|
|
166
|
+
|
|
167
|
+
constructor(options: { readonly schema?: string; readonly name: string }) {
|
|
168
|
+
super();
|
|
169
|
+
this.schema = options.schema;
|
|
170
|
+
this.name = options.name;
|
|
171
|
+
this.freeze();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
override accept<R>(visitor: PostgresDdlVisitor<R>): R {
|
|
175
|
+
return visitor.dropType(this);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
107
179
|
export class PostgresAlterTable extends PostgresDdlNode {
|
|
108
180
|
readonly kind = 'alter-table' as const;
|
|
109
181
|
readonly table: string;
|
|
@@ -127,4 +199,115 @@ export class PostgresAlterTable extends PostgresDdlNode {
|
|
|
127
199
|
}
|
|
128
200
|
}
|
|
129
201
|
|
|
130
|
-
export type
|
|
202
|
+
export type { RlsPolicyOperation };
|
|
203
|
+
|
|
204
|
+
export class PostgresCreatePolicy extends PostgresDdlNode {
|
|
205
|
+
readonly kind = 'create-policy' as const;
|
|
206
|
+
readonly schema: string;
|
|
207
|
+
readonly table: string;
|
|
208
|
+
readonly name: string;
|
|
209
|
+
readonly permissive: boolean;
|
|
210
|
+
readonly operation: RlsPolicyOperation;
|
|
211
|
+
readonly roles: ReadonlyArray<string>;
|
|
212
|
+
readonly using: string | undefined;
|
|
213
|
+
readonly withCheck: string | undefined;
|
|
214
|
+
|
|
215
|
+
constructor(options: {
|
|
216
|
+
readonly schema: string;
|
|
217
|
+
readonly table: string;
|
|
218
|
+
readonly name: string;
|
|
219
|
+
readonly permissive: boolean;
|
|
220
|
+
readonly operation: RlsPolicyOperation;
|
|
221
|
+
readonly roles: readonly string[];
|
|
222
|
+
readonly using?: string;
|
|
223
|
+
readonly withCheck?: string;
|
|
224
|
+
}) {
|
|
225
|
+
super();
|
|
226
|
+
this.schema = options.schema;
|
|
227
|
+
this.table = options.table;
|
|
228
|
+
this.name = options.name;
|
|
229
|
+
this.permissive = options.permissive;
|
|
230
|
+
this.operation = options.operation;
|
|
231
|
+
this.roles = Object.freeze([...options.roles]);
|
|
232
|
+
this.using = options.using;
|
|
233
|
+
this.withCheck = options.withCheck;
|
|
234
|
+
this.freeze();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
override accept<R>(visitor: PostgresDdlVisitor<R>): R {
|
|
238
|
+
return visitor.createPolicy(this);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export class PostgresDropPolicy extends PostgresDdlNode {
|
|
243
|
+
readonly kind = 'drop-policy' as const;
|
|
244
|
+
readonly schema: string;
|
|
245
|
+
readonly table: string;
|
|
246
|
+
readonly name: string;
|
|
247
|
+
|
|
248
|
+
constructor(options: { readonly schema: string; readonly table: string; readonly name: string }) {
|
|
249
|
+
super();
|
|
250
|
+
this.schema = options.schema;
|
|
251
|
+
this.table = options.table;
|
|
252
|
+
this.name = options.name;
|
|
253
|
+
this.freeze();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
override accept<R>(visitor: PostgresDdlVisitor<R>): R {
|
|
257
|
+
return visitor.dropPolicy(this);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export class PostgresAlterPolicyRename extends PostgresDdlNode {
|
|
262
|
+
readonly kind = 'alter-policy-rename' as const;
|
|
263
|
+
readonly schema: string;
|
|
264
|
+
readonly table: string;
|
|
265
|
+
readonly name: string;
|
|
266
|
+
readonly newName: string;
|
|
267
|
+
|
|
268
|
+
constructor(options: {
|
|
269
|
+
readonly schema: string;
|
|
270
|
+
readonly table: string;
|
|
271
|
+
readonly name: string;
|
|
272
|
+
readonly newName: string;
|
|
273
|
+
}) {
|
|
274
|
+
super();
|
|
275
|
+
this.schema = options.schema;
|
|
276
|
+
this.table = options.table;
|
|
277
|
+
this.name = options.name;
|
|
278
|
+
this.newName = options.newName;
|
|
279
|
+
this.freeze();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
override accept<R>(visitor: PostgresDdlVisitor<R>): R {
|
|
283
|
+
return visitor.alterPolicyRename(this);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export class PostgresDisableRowLevelSecurity extends PostgresDdlNode {
|
|
288
|
+
readonly kind = 'disable-row-level-security' as const;
|
|
289
|
+
readonly schema: string;
|
|
290
|
+
readonly table: string;
|
|
291
|
+
|
|
292
|
+
constructor(options: { readonly schema: string; readonly table: string }) {
|
|
293
|
+
super();
|
|
294
|
+
this.schema = options.schema;
|
|
295
|
+
this.table = options.table;
|
|
296
|
+
this.freeze();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
override accept<R>(visitor: PostgresDdlVisitor<R>): R {
|
|
300
|
+
return visitor.disableRowLevelSecurity(this);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export type AnyPostgresDdlNode =
|
|
305
|
+
| PostgresCreateTable
|
|
306
|
+
| PostgresCreateSchema
|
|
307
|
+
| PostgresCreateType
|
|
308
|
+
| PostgresDropType
|
|
309
|
+
| PostgresAlterTable
|
|
310
|
+
| PostgresCreatePolicy
|
|
311
|
+
| PostgresDropPolicy
|
|
312
|
+
| PostgresAlterPolicyRename
|
|
313
|
+
| PostgresDisableRowLevelSecurity;
|
|
@@ -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,28 @@ import type { CodecTypes } from '../exports/codec-types';
|
|
|
2
2
|
import {
|
|
3
3
|
postgresAuthoringEntityTypes,
|
|
4
4
|
postgresAuthoringFieldPresets,
|
|
5
|
+
postgresAuthoringModelAttributes,
|
|
6
|
+
postgresAuthoringPslBlockDescriptors,
|
|
5
7
|
postgresAuthoringTypes,
|
|
8
|
+
postgresLowerEntityHandles,
|
|
6
9
|
} from './authoring';
|
|
10
|
+
import { postgresQualifyColumnType } from './codecs';
|
|
7
11
|
import { postgresTargetDescriptorMetaRuntime } from './descriptor-meta-runtime';
|
|
12
|
+
import { DEFAULT_NAMESPACE_ID } from './namespace-ids';
|
|
13
|
+
import { postgresCreateNamespace } from './postgres-schema';
|
|
8
14
|
|
|
9
15
|
const postgresTargetDescriptorMetaBase = {
|
|
10
16
|
...postgresTargetDescriptorMetaRuntime,
|
|
11
|
-
defaultNamespaceId:
|
|
17
|
+
defaultNamespaceId: DEFAULT_NAMESPACE_ID,
|
|
12
18
|
authoring: {
|
|
13
19
|
type: postgresAuthoringTypes,
|
|
14
20
|
field: postgresAuthoringFieldPresets,
|
|
15
21
|
entityTypes: postgresAuthoringEntityTypes,
|
|
22
|
+
pslBlockDescriptors: postgresAuthoringPslBlockDescriptors,
|
|
23
|
+
modelAttributes: postgresAuthoringModelAttributes,
|
|
24
|
+
createNamespace: postgresCreateNamespace,
|
|
25
|
+
qualifyColumnType: postgresQualifyColumnType,
|
|
26
|
+
lowerEntityHandles: postgresLowerEntityHandles,
|
|
16
27
|
},
|
|
17
28
|
} as const;
|
|
18
29
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { EntityKindDescriptor } from '@prisma-next/framework-components/ir';
|
|
2
|
+
import { PostgresNativeEnum, type PostgresNativeEnumInput } from './postgres-native-enum';
|
|
3
|
+
import { PostgresRlsEnablement, type PostgresRlsEnablementInput } from './postgres-rls-enablement';
|
|
4
|
+
import { PostgresRlsPolicy, type PostgresRlsPolicyInput } from './postgres-rls-policy';
|
|
5
|
+
import { PostgresRole, type PostgresRoleInput } from './postgres-role';
|
|
6
|
+
import {
|
|
7
|
+
PostgresNativeEnumSchema,
|
|
8
|
+
PostgresRlsEnablementSchema,
|
|
9
|
+
PostgresRlsPolicySchema,
|
|
10
|
+
PostgresRoleSchema,
|
|
11
|
+
} from './postgres-validators';
|
|
12
|
+
|
|
13
|
+
export const policyEntityKind: EntityKindDescriptor<PostgresRlsPolicyInput, PostgresRlsPolicy> = {
|
|
14
|
+
kind: 'policy',
|
|
15
|
+
schema: PostgresRlsPolicySchema,
|
|
16
|
+
construct: (input) => new PostgresRlsPolicy(input),
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const roleEntityKind: EntityKindDescriptor<PostgresRoleInput, PostgresRole> = {
|
|
20
|
+
kind: 'role',
|
|
21
|
+
schema: PostgresRoleSchema,
|
|
22
|
+
construct: (input) => new PostgresRole(input),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const rlsEnablementEntityKind: EntityKindDescriptor<
|
|
26
|
+
PostgresRlsEnablementInput,
|
|
27
|
+
PostgresRlsEnablement
|
|
28
|
+
> = {
|
|
29
|
+
kind: 'rls',
|
|
30
|
+
schema: PostgresRlsEnablementSchema,
|
|
31
|
+
construct: (input) => new PostgresRlsEnablement(input),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const nativeEnumEntityKind: EntityKindDescriptor<
|
|
35
|
+
PostgresNativeEnumInput,
|
|
36
|
+
PostgresNativeEnum
|
|
37
|
+
> = {
|
|
38
|
+
kind: 'native_enum',
|
|
39
|
+
schema: PostgresNativeEnumSchema,
|
|
40
|
+
construct: (input) => new PostgresNativeEnum(input),
|
|
41
|
+
};
|
package/src/core/errors.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { CliStructuredError } from '@prisma-next/errors/control';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* A `PostgresMigration`
|
|
5
|
-
*
|
|
6
|
-
* migration was constructed without a `ControlStack`.
|
|
7
|
-
* usage always goes through the migration CLI entrypoint,
|
|
8
|
-
* a stack from the loaded `prisma-next.config.ts`; reaching this
|
|
9
|
-
* means a test fixture or ad-hoc consumer instantiated `PostgresMigration`
|
|
10
|
-
* with the no-arg form (legal for `operations` / `describe` introspection
|
|
11
|
-
*
|
|
4
|
+
* A `PostgresMigration` operation that needs the materialized control adapter
|
|
5
|
+
* — named by `operation` (e.g. `createTable`, `dropColumn`, `dataTransform`) —
|
|
6
|
+
* was invoked, but the migration was constructed without a `ControlStack`.
|
|
7
|
+
* Concrete authoring usage always goes through the migration CLI entrypoint,
|
|
8
|
+
* which assembles a stack from the loaded `prisma-next.config.ts`; reaching this
|
|
9
|
+
* error means a test fixture or ad-hoc consumer instantiated `PostgresMigration`
|
|
10
|
+
* with the no-arg form (legal for `operations` / `describe` introspection only).
|
|
11
|
+
*
|
|
12
|
+
* The `operation` argument is required so every throw site names the operation
|
|
13
|
+
* that actually failed; a new operation cannot inherit a misattributed message.
|
|
12
14
|
*
|
|
13
15
|
* Distinct from `PN-MIG-2001` (placeholder not filled) and `PN-MIG-2005`
|
|
14
16
|
* (data-transform query plan against wrong contract) because the missing
|
|
@@ -19,15 +21,15 @@ import { CliStructuredError } from '@prisma-next/errors/control';
|
|
|
19
21
|
* the contract it talks about (`PostgresMigration`, the Postgres control
|
|
20
22
|
* adapter, the Postgres-target stack) only exists in this package.
|
|
21
23
|
*/
|
|
22
|
-
export function errorPostgresMigrationStackMissing(): CliStructuredError {
|
|
24
|
+
export function errorPostgresMigrationStackMissing(operation: string): CliStructuredError {
|
|
23
25
|
return new CliStructuredError(
|
|
24
26
|
'2007',
|
|
25
|
-
|
|
27
|
+
`PostgresMigration.${operation} requires a control adapter`,
|
|
26
28
|
{
|
|
27
29
|
domain: 'MIG',
|
|
28
|
-
why:
|
|
30
|
+
why: `PostgresMigration.${operation} was invoked on an instance constructed without a ControlStack, so the stored controlAdapter is undefined and the operation cannot lower its plan.`,
|
|
29
31
|
fix: 'Construct the migration via the migration CLI entrypoint (which assembles a ControlStack from the loaded prisma-next.config.ts), or pass a ControlStack containing a Postgres adapter to the migration constructor in test fixtures.',
|
|
30
|
-
meta: {},
|
|
32
|
+
meta: { operation },
|
|
31
33
|
},
|
|
32
34
|
);
|
|
33
35
|
}
|
|
@@ -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
|
+
}
|