@prisma-next/target-postgres 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/dist/{codec-ids-CTikp1if.mjs → codec-ids-BvytN2P8.mjs} +3 -3
  2. package/dist/codec-ids-BvytN2P8.mjs.map +1 -0
  3. package/dist/{codec-ids-B1vOchLE.d.mts → codec-ids-CnXu9Qy3.d.mts} +3 -3
  4. package/dist/codec-ids-CnXu9Qy3.d.mts.map +1 -0
  5. package/dist/codec-ids.d.mts +2 -2
  6. package/dist/codec-ids.mjs +2 -2
  7. package/dist/{codec-types-CnFiNML4.d.mts → codec-types-DHCkwPKE.d.mts} +3 -3
  8. package/dist/{codec-types-CnFiNML4.d.mts.map → codec-types-DHCkwPKE.d.mts.map} +1 -1
  9. package/dist/codec-types.d.mts +1 -1
  10. package/dist/{codecs-CBpEv4s5.d.mts → codecs--0A5_4Bq.d.mts} +26 -23
  11. package/dist/codecs--0A5_4Bq.d.mts.map +1 -0
  12. package/dist/codecs.d.mts +2 -2
  13. package/dist/codecs.mjs +28 -35
  14. package/dist/codecs.mjs.map +1 -1
  15. package/dist/contract-free.d.mts +163 -15
  16. package/dist/contract-free.d.mts.map +1 -1
  17. package/dist/contract-free.mjs +4 -17
  18. package/dist/contract-free.mjs.map +1 -1
  19. package/dist/control.d.mts.map +1 -1
  20. package/dist/control.mjs +21 -27
  21. package/dist/control.mjs.map +1 -1
  22. package/dist/{data-transform-D25tLeYU.mjs → data-transform-BOWpliq8.mjs} +9 -17
  23. package/dist/data-transform-BOWpliq8.mjs.map +1 -0
  24. package/dist/{data-transform-DGOqcLrf.d.mts → data-transform-DDgWdB5o.d.mts} +2 -2
  25. package/dist/data-transform-DDgWdB5o.d.mts.map +1 -0
  26. package/dist/data-transform.d.mts +1 -1
  27. package/dist/data-transform.mjs +1 -1
  28. package/dist/ddl-QDyOSeLc.mjs +251 -0
  29. package/dist/ddl-QDyOSeLc.mjs.map +1 -0
  30. package/dist/ddl.d.mts +2 -2
  31. package/dist/ddl.mjs +2 -2
  32. package/dist/descriptor-meta-CpGygXpI.mjs +140 -0
  33. package/dist/descriptor-meta-CpGygXpI.mjs.map +1 -0
  34. package/dist/{issue-planner-Br0pt1Ea.mjs → issue-planner-DL6g3CmE.mjs} +52 -366
  35. package/dist/issue-planner-DL6g3CmE.mjs.map +1 -0
  36. package/dist/issue-planner.d.mts +8 -11
  37. package/dist/issue-planner.d.mts.map +1 -1
  38. package/dist/issue-planner.mjs +1 -1
  39. package/dist/migration.d.mts +5 -92
  40. package/dist/migration.d.mts.map +1 -1
  41. package/dist/migration.mjs +4 -4
  42. package/dist/{nodes-DZk2JZG3.mjs → nodes-Bbhs2rwj.mjs} +31 -2
  43. package/dist/nodes-Bbhs2rwj.mjs.map +1 -0
  44. package/dist/{nodes-779hmCfL.d.mts → nodes-pLeLgdis.d.mts} +30 -3
  45. package/dist/nodes-pLeLgdis.d.mts.map +1 -0
  46. package/dist/{op-factory-call-D2aAUhmS.mjs → op-factory-call-D_p5vxwt.mjs} +601 -418
  47. package/dist/op-factory-call-D_p5vxwt.mjs.map +1 -0
  48. package/dist/{op-factory-call-DMA86_2D.d.mts → op-factory-call-DmQEc3XV.d.mts} +119 -72
  49. package/dist/op-factory-call-DmQEc3XV.d.mts.map +1 -0
  50. package/dist/op-factory-call.d.mts +2 -2
  51. package/dist/op-factory-call.mjs +2 -2
  52. package/dist/pack.d.mts +36 -15
  53. package/dist/pack.d.mts.map +1 -1
  54. package/dist/pack.mjs +1 -1
  55. package/dist/{planner-CAYPJObw.mjs → planner-Bs_baQax.mjs} +25 -45
  56. package/dist/planner-Bs_baQax.mjs.map +1 -0
  57. package/dist/{planner-ddl-builders-Cw2n2llW.mjs → planner-ddl-builders-B2wOwLqI.mjs} +2 -2
  58. package/dist/planner-ddl-builders-B2wOwLqI.mjs.map +1 -0
  59. package/dist/planner-ddl-builders.d.mts +4 -4
  60. package/dist/planner-ddl-builders.d.mts.map +1 -1
  61. package/dist/planner-ddl-builders.mjs +1 -1
  62. package/dist/{planner-identity-values-BIpa5p2I.mjs → planner-identity-values-CJPha2Sz.mjs} +3 -9
  63. package/dist/planner-identity-values-CJPha2Sz.mjs.map +1 -0
  64. package/dist/planner-identity-values.d.mts +1 -1
  65. package/dist/planner-identity-values.d.mts.map +1 -1
  66. package/dist/planner-identity-values.mjs +1 -1
  67. package/dist/{planner-produced-postgres-migration-NSEhWL0L.mjs → planner-produced-postgres-migration-Cji5vxUf.mjs} +6 -4
  68. package/dist/planner-produced-postgres-migration-Cji5vxUf.mjs.map +1 -0
  69. package/dist/{planner-produced-postgres-migration-B4EDvLdz.d.mts → planner-produced-postgres-migration-QqHa2C2l.d.mts} +5 -6
  70. package/dist/planner-produced-postgres-migration-QqHa2C2l.d.mts.map +1 -0
  71. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  72. package/dist/planner-produced-postgres-migration.mjs +1 -1
  73. package/dist/planner-sql-checks-jqUUGyQR.mjs +152 -0
  74. package/dist/planner-sql-checks-jqUUGyQR.mjs.map +1 -0
  75. package/dist/planner-sql-checks.d.mts +3 -49
  76. package/dist/planner-sql-checks.d.mts.map +1 -1
  77. package/dist/planner-sql-checks.mjs +2 -2
  78. package/dist/{planner-type-resolution-836DExFN.mjs → planner-type-resolution-Bt2f_q-F.mjs} +1 -6
  79. package/dist/planner-type-resolution-Bt2f_q-F.mjs.map +1 -0
  80. package/dist/planner.d.mts +4 -4
  81. package/dist/planner.d.mts.map +1 -1
  82. package/dist/planner.mjs +1 -1
  83. package/dist/{postgres-contract-serializer-DYTyXjPf.mjs → postgres-contract-serializer-k3TAcPMY.mjs} +30 -37
  84. package/dist/postgres-contract-serializer-k3TAcPMY.mjs.map +1 -0
  85. package/dist/postgres-migration-B5jKrXv3.mjs +145 -0
  86. package/dist/postgres-migration-B5jKrXv3.mjs.map +1 -0
  87. package/dist/postgres-migration-Y4YBJqkS.d.mts +181 -0
  88. package/dist/postgres-migration-Y4YBJqkS.d.mts.map +1 -0
  89. package/dist/{postgres-schema-BuxCxbvB.mjs → postgres-schema-COGZ1ark.mjs} +82 -23
  90. package/dist/postgres-schema-COGZ1ark.mjs.map +1 -0
  91. package/dist/{render-ops-BpjstrKQ.mjs → render-ops-BREh1kHe.mjs} +10 -5
  92. package/dist/render-ops-BREh1kHe.mjs.map +1 -0
  93. package/dist/render-ops.d.mts +2 -2
  94. package/dist/render-ops.d.mts.map +1 -1
  95. package/dist/render-ops.mjs +1 -1
  96. package/dist/runtime.d.mts +1 -0
  97. package/dist/runtime.d.mts.map +1 -1
  98. package/dist/runtime.mjs +2 -2
  99. package/dist/table-source-BvFo7gVs.d.mts +15 -0
  100. package/dist/table-source-BvFo7gVs.d.mts.map +1 -0
  101. package/dist/types.d.mts +34 -19
  102. package/dist/types.d.mts.map +1 -1
  103. package/dist/types.mjs +2 -3
  104. package/package.json +17 -18
  105. package/src/contract-free/checks.ts +363 -0
  106. package/src/contract-free/ddl.ts +28 -1
  107. package/src/core/authoring.ts +43 -44
  108. package/src/core/codec-helpers.ts +0 -17
  109. package/src/core/codec-ids.ts +1 -1
  110. package/src/core/codec-type-map.ts +2 -2
  111. package/src/core/codecs.ts +43 -48
  112. package/src/core/ddl/nodes.ts +59 -1
  113. package/src/core/migrations/control-policy.ts +17 -47
  114. package/src/core/migrations/issue-planner.ts +34 -70
  115. package/src/core/migrations/op-factory-call.ts +486 -215
  116. package/src/core/migrations/operations/columns.ts +175 -140
  117. package/src/core/migrations/operations/constraints.ts +79 -108
  118. package/src/core/migrations/operations/data-transform.ts +15 -18
  119. package/src/core/migrations/operations/dependencies.ts +16 -14
  120. package/src/core/migrations/operations/indexes.ts +31 -28
  121. package/src/core/migrations/operations/shared.ts +2 -2
  122. package/src/core/migrations/operations/tables.ts +13 -14
  123. package/src/core/migrations/planner-ddl-builders.ts +3 -4
  124. package/src/core/migrations/planner-identity-values.ts +4 -28
  125. package/src/core/migrations/planner-produced-postgres-migration.ts +15 -7
  126. package/src/core/migrations/planner-recipes.ts +44 -39
  127. package/src/core/migrations/planner-sql-checks.ts +3 -178
  128. package/src/core/migrations/planner-strategies.ts +76 -449
  129. package/src/core/migrations/planner-type-resolution.ts +2 -20
  130. package/src/core/migrations/planner.ts +6 -6
  131. package/src/core/migrations/postgres-migration.ts +287 -7
  132. package/src/core/migrations/render-ops.ts +26 -13
  133. package/src/core/migrations/runner.ts +26 -20
  134. package/src/core/postgres-contract-serializer.ts +37 -54
  135. package/src/core/postgres-enum-type-schema.ts +17 -0
  136. package/src/core/postgres-schema.ts +86 -46
  137. package/src/exports/codecs.ts +2 -2
  138. package/src/exports/contract-free.ts +22 -1
  139. package/src/exports/control.ts +0 -22
  140. package/src/exports/ddl.ts +4 -0
  141. package/src/exports/migration.ts +1 -29
  142. package/src/exports/op-factory-call.ts +0 -4
  143. package/src/exports/planner-sql-checks.ts +0 -7
  144. package/src/exports/types.ts +0 -1
  145. package/dist/codec-ids-B1vOchLE.d.mts.map +0 -1
  146. package/dist/codec-ids-CTikp1if.mjs.map +0 -1
  147. package/dist/codecs-CBpEv4s5.d.mts.map +0 -1
  148. package/dist/data-transform-D25tLeYU.mjs.map +0 -1
  149. package/dist/data-transform-DGOqcLrf.d.mts.map +0 -1
  150. package/dist/ddl-77SyXgFt.mjs +0 -30
  151. package/dist/ddl-77SyXgFt.mjs.map +0 -1
  152. package/dist/descriptor-meta-DKmj-IMN.mjs +0 -14
  153. package/dist/descriptor-meta-DKmj-IMN.mjs.map +0 -1
  154. package/dist/descriptor-meta-runtime-My8_s4cs.mjs +0 -130
  155. package/dist/descriptor-meta-runtime-My8_s4cs.mjs.map +0 -1
  156. package/dist/enum-planning-BCyvlFHk.mjs +0 -0
  157. package/dist/enum-planning-BCyvlFHk.mjs.map +0 -1
  158. package/dist/enum-planning.d.mts +0 -86
  159. package/dist/enum-planning.d.mts.map +0 -1
  160. package/dist/enum-planning.mjs +0 -2
  161. package/dist/issue-planner-Br0pt1Ea.mjs.map +0 -1
  162. package/dist/nodes-779hmCfL.d.mts.map +0 -1
  163. package/dist/nodes-DZk2JZG3.mjs.map +0 -1
  164. package/dist/op-factory-call-D2aAUhmS.mjs.map +0 -1
  165. package/dist/op-factory-call-DMA86_2D.d.mts.map +0 -1
  166. package/dist/planner-CAYPJObw.mjs.map +0 -1
  167. package/dist/planner-ddl-builders-Cw2n2llW.mjs.map +0 -1
  168. package/dist/planner-identity-values-BIpa5p2I.mjs.map +0 -1
  169. package/dist/planner-produced-postgres-migration-B4EDvLdz.d.mts.map +0 -1
  170. package/dist/planner-produced-postgres-migration-NSEhWL0L.mjs.map +0 -1
  171. package/dist/planner-sql-checks-DAdhnI2c.mjs +0 -272
  172. package/dist/planner-sql-checks-DAdhnI2c.mjs.map +0 -1
  173. package/dist/planner-type-resolution-836DExFN.mjs.map +0 -1
  174. package/dist/postgres-contract-serializer-DYTyXjPf.mjs.map +0 -1
  175. package/dist/postgres-enum-type-BVn63a89.d.mts +0 -72
  176. package/dist/postgres-enum-type-BVn63a89.d.mts.map +0 -1
  177. package/dist/postgres-enum-type-DPKqCBem.mjs +0 -62
  178. package/dist/postgres-enum-type-DPKqCBem.mjs.map +0 -1
  179. package/dist/postgres-migration-COore9Mz.mjs +0 -71
  180. package/dist/postgres-migration-COore9Mz.mjs.map +0 -1
  181. package/dist/postgres-migration-DZ_gLUOW.d.mts +0 -72
  182. package/dist/postgres-migration-DZ_gLUOW.d.mts.map +0 -1
  183. package/dist/postgres-schema-BuxCxbvB.mjs.map +0 -1
  184. package/dist/render-ops-BpjstrKQ.mjs.map +0 -1
  185. package/dist/shared-DarONYBZ.d.mts +0 -43
  186. package/dist/shared-DarONYBZ.d.mts.map +0 -1
  187. package/src/core/migrations/enum-planning.ts +0 -213
  188. package/src/core/migrations/operations/enums.ts +0 -114
  189. package/src/core/postgres-enum-type.ts +0 -89
  190. package/src/exports/enum-planning.ts +0 -11
@@ -0,0 +1,363 @@
1
+ import type { SelectAst } from '@prisma-next/sql-relational-core/ast';
2
+ import {
3
+ type CfExpr,
4
+ type CfExprSelectQuery,
5
+ cfExpr,
6
+ cfTable,
7
+ exprSelect,
8
+ } from '@prisma-next/sql-relational-core/contract-free';
9
+ import { PostgresTableSource } from '../core/ast/table-source';
10
+ import { PG_TEXT_CODEC_ID } from '../core/codec-ids';
11
+ import { postgresCreateNamespace } from '../core/postgres-schema';
12
+
13
+ /**
14
+ * `to_regclass($1)` with the qualified table name bound as a text parameter.
15
+ * Thin vocabulary wrapper over the core `cfExpr.fn` helper — the target
16
+ * supplies only the template and the codec'd operand.
17
+ */
18
+ export function toRegclass(qualifiedName: string): CfExpr {
19
+ return cfExpr.fn({
20
+ method: 'to_regclass',
21
+ template: 'to_regclass({{self}})',
22
+ self: cfExpr.param(qualifiedName, PG_TEXT_CODEC_ID),
23
+ returns: { codecId: PG_TEXT_CODEC_ID, nullable: true },
24
+ });
25
+ }
26
+
27
+ export interface TableExistsCheckBuilder {
28
+ tableAbsent(): SelectAst;
29
+ tablePresent(): SelectAst;
30
+ }
31
+
32
+ /**
33
+ * Typed builder for the migration planner's table-existence checks. Produces
34
+ * FROM-less `SELECT to_regclass($1) IS [NOT] NULL AS "result"` ASTs with the
35
+ * qualified table name bound as a text parameter — never inlined into the SQL.
36
+ *
37
+ * `schema` is a namespace coordinate: the framework `__unbound__` sentinel
38
+ * elides the qualifier (search_path decides at runtime); any other id
39
+ * qualifies as `"schema"."table"`.
40
+ */
41
+ export function tableExistsAst(schema: string, table: string): TableExistsCheckBuilder {
42
+ const qualified = postgresCreateNamespace({ id: schema, entries: { table: {} } }).qualifyTable(
43
+ table,
44
+ );
45
+ const regclass = toRegclass(qualified);
46
+ return {
47
+ tableAbsent: () => exprSelect().project('result', regclass.isNull()).build(),
48
+ tablePresent: () => exprSelect().project('result', regclass.isNotNull()).build(),
49
+ };
50
+ }
51
+
52
+ export interface ConstraintExistsCheckBuilder {
53
+ constraintPresent(): SelectAst;
54
+ constraintAbsent(): SelectAst;
55
+ }
56
+
57
+ /**
58
+ * Typed builder for the migration planner's constraint-existence checks.
59
+ * Produces `SELECT [NOT ]EXISTS (SELECT 1 FROM pg_constraint c JOIN
60
+ * pg_namespace n ON n.oid = c.connamespace WHERE c.conname = $1 AND
61
+ * n.nspname = $2 [AND c.conrelid = to_regclass($3)]) AS "result"` with the
62
+ * constraint name, schema name, and qualified table name bound as text
63
+ * parameters.
64
+ *
65
+ * When `table` is omitted the check matches by name + schema across all
66
+ * tables. Pass `table` to scope the check to a single table (prevents false
67
+ * matches on identically-named constraints in different tables). `schema`
68
+ * is a namespace coordinate: the `__unbound__` sentinel compares `nspname`
69
+ * against `current_schema()` instead of a bound parameter.
70
+ */
71
+ export function constraintExistsAst(options: {
72
+ readonly constraintName: string;
73
+ readonly schema: string;
74
+ readonly table?: string;
75
+ }): ConstraintExistsCheckBuilder {
76
+ const namespace = postgresCreateNamespace({ id: options.schema, entries: { table: {} } });
77
+ const conditions = [
78
+ cfExpr.columnRef('c', 'conname').eqParam(options.constraintName, PG_TEXT_CODEC_ID),
79
+ cfExpr.columnRef('n', 'nspname').eqExpr(namespace.schemaFilterExpression()),
80
+ ];
81
+ if (options.table !== undefined) {
82
+ conditions.push(
83
+ cfExpr.columnRef('c', 'conrelid').eqExpr(toRegclass(namespace.qualifyTable(options.table))),
84
+ );
85
+ }
86
+ const inner = (): CfExprSelectQuery =>
87
+ exprSelect()
88
+ .from(cfTable('pg_constraint', 'c'))
89
+ .join(
90
+ cfTable('pg_namespace', 'n'),
91
+ cfExpr.columnRef('n', 'oid').eqExpr(cfExpr.columnRef('c', 'connamespace')),
92
+ )
93
+ .project('one', cfExpr.lit(1))
94
+ .where(cfExpr.allOf(conditions));
95
+ return {
96
+ constraintPresent: () => exprSelect().project('result', cfExpr.exists(inner())).build(),
97
+ constraintAbsent: () => exprSelect().project('result', cfExpr.notExists(inner())).build(),
98
+ };
99
+ }
100
+
101
+ function checkNamespace(schema: string) {
102
+ return postgresCreateNamespace({ id: schema, entries: { table: {} } });
103
+ }
104
+
105
+ function informationSchemaColumns(): PostgresTableSource {
106
+ return new PostgresTableSource({ schema: 'information_schema', name: 'columns' });
107
+ }
108
+
109
+ function infoSchemaColumnConditions(options: {
110
+ readonly schema: string;
111
+ readonly table: string;
112
+ readonly column: string;
113
+ }): CfExpr[] {
114
+ return [
115
+ cfExpr
116
+ .identifierRef('table_schema')
117
+ .eqExpr(checkNamespace(options.schema).schemaFilterExpression()),
118
+ cfExpr.identifierRef('table_name').eqParam(options.table, PG_TEXT_CODEC_ID),
119
+ cfExpr.identifierRef('column_name').eqParam(options.column, PG_TEXT_CODEC_ID),
120
+ ];
121
+ }
122
+
123
+ function infoSchemaColumnQuery(conditions: ReadonlyArray<CfExpr>): CfExprSelectQuery {
124
+ return exprSelect()
125
+ .from(informationSchemaColumns())
126
+ .project('one', cfExpr.lit(1))
127
+ .where(cfExpr.allOf(conditions));
128
+ }
129
+
130
+ export interface ColumnExistsCheckBuilder {
131
+ columnPresent(): SelectAst;
132
+ columnAbsent(): SelectAst;
133
+ }
134
+
135
+ /**
136
+ * Typed builder for column-existence checks over
137
+ * `information_schema.columns`, with schema, table, and column names bound
138
+ * as text parameters.
139
+ */
140
+ export function columnExistsAst(options: {
141
+ readonly schema: string;
142
+ readonly table: string;
143
+ readonly column: string;
144
+ }): ColumnExistsCheckBuilder {
145
+ const inner = () => infoSchemaColumnQuery(infoSchemaColumnConditions(options));
146
+ return {
147
+ columnPresent: () => exprSelect().project('result', cfExpr.exists(inner())).build(),
148
+ columnAbsent: () => exprSelect().project('result', cfExpr.notExists(inner())).build(),
149
+ };
150
+ }
151
+
152
+ /**
153
+ * Typed nullability check: EXISTS over `information_schema.columns` with
154
+ * `is_nullable` compared against the bound `'YES'` / `'NO'` marker.
155
+ */
156
+ export function columnNullabilityAst(options: {
157
+ readonly schema: string;
158
+ readonly table: string;
159
+ readonly column: string;
160
+ readonly nullable: boolean;
161
+ }): SelectAst {
162
+ const conditions = [
163
+ ...infoSchemaColumnConditions(options),
164
+ cfExpr.identifierRef('is_nullable').eqParam(options.nullable ? 'YES' : 'NO', PG_TEXT_CODEC_ID),
165
+ ];
166
+ return exprSelect()
167
+ .project('result', cfExpr.exists(infoSchemaColumnQuery(conditions)))
168
+ .build();
169
+ }
170
+
171
+ export interface ColumnDefaultCheckBuilder {
172
+ defaultPresent(): SelectAst;
173
+ defaultAbsent(): SelectAst;
174
+ noDefault(): SelectAst;
175
+ }
176
+
177
+ /**
178
+ * Typed default-presence checks over `information_schema.columns`.
179
+ * `defaultPresent` / `defaultAbsent` assert the column row exists with a
180
+ * non-null / null `column_default`; `noDefault` is the NOT EXISTS variant
181
+ * (also true when the column row is missing entirely).
182
+ */
183
+ export function columnDefaultAst(options: {
184
+ readonly schema: string;
185
+ readonly table: string;
186
+ readonly column: string;
187
+ }): ColumnDefaultCheckBuilder {
188
+ const withDefault = () =>
189
+ infoSchemaColumnQuery([
190
+ ...infoSchemaColumnConditions(options),
191
+ cfExpr.identifierRef('column_default').isNotNull(),
192
+ ]);
193
+ const withoutDefault = () =>
194
+ infoSchemaColumnQuery([
195
+ ...infoSchemaColumnConditions(options),
196
+ cfExpr.identifierRef('column_default').isNull(),
197
+ ]);
198
+ return {
199
+ defaultPresent: () => exprSelect().project('result', cfExpr.exists(withDefault())).build(),
200
+ defaultAbsent: () => exprSelect().project('result', cfExpr.exists(withoutDefault())).build(),
201
+ noDefault: () => exprSelect().project('result', cfExpr.notExists(withDefault())).build(),
202
+ };
203
+ }
204
+
205
+ /**
206
+ * Typed column-type check: EXISTS over `pg_attribute` joined to `pg_class`
207
+ * and `pg_namespace`, comparing `format_type(a.atttypid, a.atttypmod)`
208
+ * against the bound expected display type and excluding dropped columns.
209
+ */
210
+ export function columnTypeAst(options: {
211
+ readonly schema: string;
212
+ readonly table: string;
213
+ readonly column: string;
214
+ readonly expectedType: string;
215
+ }): SelectAst {
216
+ const formatType = cfExpr.fn({
217
+ method: 'format_type',
218
+ template: 'format_type({{self}}, {{arg0}})',
219
+ self: cfExpr.columnRef('a', 'atttypid'),
220
+ args: [cfExpr.columnRef('a', 'atttypmod')],
221
+ returns: { codecId: PG_TEXT_CODEC_ID, nullable: false },
222
+ });
223
+ const inner = exprSelect()
224
+ .from(cfTable('pg_attribute', 'a'))
225
+ .join(
226
+ cfTable('pg_class', 'c'),
227
+ cfExpr.columnRef('c', 'oid').eqExpr(cfExpr.columnRef('a', 'attrelid')),
228
+ )
229
+ .join(
230
+ cfTable('pg_namespace', 'n'),
231
+ cfExpr.columnRef('n', 'oid').eqExpr(cfExpr.columnRef('c', 'relnamespace')),
232
+ )
233
+ .project('one', cfExpr.lit(1))
234
+ .where(
235
+ cfExpr.allOf([
236
+ cfExpr
237
+ .columnRef('n', 'nspname')
238
+ .eqExpr(checkNamespace(options.schema).schemaFilterExpression()),
239
+ cfExpr.columnRef('c', 'relname').eqParam(options.table, PG_TEXT_CODEC_ID),
240
+ cfExpr.columnRef('a', 'attname').eqParam(options.column, PG_TEXT_CODEC_ID),
241
+ formatType.eqParam(options.expectedType, PG_TEXT_CODEC_ID),
242
+ cfExpr.columnRef('a', 'attisdropped').not(),
243
+ ]),
244
+ );
245
+ return exprSelect().project('result', cfExpr.exists(inner)).build();
246
+ }
247
+
248
+ export interface TablePrimaryKeyCheckBuilder {
249
+ pkPresent(): SelectAst;
250
+ pkAbsent(): SelectAst;
251
+ }
252
+
253
+ /**
254
+ * Typed primary-key existence check over `pg_index` joined to `pg_class`
255
+ * and `pg_namespace`, with a LEFT JOIN on the index relation so an
256
+ * optional `constraintName` can scope the match to a named constraint.
257
+ */
258
+ export function tablePrimaryKeyAst(options: {
259
+ readonly schema: string;
260
+ readonly table: string;
261
+ readonly constraintName?: string;
262
+ }): TablePrimaryKeyCheckBuilder {
263
+ const conditions = [
264
+ cfExpr
265
+ .columnRef('n', 'nspname')
266
+ .eqExpr(checkNamespace(options.schema).schemaFilterExpression()),
267
+ cfExpr.columnRef('c', 'relname').eqParam(options.table, PG_TEXT_CODEC_ID),
268
+ cfExpr.columnRef('i', 'indisprimary'),
269
+ ];
270
+ if (options.constraintName !== undefined) {
271
+ conditions.push(
272
+ cfExpr.columnRef('c2', 'relname').eqParam(options.constraintName, PG_TEXT_CODEC_ID),
273
+ );
274
+ }
275
+ const inner = () =>
276
+ exprSelect()
277
+ .from(cfTable('pg_index', 'i'))
278
+ .join(
279
+ cfTable('pg_class', 'c'),
280
+ cfExpr.columnRef('c', 'oid').eqExpr(cfExpr.columnRef('i', 'indrelid')),
281
+ )
282
+ .join(
283
+ cfTable('pg_namespace', 'n'),
284
+ cfExpr.columnRef('n', 'oid').eqExpr(cfExpr.columnRef('c', 'relnamespace')),
285
+ )
286
+ .leftJoin(
287
+ cfTable('pg_class', 'c2'),
288
+ cfExpr.columnRef('c2', 'oid').eqExpr(cfExpr.columnRef('i', 'indexrelid')),
289
+ )
290
+ .project('one', cfExpr.lit(1))
291
+ .where(cfExpr.allOf(conditions));
292
+ return {
293
+ pkPresent: () => exprSelect().project('result', cfExpr.exists(inner())).build(),
294
+ pkAbsent: () => exprSelect().project('result', cfExpr.notExists(inner())).build(),
295
+ };
296
+ }
297
+
298
+ /**
299
+ * Typed emptiness check: NOT EXISTS over the user table itself with
300
+ * `LIMIT 1`. The table is addressed through the namespace's polymorphic
301
+ * `tableSource` (qualified for named schemas, bare for the unbound slot).
302
+ */
303
+ export function tableIsEmptyAst(schema: string, table: string): SelectAst {
304
+ const inner = exprSelect()
305
+ .from(checkNamespace(schema).tableSource(table))
306
+ .project('one', cfExpr.lit(1))
307
+ .limit(1);
308
+ return exprSelect().project('result', cfExpr.notExists(inner)).build();
309
+ }
310
+
311
+ /**
312
+ * Typed no-NULL-values data check used by `SET NOT NULL` prechecks:
313
+ * NOT EXISTS over the user table where the column IS NULL.
314
+ */
315
+ export function noNullValuesAst(options: {
316
+ readonly schema: string;
317
+ readonly table: string;
318
+ readonly column: string;
319
+ }): SelectAst {
320
+ const inner = exprSelect()
321
+ .from(checkNamespace(options.schema).tableSource(options.table))
322
+ .project('one', cfExpr.lit(1))
323
+ .where(cfExpr.identifierRef(options.column).isNull());
324
+ return exprSelect().project('result', cfExpr.notExists(inner)).build();
325
+ }
326
+
327
+ export interface ExtensionExistsCheckBuilder {
328
+ extensionPresent(): SelectAst;
329
+ extensionAbsent(): SelectAst;
330
+ }
331
+
332
+ /**
333
+ * Typed extension existence check over `pg_extension`, with the extension
334
+ * name bound as a text parameter.
335
+ */
336
+ export function extensionExistsAst(extensionName: string): ExtensionExistsCheckBuilder {
337
+ const inner = () =>
338
+ exprSelect()
339
+ .from(cfTable('pg_extension'))
340
+ .project('one', cfExpr.lit(1))
341
+ .where(cfExpr.identifierRef('extname').eqParam(extensionName, PG_TEXT_CODEC_ID));
342
+ return {
343
+ extensionPresent: () => exprSelect().project('result', cfExpr.exists(inner())).build(),
344
+ extensionAbsent: () => exprSelect().project('result', cfExpr.notExists(inner())).build(),
345
+ };
346
+ }
347
+
348
+ export interface IndexExistsCheckBuilder {
349
+ indexPresent(): SelectAst;
350
+ indexAbsent(): SelectAst;
351
+ }
352
+
353
+ /**
354
+ * Typed index existence check riding the same `to_regclass` vocabulary as
355
+ * `tableExistsAst`, with the qualified index name bound as a text parameter.
356
+ */
357
+ export function indexExistsAst(schema: string, indexName: string): IndexExistsCheckBuilder {
358
+ const regclass = toRegclass(checkNamespace(schema).qualifyTable(indexName));
359
+ return {
360
+ indexPresent: () => exprSelect().project('result', regclass.isNotNull()).build(),
361
+ indexAbsent: () => exprSelect().project('result', regclass.isNull()).build(),
362
+ };
363
+ }
@@ -1,5 +1,11 @@
1
1
  import type { DdlColumn, DdlTableConstraint } from '@prisma-next/sql-relational-core/ast';
2
- import { PostgresCreateSchema, PostgresCreateTable } from '../core/ddl/nodes';
2
+ import {
3
+ AddColumnAction,
4
+ type AnyAlterTableAction,
5
+ PostgresAlterTable,
6
+ PostgresCreateSchema,
7
+ PostgresCreateTable,
8
+ } from '../core/ddl/nodes';
3
9
 
4
10
  /**
5
11
  * Build a Postgres `CREATE TABLE` query node.
@@ -35,3 +41,24 @@ export function createSchema(options: {
35
41
  }): PostgresCreateSchema {
36
42
  return new PostgresCreateSchema(options);
37
43
  }
44
+
45
+ /**
46
+ * Build an `ADD COLUMN` action for use inside {@link alterTable}.
47
+ * The column is a structured `DdlColumn` so codec-encoded defaults flow
48
+ * through the adapter's `pgRenderDdlColumn` → `pgRenderDdlColumnDefault` path.
49
+ */
50
+ export function addColumnAction(column: DdlColumn): AddColumnAction {
51
+ return new AddColumnAction(column);
52
+ }
53
+
54
+ /**
55
+ * Build a Postgres `ALTER TABLE` query node carrying one or more actions.
56
+ * See {@link addColumnAction} for building actions.
57
+ */
58
+ export function alterTable(options: {
59
+ readonly table: string;
60
+ readonly schema?: string;
61
+ readonly actions: readonly AnyAlterTableAction[];
62
+ }): PostgresAlterTable {
63
+ return new PostgresAlterTable(options);
64
+ }
@@ -4,53 +4,10 @@ import type {
4
4
  AuthoringFieldNamespace,
5
5
  AuthoringTypeNamespace,
6
6
  } from '@prisma-next/framework-components/authoring';
7
- import type { PostgresEnumStorageEntry } from '@prisma-next/sql-contract/types';
8
- import { PostgresEnumTypeSchema } from '@prisma-next/sql-contract/validators';
9
- import { PostgresEnumType, type PostgresEnumTypeInput } from './postgres-enum-type';
10
7
 
11
8
  export const postgresAuthoringTypes = {} as const satisfies AuthoringTypeNamespace;
12
9
 
13
- /**
14
- * Entity type contributions surface as top-level helpers on the
15
- * composed-helpers shape (e.g. `helpers.enum({...})`), flattened
16
- * alongside the built-in `model` / `rel` helpers. Pack contributions
17
- * still ship via the contribution data structure
18
- * `authoring.entityTypes.<name>`; the composed-helpers template
19
- * performs the rename in the type system.
20
- *
21
- * `enum` is the first real consumer of the entities-namespace mechanism:
22
- * the factory constructs a `PostgresEnumType` IR-class instance from
23
- * the user-supplied input. Both authoring runtimes (TS DSL and PSL)
24
- * dispatch through this single contribution — PSL `enum Status { … }`
25
- * declarations are lowered by the interpreter into a factory call
26
- * with the parsed name + value list; TS DSL `helpers.enum({...})`
27
- * resolves through the same path. Removing this contribution makes
28
- * both surfaces fail with a "no entity helper named `enum`" type
29
- * error at the contract-definition site.
30
- */
31
- /**
32
- * The factory constructs a `PostgresEnumType` instance natively — the
33
- * `SqlStorage.types` slot accepts polymorphic IR (the framework
34
- * `StorageType` alphabet), so no cast is needed at the contribution
35
- * surface. The declared return type is the structural
36
- * `PostgresEnumStorageEntry` so the inferred contract type stays
37
- * portable (it names a type exported from
38
- * `@prisma-next/sql-contract/types`, a public surface every consumer
39
- * already imports). Sharpening the inferred contract type to surface
40
- * enum-specific narrowing through `EntityHelperFunction` is a
41
- * separable refinement and lives outside this PR.
42
- */
43
- export const postgresAuthoringEntityTypes = {
44
- enum: {
45
- kind: 'entity',
46
- discriminator: 'postgres-enum',
47
- validatorSchema: PostgresEnumTypeSchema,
48
- output: {
49
- factory: (input: PostgresEnumTypeInput): PostgresEnumStorageEntry =>
50
- new PostgresEnumType(input),
51
- },
52
- },
53
- } as const satisfies AuthoringEntityTypeNamespace;
10
+ export const postgresAuthoringEntityTypes = {} as const satisfies AuthoringEntityTypeNamespace;
54
11
 
55
12
  /**
56
13
  * Field presets contributed by the Postgres target pack.
@@ -59,6 +16,11 @@ export const postgresAuthoringEntityTypes = {
59
16
  * (see `createPostgresPslScalarTypeDescriptors`), so that authoring a field
60
17
  * via the TS callback surface (e.g. `field.int()`) and via the PSL scalar
61
18
  * surface (e.g. `Int`) lowers to byte-identical contracts.
19
+ *
20
+ * The `uuidNative` / `id.uuidv4Native` / `id.uuidv7Native` presets use the
21
+ * native Postgres `uuid` type (codecId `pg/uuid@1`). For cross-target
22
+ * portability use `uuidString` / `id.uuidv4String` / `id.uuidv7String` from
23
+ * the family pack instead.
62
24
  */
63
25
  export const postgresAuthoringFieldPresets = {
64
26
  text: {
@@ -128,4 +90,41 @@ export const postgresAuthoringFieldPresets = {
128
90
  codecId: 'pg/timestamptz@1',
129
91
  nativeType: 'timestamptz',
130
92
  }),
93
+ uuidNative: {
94
+ kind: 'fieldPreset',
95
+ output: {
96
+ codecId: 'pg/uuid@1',
97
+ nativeType: 'uuid',
98
+ },
99
+ },
100
+ id: {
101
+ uuidv4Native: {
102
+ kind: 'fieldPreset',
103
+ output: {
104
+ codecId: 'pg/uuid@1',
105
+ nativeType: 'uuid',
106
+ executionDefaults: {
107
+ onCreate: {
108
+ kind: 'generator',
109
+ id: 'uuidv4',
110
+ },
111
+ },
112
+ id: true,
113
+ },
114
+ },
115
+ uuidv7Native: {
116
+ kind: 'fieldPreset',
117
+ output: {
118
+ codecId: 'pg/uuid@1',
119
+ nativeType: 'uuid',
120
+ executionDefaults: {
121
+ onCreate: {
122
+ kind: 'generator',
123
+ id: 'uuidv7',
124
+ },
125
+ },
126
+ id: true,
127
+ },
128
+ },
129
+ },
131
130
  } as const satisfies AuthoringFieldNamespace;
@@ -109,23 +109,6 @@ export const pgIntervalDecode = (wire: string | Record<string, unknown>): string
109
109
  return JSON.stringify(wire);
110
110
  };
111
111
 
112
- export const pgEnumRenderOutputType = (typeParams: {
113
- readonly values?: readonly unknown[];
114
- }): string => {
115
- const values = typeParams.values;
116
- if (!Array.isArray(values)) {
117
- throw new Error(
118
- `renderOutputType: expected array "values" in typeParams for enum, got ${typeof values}`,
119
- );
120
- }
121
- if (!values.every((v): v is string => typeof v === 'string')) {
122
- throw new Error(`renderOutputType: expected string[] "values" in typeParams for enum`);
123
- }
124
- return values
125
- .map((value) => `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`)
126
- .join(' | ');
127
- };
128
-
129
112
  export const pgJsonEncode = (value: string | JsonValue): string => JSON.stringify(value);
130
113
  export const pgJsonDecode = (wire: string | JsonValue): JsonValue =>
131
114
  typeof wire === 'string' ? JSON.parse(wire) : wire;
@@ -8,7 +8,6 @@ export {
8
8
  } from '@prisma-next/sql-relational-core/ast';
9
9
  export const PG_TEXT_CODEC_ID = 'pg/text@1' as const;
10
10
  export const PG_TEXT_ARRAY_CODEC_ID = 'pg/text-array@1' as const;
11
- export const PG_ENUM_CODEC_ID = 'pg/enum@1' as const;
12
11
  export const PG_CHAR_CODEC_ID = 'pg/char@1' as const;
13
12
  export const PG_VARCHAR_CODEC_ID = 'pg/varchar@1' as const;
14
13
  export const PG_INT_CODEC_ID = 'pg/int@1' as const;
@@ -30,3 +29,4 @@ export const PG_INTERVAL_CODEC_ID = 'pg/interval@1' as const;
30
29
  export const PG_JSON_CODEC_ID = 'pg/json@1' as const;
31
30
  export const PG_JSONB_CODEC_ID = 'pg/jsonb@1' as const;
32
31
  export const PG_BYTEA_CODEC_ID = 'pg/bytea@1' as const;
32
+ export const PG_UUID_CODEC_ID = 'pg/uuid@1' as const;
@@ -21,7 +21,6 @@ import {
21
21
  pgBoolDescriptor,
22
22
  pgByteaDescriptor,
23
23
  pgCharDescriptor,
24
- pgEnumDescriptor,
25
24
  pgFloat4Descriptor,
26
25
  pgFloat8Descriptor,
27
26
  pgFloatDescriptor,
@@ -38,6 +37,7 @@ import {
38
37
  pgTimestampDescriptor,
39
38
  pgTimestamptzDescriptor,
40
39
  pgTimetzDescriptor,
40
+ pgUuidDescriptor,
41
41
  pgVarbitDescriptor,
42
42
  pgVarcharDescriptor,
43
43
  } from './codecs';
@@ -68,8 +68,8 @@ export const codecDescriptorMap = {
68
68
  bit: pgBitDescriptor,
69
69
  'bit varying': pgVarbitDescriptor,
70
70
  bytea: pgByteaDescriptor,
71
+ uuid: pgUuidDescriptor,
71
72
  interval: pgIntervalDescriptor,
72
- enum: pgEnumDescriptor,
73
73
  json: pgJsonDescriptor,
74
74
  jsonb: pgJsonbDescriptor,
75
75
  } as const;