@prisma-next/target-postgres 0.12.0-dev.62 → 0.12.0-dev.63

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 (90) hide show
  1. package/dist/contract-free.d.mts +7 -2
  2. package/dist/contract-free.d.mts.map +1 -1
  3. package/dist/contract-free.mjs +2 -24
  4. package/dist/contract-free.mjs.map +1 -1
  5. package/dist/control.d.mts.map +1 -1
  6. package/dist/control.mjs +5 -5
  7. package/dist/control.mjs.map +1 -1
  8. package/dist/ddl-77SyXgFt.mjs +30 -0
  9. package/dist/ddl-77SyXgFt.mjs.map +1 -0
  10. package/dist/ddl.d.mts +1 -1
  11. package/dist/ddl.mjs +1 -1
  12. package/dist/{issue-planner-hxJaxYHv.mjs → issue-planner-Wd6zcYtT.mjs} +15 -5
  13. package/dist/issue-planner-Wd6zcYtT.mjs.map +1 -0
  14. package/dist/issue-planner.d.mts +1 -1
  15. package/dist/issue-planner.d.mts.map +1 -1
  16. package/dist/issue-planner.mjs +1 -1
  17. package/dist/migration.d.mts +4 -7
  18. package/dist/migration.d.mts.map +1 -1
  19. package/dist/migration.mjs +4 -3
  20. package/dist/migration.mjs.map +1 -1
  21. package/dist/{nodes-Dq0ALHOk.d.mts → nodes-779hmCfL.d.mts} +4 -2
  22. package/dist/nodes-779hmCfL.d.mts.map +1 -0
  23. package/dist/{nodes-Dm1XR7UL.mjs → nodes-DZk2JZG3.mjs} +6 -1
  24. package/dist/nodes-DZk2JZG3.mjs.map +1 -0
  25. package/dist/{op-factory-call-D47zfLOp.d.mts → op-factory-call-Bj1jPDpD.d.mts} +12 -11
  26. package/dist/op-factory-call-Bj1jPDpD.d.mts.map +1 -0
  27. package/dist/op-factory-call-q9Tpw6wc.mjs +1217 -0
  28. package/dist/op-factory-call-q9Tpw6wc.mjs.map +1 -0
  29. package/dist/op-factory-call.d.mts +1 -1
  30. package/dist/op-factory-call.mjs +1 -1
  31. package/dist/{planner-C5tapy2_.mjs → planner-BI6l-3ft.mjs} +11 -7
  32. package/dist/planner-BI6l-3ft.mjs.map +1 -0
  33. package/dist/planner-ddl-builders-DDyptTt5.mjs.map +1 -1
  34. package/dist/planner-ddl-builders.d.mts +3 -3
  35. package/dist/planner-ddl-builders.d.mts.map +1 -1
  36. package/dist/{planner-produced-postgres-migration-BCy50btZ.mjs → planner-produced-postgres-migration-7aaRoTZN.mjs} +8 -6
  37. package/dist/planner-produced-postgres-migration-7aaRoTZN.mjs.map +1 -0
  38. package/dist/{planner-produced-postgres-migration-CpsGhewL.d.mts → planner-produced-postgres-migration-B11fjNTx.d.mts} +4 -3
  39. package/dist/planner-produced-postgres-migration-B11fjNTx.d.mts.map +1 -0
  40. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  41. package/dist/planner-produced-postgres-migration.mjs +1 -1
  42. package/dist/planner.d.mts +5 -2
  43. package/dist/planner.d.mts.map +1 -1
  44. package/dist/planner.mjs +1 -1
  45. package/dist/{postgres-migration--iqDr7rB.d.mts → postgres-migration-B6rKNT4N.d.mts} +23 -1
  46. package/dist/postgres-migration-B6rKNT4N.d.mts.map +1 -0
  47. package/dist/{postgres-migration-DcymFlMG.mjs → postgres-migration-C_wPoWz_.mjs} +21 -1
  48. package/dist/postgres-migration-C_wPoWz_.mjs.map +1 -0
  49. package/dist/{render-ops-Wb6SCGmj.mjs → render-ops-BpjstrKQ.mjs} +4 -3
  50. package/dist/{render-ops-Wb6SCGmj.mjs.map → render-ops-BpjstrKQ.mjs.map} +1 -1
  51. package/dist/render-ops.d.mts +2 -1
  52. package/dist/render-ops.d.mts.map +1 -1
  53. package/dist/render-ops.mjs +1 -1
  54. package/dist/{render-typescript-BShM6uyS.mjs → render-typescript-KMgosran.mjs} +5 -2
  55. package/dist/render-typescript-KMgosran.mjs.map +1 -0
  56. package/dist/render-typescript.mjs +1 -1
  57. package/dist/{shared-C3gXQyxj.d.mts → shared-C6QxSU0W.d.mts} +4 -4
  58. package/dist/{shared-C3gXQyxj.d.mts.map → shared-C6QxSU0W.d.mts.map} +1 -1
  59. package/package.json +17 -17
  60. package/src/contract-free/ddl.ts +6 -1
  61. package/src/core/ddl/nodes.ts +14 -1
  62. package/src/core/migrations/issue-planner.ts +27 -10
  63. package/src/core/migrations/op-factory-call.ts +197 -42
  64. package/src/core/migrations/operations/constraints.ts +1 -9
  65. package/src/core/migrations/operations/dependencies.ts +0 -17
  66. package/src/core/migrations/operations/shared.ts +3 -3
  67. package/src/core/migrations/operations/tables.ts +1 -39
  68. package/src/core/migrations/planner-ddl-builders.ts +4 -4
  69. package/src/core/migrations/planner-produced-postgres-migration.ts +11 -6
  70. package/src/core/migrations/planner-strategies.ts +1 -0
  71. package/src/core/migrations/planner.ts +11 -2
  72. package/src/core/migrations/postgres-migration.ts +41 -0
  73. package/src/core/migrations/render-ops.ts +7 -2
  74. package/src/core/migrations/render-typescript.ts +5 -1
  75. package/src/exports/control.ts +6 -8
  76. package/src/exports/migration.ts +9 -2
  77. package/dist/issue-planner-hxJaxYHv.mjs.map +0 -1
  78. package/dist/nodes-Dm1XR7UL.mjs.map +0 -1
  79. package/dist/nodes-Dq0ALHOk.d.mts.map +0 -1
  80. package/dist/op-factory-call-D47zfLOp.d.mts.map +0 -1
  81. package/dist/op-factory-call-G9G02kxC.mjs +0 -625
  82. package/dist/op-factory-call-G9G02kxC.mjs.map +0 -1
  83. package/dist/planner-C5tapy2_.mjs.map +0 -1
  84. package/dist/planner-produced-postgres-migration-BCy50btZ.mjs.map +0 -1
  85. package/dist/planner-produced-postgres-migration-CpsGhewL.d.mts.map +0 -1
  86. package/dist/postgres-migration--iqDr7rB.d.mts.map +0 -1
  87. package/dist/postgres-migration-DcymFlMG.mjs.map +0 -1
  88. package/dist/render-typescript-BShM6uyS.mjs.map +0 -1
  89. package/dist/tables-OLIoVjxf.mjs +0 -516
  90. package/dist/tables-OLIoVjxf.mjs.map +0 -1
@@ -0,0 +1,1217 @@
1
+ import { i as quoteIdentifier, n as escapeLiteral, r as qualifyName } from "./sql-utils-DcfMz4MQ.mjs";
2
+ import { n as createTable, t as createSchema } from "./ddl-77SyXgFt.mjs";
3
+ import { a as columnNullabilityCheck, c as qualifyTableName, d as toRegclassLiteral, n as columnDefaultExistsCheck, o as columnTypeCheck, r as columnExistsCheck, s as constraintExistsCheck } from "./planner-sql-checks-DAdhnI2c.mjs";
4
+ import { FunctionColumnDefault, LiteralColumnDefault } from "@prisma-next/sql-relational-core/ast";
5
+ import { ifDefined } from "@prisma-next/utils/defined";
6
+ import { UNBOUND_NAMESPACE_ID } from "@prisma-next/framework-components/ir";
7
+ import { blindCast } from "@prisma-next/utils/casts";
8
+ import { errorUnfilledPlaceholder } from "@prisma-next/errors/migration";
9
+ import { TsExpression, jsonToTsSource } from "@prisma-next/ts-render";
10
+ import { REFERENTIAL_ACTION_SQL } from "@prisma-next/sql-contract/referential-action-sql";
11
+ //#region src/core/migrations/operations/shared.ts
12
+ function step(description, sql) {
13
+ return {
14
+ description,
15
+ sql
16
+ };
17
+ }
18
+ function targetDetails(objectType, name, schema, table) {
19
+ return {
20
+ id: "postgres",
21
+ details: {
22
+ schema,
23
+ objectType,
24
+ name,
25
+ ...ifDefined("table", table)
26
+ }
27
+ };
28
+ }
29
+ //#endregion
30
+ //#region src/core/migrations/operations/columns.ts
31
+ function addColumn(schemaName, tableName, column) {
32
+ const addSql = [
33
+ `ALTER TABLE ${qualifyTableName(schemaName, tableName)}`,
34
+ `ADD COLUMN ${quoteIdentifier(column.name)} ${column.typeSql}`,
35
+ column.defaultSql,
36
+ column.nullable ? "" : "NOT NULL"
37
+ ].filter(Boolean).join(" ");
38
+ return {
39
+ id: `column.${tableName}.${column.name}`,
40
+ label: `Add column "${column.name}" to "${tableName}"`,
41
+ operationClass: "additive",
42
+ target: targetDetails("column", column.name, schemaName, tableName),
43
+ precheck: [step(`ensure column "${column.name}" is missing`, columnExistsCheck({
44
+ schema: schemaName,
45
+ table: tableName,
46
+ column: column.name,
47
+ exists: false
48
+ }))],
49
+ execute: [step(`add column "${column.name}"`, addSql)],
50
+ postcheck: [step(`verify column "${column.name}" exists`, columnExistsCheck({
51
+ schema: schemaName,
52
+ table: tableName,
53
+ column: column.name
54
+ }))]
55
+ };
56
+ }
57
+ function dropColumn(schemaName, tableName, columnName) {
58
+ const qualified = qualifyTableName(schemaName, tableName);
59
+ return {
60
+ id: `dropColumn.${tableName}.${columnName}`,
61
+ label: `Drop column "${columnName}" from "${tableName}"`,
62
+ operationClass: "destructive",
63
+ target: targetDetails("column", columnName, schemaName, tableName),
64
+ precheck: [step(`ensure column "${columnName}" exists`, columnExistsCheck({
65
+ schema: schemaName,
66
+ table: tableName,
67
+ column: columnName
68
+ }))],
69
+ execute: [step(`drop column "${columnName}"`, `ALTER TABLE ${qualified} DROP COLUMN ${quoteIdentifier(columnName)}`)],
70
+ postcheck: [step(`verify column "${columnName}" does not exist`, columnExistsCheck({
71
+ schema: schemaName,
72
+ table: tableName,
73
+ column: columnName,
74
+ exists: false
75
+ }))]
76
+ };
77
+ }
78
+ /**
79
+ * `qualifiedTargetType` is the new column type as it appears in the
80
+ * `ALTER COLUMN TYPE` clause (schema-qualified for user-defined types, raw
81
+ * native name for built-ins). `formatTypeExpected` is the unqualified
82
+ * `format_type` form used in the postcheck. `rawTargetTypeForLabel` is the
83
+ * string appearing in the human-readable label (typically `toType` when
84
+ * explicit, else the column's native type).
85
+ */
86
+ function alterColumnType(schemaName, tableName, columnName, options) {
87
+ const qualified = qualifyTableName(schemaName, tableName);
88
+ const usingClause = options.using ? ` USING ${options.using}` : ` USING ${quoteIdentifier(columnName)}::${options.qualifiedTargetType}`;
89
+ return {
90
+ id: `alterType.${tableName}.${columnName}`,
91
+ label: `Alter type of "${tableName}"."${columnName}" to ${options.rawTargetTypeForLabel}`,
92
+ operationClass: "destructive",
93
+ target: targetDetails("column", columnName, schemaName, tableName),
94
+ precheck: [step(`ensure column "${columnName}" exists`, columnExistsCheck({
95
+ schema: schemaName,
96
+ table: tableName,
97
+ column: columnName
98
+ }))],
99
+ execute: [step(`alter type of "${columnName}"`, `ALTER TABLE ${qualified} ALTER COLUMN ${quoteIdentifier(columnName)} TYPE ${options.qualifiedTargetType}${usingClause}`)],
100
+ postcheck: [step(`verify column "${columnName}" has type "${options.formatTypeExpected}"`, columnTypeCheck({
101
+ schema: schemaName,
102
+ table: tableName,
103
+ column: columnName,
104
+ expectedType: options.formatTypeExpected
105
+ }))],
106
+ meta: { warning: "TABLE_REWRITE" }
107
+ };
108
+ }
109
+ function setNotNull(schemaName, tableName, columnName) {
110
+ const qualified = qualifyTableName(schemaName, tableName);
111
+ return {
112
+ id: `alterNullability.setNotNull.${tableName}.${columnName}`,
113
+ label: `Set NOT NULL on "${tableName}"."${columnName}"`,
114
+ operationClass: "destructive",
115
+ target: targetDetails("column", columnName, schemaName, tableName),
116
+ precheck: [step(`ensure column "${columnName}" exists`, columnExistsCheck({
117
+ schema: schemaName,
118
+ table: tableName,
119
+ column: columnName
120
+ })), step(`ensure no NULL values in "${columnName}"`, `SELECT NOT EXISTS (SELECT 1 FROM ${qualified} WHERE ${quoteIdentifier(columnName)} IS NULL)`)],
121
+ execute: [step(`set NOT NULL on "${columnName}"`, `ALTER TABLE ${qualified} ALTER COLUMN ${quoteIdentifier(columnName)} SET NOT NULL`)],
122
+ postcheck: [step(`verify column "${columnName}" is NOT NULL`, columnNullabilityCheck({
123
+ schema: schemaName,
124
+ table: tableName,
125
+ column: columnName,
126
+ nullable: false
127
+ }))]
128
+ };
129
+ }
130
+ function dropNotNull(schemaName, tableName, columnName) {
131
+ const qualified = qualifyTableName(schemaName, tableName);
132
+ return {
133
+ id: `alterNullability.dropNotNull.${tableName}.${columnName}`,
134
+ label: `Drop NOT NULL on "${tableName}"."${columnName}"`,
135
+ operationClass: "widening",
136
+ target: targetDetails("column", columnName, schemaName, tableName),
137
+ precheck: [step(`ensure column "${columnName}" exists`, columnExistsCheck({
138
+ schema: schemaName,
139
+ table: tableName,
140
+ column: columnName
141
+ }))],
142
+ execute: [step(`drop NOT NULL on "${columnName}"`, `ALTER TABLE ${qualified} ALTER COLUMN ${quoteIdentifier(columnName)} DROP NOT NULL`)],
143
+ postcheck: [step(`verify column "${columnName}" is nullable`, columnNullabilityCheck({
144
+ schema: schemaName,
145
+ table: tableName,
146
+ column: columnName,
147
+ nullable: true
148
+ }))]
149
+ };
150
+ }
151
+ /**
152
+ * `defaultSql` is the full `DEFAULT …` clause as produced by
153
+ * `buildColumnDefaultSql` — e.g. `"DEFAULT 42"`,
154
+ * `"DEFAULT (CURRENT_TIMESTAMP)"`, or `"DEFAULT nextval('seq'::regclass)"`.
155
+ *
156
+ * `operationClass` defaults to `'additive'` (setting a default on a column
157
+ * that currently has none). The reconciliation planner passes `'widening'`
158
+ * when the column already has a different default — policy enforcement
159
+ * treats that as a widening change rather than an additive one.
160
+ */
161
+ function setDefault(schemaName, tableName, columnName, defaultSql, operationClass = "additive") {
162
+ const qualified = qualifyTableName(schemaName, tableName);
163
+ return {
164
+ id: `setDefault.${tableName}.${columnName}`,
165
+ label: `Set default on "${tableName}"."${columnName}"`,
166
+ operationClass,
167
+ target: targetDetails("column", columnName, schemaName, tableName),
168
+ precheck: [step(`ensure column "${columnName}" exists`, columnExistsCheck({
169
+ schema: schemaName,
170
+ table: tableName,
171
+ column: columnName
172
+ }))],
173
+ execute: [step(`set default on "${columnName}"`, `ALTER TABLE ${qualified} ALTER COLUMN ${quoteIdentifier(columnName)} SET ${defaultSql}`)],
174
+ postcheck: [step(`verify column "${columnName}" has a default`, columnDefaultExistsCheck({
175
+ schema: schemaName,
176
+ table: tableName,
177
+ column: columnName,
178
+ exists: true
179
+ }))]
180
+ };
181
+ }
182
+ function dropDefault(schemaName, tableName, columnName) {
183
+ const qualified = qualifyTableName(schemaName, tableName);
184
+ return {
185
+ id: `dropDefault.${tableName}.${columnName}`,
186
+ label: `Drop default on "${tableName}"."${columnName}"`,
187
+ operationClass: "destructive",
188
+ target: targetDetails("column", columnName, schemaName, tableName),
189
+ precheck: [step(`ensure column "${columnName}" exists`, columnExistsCheck({
190
+ schema: schemaName,
191
+ table: tableName,
192
+ column: columnName
193
+ }))],
194
+ execute: [step(`drop default on "${columnName}"`, `ALTER TABLE ${qualified} ALTER COLUMN ${quoteIdentifier(columnName)} DROP DEFAULT`)],
195
+ postcheck: [step(`verify column "${columnName}" has no default`, columnDefaultExistsCheck({
196
+ schema: schemaName,
197
+ table: tableName,
198
+ column: columnName,
199
+ exists: false
200
+ }))]
201
+ };
202
+ }
203
+ //#endregion
204
+ //#region src/core/migrations/operations/constraints.ts
205
+ function renderForeignKeySql(schemaName, tableName, fk) {
206
+ let sql = `ALTER TABLE ${qualifyTableName(schemaName, tableName)}
207
+ ADD CONSTRAINT ${quoteIdentifier(fk.name)}
208
+ FOREIGN KEY (${fk.columns.map(quoteIdentifier).join(", ")})
209
+ REFERENCES ${qualifyTableName(fk.references.schema, fk.references.table)} (${fk.references.columns.map(quoteIdentifier).join(", ")})`;
210
+ if (fk.onDelete !== void 0) {
211
+ const action = REFERENTIAL_ACTION_SQL[fk.onDelete];
212
+ if (!action) throw new Error(`Unknown referential action for onDelete: ${String(fk.onDelete)}`);
213
+ sql += `\nON DELETE ${action}`;
214
+ }
215
+ if (fk.onUpdate !== void 0) {
216
+ const action = REFERENTIAL_ACTION_SQL[fk.onUpdate];
217
+ if (!action) throw new Error(`Unknown referential action for onUpdate: ${String(fk.onUpdate)}`);
218
+ sql += `\nON UPDATE ${action}`;
219
+ }
220
+ return sql;
221
+ }
222
+ function addPrimaryKey(schemaName, tableName, constraintName, columns) {
223
+ const qualified = qualifyTableName(schemaName, tableName);
224
+ const columnList = columns.map(quoteIdentifier).join(", ");
225
+ return {
226
+ id: `primaryKey.${tableName}.${constraintName}`,
227
+ label: `Add primary key on "${tableName}"`,
228
+ operationClass: "additive",
229
+ target: targetDetails("primaryKey", constraintName, schemaName, tableName),
230
+ precheck: [step(`ensure primary key "${constraintName}" does not exist`, constraintExistsCheck({
231
+ constraintName,
232
+ schema: schemaName,
233
+ table: tableName,
234
+ exists: false
235
+ }))],
236
+ execute: [step(`add primary key "${constraintName}"`, `ALTER TABLE ${qualified} ADD CONSTRAINT ${quoteIdentifier(constraintName)} PRIMARY KEY (${columnList})`)],
237
+ postcheck: [step(`verify primary key "${constraintName}" exists`, constraintExistsCheck({
238
+ constraintName,
239
+ schema: schemaName,
240
+ table: tableName
241
+ }))]
242
+ };
243
+ }
244
+ function addUnique(schemaName, tableName, constraintName, columns) {
245
+ const qualified = qualifyTableName(schemaName, tableName);
246
+ const columnList = columns.map(quoteIdentifier).join(", ");
247
+ return {
248
+ id: `unique.${tableName}.${constraintName}`,
249
+ label: `Add unique constraint on "${tableName}" (${columns.join(", ")})`,
250
+ operationClass: "additive",
251
+ target: targetDetails("unique", constraintName, schemaName, tableName),
252
+ precheck: [step(`ensure constraint "${constraintName}" does not exist`, constraintExistsCheck({
253
+ constraintName,
254
+ schema: schemaName,
255
+ table: tableName,
256
+ exists: false
257
+ }))],
258
+ execute: [step(`add unique constraint "${constraintName}"`, `ALTER TABLE ${qualified} ADD CONSTRAINT ${quoteIdentifier(constraintName)} UNIQUE (${columnList})`)],
259
+ postcheck: [step(`verify constraint "${constraintName}" exists`, constraintExistsCheck({
260
+ constraintName,
261
+ schema: schemaName,
262
+ table: tableName
263
+ }))]
264
+ };
265
+ }
266
+ function addForeignKey(schemaName, tableName, fk) {
267
+ return {
268
+ id: `foreignKey.${tableName}.${fk.name}`,
269
+ label: `Add foreign key "${fk.name}" on "${tableName}"`,
270
+ operationClass: "additive",
271
+ target: targetDetails("foreignKey", fk.name, schemaName, tableName),
272
+ precheck: [step(`ensure FK "${fk.name}" does not exist`, constraintExistsCheck({
273
+ constraintName: fk.name,
274
+ schema: schemaName,
275
+ table: tableName,
276
+ exists: false
277
+ }))],
278
+ execute: [step(`add FK "${fk.name}"`, renderForeignKeySql(schemaName, tableName, fk))],
279
+ postcheck: [step(`verify FK "${fk.name}" exists`, constraintExistsCheck({
280
+ constraintName: fk.name,
281
+ schema: schemaName,
282
+ table: tableName
283
+ }))]
284
+ };
285
+ }
286
+ /**
287
+ * `kind` feeds the operation's `target.details.objectType`. Descriptor-flow
288
+ * does not carry kind information in its drop-constraint descriptor, so the
289
+ * default is `'unique'`. The reconciliation planner passes the correct kind
290
+ * (`'foreignKey'`, `'primaryKey'`, or `'unique'`) based on the `SchemaIssue`
291
+ * that produced the drop.
292
+ */
293
+ function dropConstraint(schemaName, tableName, constraintName, kind = "unique") {
294
+ const qualified = qualifyTableName(schemaName, tableName);
295
+ return {
296
+ id: `dropConstraint.${tableName}.${constraintName}`,
297
+ label: `Drop constraint "${constraintName}" on "${tableName}"`,
298
+ operationClass: "destructive",
299
+ target: targetDetails(kind, constraintName, schemaName, tableName),
300
+ precheck: [step(`ensure constraint "${constraintName}" exists`, constraintExistsCheck({
301
+ constraintName,
302
+ schema: schemaName,
303
+ table: tableName
304
+ }))],
305
+ execute: [step(`drop constraint "${constraintName}"`, `ALTER TABLE ${qualified} DROP CONSTRAINT ${quoteIdentifier(constraintName)}`)],
306
+ postcheck: [step(`verify constraint "${constraintName}" does not exist`, constraintExistsCheck({
307
+ constraintName,
308
+ schema: schemaName,
309
+ table: tableName,
310
+ exists: false
311
+ }))]
312
+ };
313
+ }
314
+ //#endregion
315
+ //#region src/core/migrations/operations/dependencies.ts
316
+ function createExtension(extensionName) {
317
+ return {
318
+ id: `extension.${extensionName}`,
319
+ label: `Create extension "${extensionName}"`,
320
+ operationClass: "additive",
321
+ target: { id: "postgres" },
322
+ precheck: [],
323
+ execute: [step(`Create extension "${extensionName}"`, `CREATE EXTENSION IF NOT EXISTS ${quoteIdentifier(extensionName)}`)],
324
+ postcheck: []
325
+ };
326
+ }
327
+ /**
328
+ * Install a Postgres extension as the baseline op for an extension-pack
329
+ * contract space. Layered on top of {@link createExtension}: stamps an
330
+ * `invariantId` (required so the per-space marker records the install),
331
+ * scopes the op `id` under a caller-chosen namespace (e.g. `pgvector.`),
332
+ * and emits pre- and postcheck SQL probing `pg_extension`. The richer
333
+ * shape lets the runner's idempotency probe skip the install on re-run
334
+ * (postcheck-pre-satisfied) without firing the precheck.
335
+ *
336
+ * Use this for hand-rolled baseline migrations in contract-space
337
+ * extension packages (e.g. `extension-pgvector`, `extension-paradedb`);
338
+ * use the bare {@link createExtension} for planner-emitted ops where the
339
+ * caller already controls idempotency through the surrounding plan.
340
+ */
341
+ function installExtension(options) {
342
+ const { extensionName, invariantId, id } = options;
343
+ return {
344
+ id,
345
+ label: options.label ?? `Enable extension "${extensionName}"`,
346
+ operationClass: "additive",
347
+ invariantId,
348
+ target: {
349
+ id: "postgres",
350
+ details: {
351
+ schema: "public",
352
+ objectType: "dependency",
353
+ name: extensionName
354
+ }
355
+ },
356
+ precheck: [step(`verify extension "${extensionName}" is not already enabled`, `SELECT NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extensionName}')`)],
357
+ execute: [step(`create extension "${extensionName}"`, `CREATE EXTENSION IF NOT EXISTS ${extensionName}`)],
358
+ postcheck: [step(`confirm extension "${extensionName}" is enabled`, `SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extensionName}')`)]
359
+ };
360
+ }
361
+ //#endregion
362
+ //#region src/core/migrations/operations/enums.ts
363
+ function enumTypeExistsCheck(schemaName, nativeType, exists = true) {
364
+ return `SELECT ${exists ? "EXISTS" : "NOT EXISTS"} (
365
+ SELECT 1
366
+ FROM pg_type t
367
+ JOIN pg_namespace n ON t.typnamespace = n.oid
368
+ WHERE n.nspname = '${escapeLiteral(schemaName)}'
369
+ AND t.typname = '${escapeLiteral(nativeType)}'
370
+ )`;
371
+ }
372
+ function createEnumType(schemaName, typeName, values, nativeType = typeName) {
373
+ const qualifiedType = qualifyName(schemaName, nativeType);
374
+ const literalValues = values.map((v) => `'${escapeLiteral(v)}'`).join(", ");
375
+ return {
376
+ id: `type.${typeName}`,
377
+ label: `Create enum type "${typeName}"`,
378
+ operationClass: "additive",
379
+ target: targetDetails("type", typeName, schemaName),
380
+ precheck: [step(`ensure type "${nativeType}" does not exist`, enumTypeExistsCheck(schemaName, nativeType, false))],
381
+ execute: [step(`create enum type "${typeName}"`, `CREATE TYPE ${qualifiedType} AS ENUM (${literalValues})`)],
382
+ postcheck: [step(`verify type "${nativeType}" exists`, enumTypeExistsCheck(schemaName, nativeType))]
383
+ };
384
+ }
385
+ /**
386
+ * `typeName` is the contract-facing type name (used for id/label).
387
+ * `nativeType` is the Postgres type name to mutate (may differ for external types).
388
+ */
389
+ function addEnumValues(schemaName, typeName, nativeType, values) {
390
+ const qualifiedType = qualifyName(schemaName, nativeType);
391
+ return {
392
+ id: `type.${typeName}.addValues`,
393
+ label: `Add values to enum type "${typeName}": ${values.join(", ")}`,
394
+ operationClass: "additive",
395
+ target: targetDetails("type", typeName, schemaName),
396
+ precheck: [step(`ensure type "${nativeType}" exists`, enumTypeExistsCheck(schemaName, nativeType))],
397
+ execute: values.map((value) => step(`add value '${value}' to enum "${nativeType}"`, `ALTER TYPE ${qualifiedType} ADD VALUE '${escapeLiteral(value)}'`)),
398
+ postcheck: [step(`verify type "${nativeType}" exists`, enumTypeExistsCheck(schemaName, nativeType))]
399
+ };
400
+ }
401
+ function dropEnumType(schemaName, typeName) {
402
+ const qualified = qualifyName(schemaName, typeName);
403
+ return {
404
+ id: `type.${typeName}.drop`,
405
+ label: `Drop enum type "${typeName}"`,
406
+ operationClass: "destructive",
407
+ target: targetDetails("type", typeName, schemaName),
408
+ precheck: [step(`ensure type "${typeName}" exists`, enumTypeExistsCheck(schemaName, typeName))],
409
+ execute: [step(`drop enum type "${typeName}"`, `DROP TYPE ${qualified}`)],
410
+ postcheck: [step(`verify type "${typeName}" removed`, enumTypeExistsCheck(schemaName, typeName, false))]
411
+ };
412
+ }
413
+ function renameType(schemaName, fromName, toName) {
414
+ const qualifiedFrom = qualifyName(schemaName, fromName);
415
+ return {
416
+ id: `type.${fromName}.rename`,
417
+ label: `Rename type "${fromName}" to "${toName}"`,
418
+ operationClass: "destructive",
419
+ target: targetDetails("type", fromName, schemaName),
420
+ precheck: [step(`ensure type "${fromName}" exists`, enumTypeExistsCheck(schemaName, fromName)), step(`ensure type "${toName}" does not already exist`, enumTypeExistsCheck(schemaName, toName, false))],
421
+ execute: [step(`rename type "${fromName}" to "${toName}"`, `ALTER TYPE ${qualifiedFrom} RENAME TO ${quoteIdentifier(toName)}`)],
422
+ postcheck: [step(`verify type "${toName}" exists`, enumTypeExistsCheck(schemaName, toName))]
423
+ };
424
+ }
425
+ //#endregion
426
+ //#region src/core/migrations/operations/indexes.ts
427
+ function renderIndexOptionValue(key, value) {
428
+ if (typeof value === "string") return `'${escapeLiteral(value)}'`;
429
+ if (typeof value === "number" && Number.isFinite(value)) return String(value);
430
+ if (typeof value === "boolean") return value ? "true" : "false";
431
+ throw new Error(`Index option "${key}" must be a string, finite number, or boolean; got ${typeof value}`);
432
+ }
433
+ function renderIndexOptions(options) {
434
+ return Object.entries(options).map(([key, value]) => `${quoteIdentifier(key)} = ${renderIndexOptionValue(key, value)}`).join(", ");
435
+ }
436
+ function createIndex(schemaName, tableName, indexName, columns, extras) {
437
+ const qualified = qualifyTableName(schemaName, tableName);
438
+ const columnList = columns.map(quoteIdentifier).join(", ");
439
+ const using = extras?.type ? ` USING ${quoteIdentifier(extras.type)}` : "";
440
+ const options = extras?.options;
441
+ const withClause = options && Object.keys(options).length > 0 ? ` WITH (${renderIndexOptions(options)})` : "";
442
+ return {
443
+ id: `index.${tableName}.${indexName}`,
444
+ label: `Create index "${indexName}" on "${tableName}"`,
445
+ operationClass: "additive",
446
+ target: targetDetails("index", indexName, schemaName, tableName),
447
+ precheck: [step(`ensure index "${indexName}" does not exist`, `SELECT to_regclass(${toRegclassLiteral(schemaName, indexName)}) IS NULL`)],
448
+ execute: [step(`create index "${indexName}"`, `CREATE INDEX ${quoteIdentifier(indexName)} ON ${qualified}${using} (${columnList})${withClause}`)],
449
+ postcheck: [step(`verify index "${indexName}" exists`, `SELECT to_regclass(${toRegclassLiteral(schemaName, indexName)}) IS NOT NULL`)]
450
+ };
451
+ }
452
+ function dropIndex(schemaName, tableName, indexName) {
453
+ return {
454
+ id: `dropIndex.${tableName}.${indexName}`,
455
+ label: `Drop index "${indexName}"`,
456
+ operationClass: "destructive",
457
+ target: targetDetails("index", indexName, schemaName, tableName),
458
+ precheck: [step(`ensure index "${indexName}" exists`, `SELECT to_regclass(${toRegclassLiteral(schemaName, indexName)}) IS NOT NULL`)],
459
+ execute: [step(`drop index "${indexName}"`, `DROP INDEX ${qualifyTableName(schemaName, indexName)}`)],
460
+ postcheck: [step(`verify index "${indexName}" does not exist`, `SELECT to_regclass(${toRegclassLiteral(schemaName, indexName)}) IS NULL`)]
461
+ };
462
+ }
463
+ //#endregion
464
+ //#region src/core/migrations/operations/tables.ts
465
+ function dropTable(schemaName, tableName) {
466
+ const qualified = qualifyTableName(schemaName, tableName);
467
+ return {
468
+ id: `dropTable.${tableName}`,
469
+ label: `Drop table "${tableName}"`,
470
+ operationClass: "destructive",
471
+ target: targetDetails("table", tableName, schemaName),
472
+ precheck: [step(`ensure table "${tableName}" exists`, `SELECT to_regclass(${toRegclassLiteral(schemaName, tableName)}) IS NOT NULL`)],
473
+ execute: [step(`drop table "${tableName}"`, `DROP TABLE ${qualified}`)],
474
+ postcheck: [step(`verify table "${tableName}" does not exist`, `SELECT to_regclass(${toRegclassLiteral(schemaName, tableName)}) IS NULL`)]
475
+ };
476
+ }
477
+ //#endregion
478
+ //#region src/core/migrations/op-factory-call.ts
479
+ /**
480
+ * Postgres migration IR: one concrete `*Call` class per pure factory under
481
+ * `operations/`, plus a shared `PostgresOpFactoryCallNode` abstract base.
482
+ *
483
+ * Every call class carries the literal arguments its backing factory would
484
+ * receive, computes a human-readable `label` in its constructor, and
485
+ * implements two polymorphic hooks:
486
+ *
487
+ * - `toOp()` — converts the IR node to a runtime
488
+ * `SqlMigrationPlanOperation` by delegating to the matching pure factory
489
+ * under `operations/`. `DataTransformCall.toOp()` always throws
490
+ * `PN-MIG-2001` because a planner-generated data transform is an
491
+ * unfilled authoring stub by construction.
492
+ * - `renderTypeScript()` / `importRequirements()` — inherited from
493
+ * `TsExpression`. Used by `renderCallsToTypeScript` to emit the call as
494
+ * a TypeScript expression inside the scaffolded `migration.ts`.
495
+ *
496
+ * The abstract base and all concrete classes are package-private. External
497
+ * consumers see only the framework-level `OpFactoryCall` interface and the
498
+ * `PostgresOpFactoryCall` union.
499
+ */
500
+ const TARGET_MIGRATION_MODULE = "@prisma-next/postgres/migration";
501
+ const RELATIONAL_CORE_CONTRACT_FREE = "@prisma-next/sql-relational-core/contract-free";
502
+ var PostgresOpFactoryCallNode = class extends TsExpression {
503
+ importRequirements() {
504
+ return [{
505
+ moduleSpecifier: TARGET_MIGRATION_MODULE,
506
+ symbol: this.factoryName
507
+ }];
508
+ }
509
+ freeze() {
510
+ Object.freeze(this);
511
+ }
512
+ };
513
+ function postgresDefaultToDdlColumnDefault(columnDefault) {
514
+ if (!columnDefault) return void 0;
515
+ switch (columnDefault.kind) {
516
+ case "literal": return new LiteralColumnDefault(columnDefault.value);
517
+ case "function":
518
+ if (columnDefault.expression === "autoincrement()") return void 0;
519
+ return new FunctionColumnDefault(columnDefault.expression);
520
+ case "sequence": return new FunctionColumnDefault(`nextval('${escapeLiteral(quoteIdentifier(columnDefault.name))}'::regclass)`);
521
+ default: throw new Error(`postgresDefaultToDdlColumnDefault: unhandled kind "${blindCast(columnDefault).kind}"`);
522
+ }
523
+ }
524
+ function renderDdlColumnDefault(def) {
525
+ if (!def) return "";
526
+ if (def.kind === "literal") return `lit(${jsonToTsSource(def.value)})`;
527
+ return `fn(${jsonToTsSource(def.expression)})`;
528
+ }
529
+ function renderDdlColumnAsTsCall(col) {
530
+ const opts = [];
531
+ if (col.notNull) opts.push("notNull: true");
532
+ if (col.primaryKey) opts.push("primaryKey: true");
533
+ if (col.default) opts.push(`default: ${renderDdlColumnDefault(col.default)}`);
534
+ const optsStr = opts.length > 0 ? `, { ${opts.join(", ")} }` : "";
535
+ return `col(${jsonToTsSource(col.name)}, ${jsonToTsSource(col.type)}${optsStr})`;
536
+ }
537
+ function renderDdlConstraintAsTsCall(constraint) {
538
+ switch (constraint.kind) {
539
+ case "primary-key": {
540
+ const nameOpt = constraint.name ? `, { name: ${jsonToTsSource(constraint.name)} }` : "";
541
+ return `primaryKey(${jsonToTsSource(constraint.columns)}${nameOpt})`;
542
+ }
543
+ case "foreign-key": {
544
+ const opts = [];
545
+ if (constraint.name) opts.push(`name: ${jsonToTsSource(constraint.name)}`);
546
+ if (constraint.onDelete) opts.push(`onDelete: ${jsonToTsSource(constraint.onDelete)}`);
547
+ if (constraint.onUpdate) opts.push(`onUpdate: ${jsonToTsSource(constraint.onUpdate)}`);
548
+ const optsStr = opts.length > 0 ? `, { ${opts.join(", ")} }` : "";
549
+ return `foreignKey(${jsonToTsSource(constraint.columns)}, ${jsonToTsSource(constraint.refTable)}, ${jsonToTsSource(constraint.refColumns)}${optsStr})`;
550
+ }
551
+ case "unique": {
552
+ const nameOpt = constraint.name ? `, { name: ${jsonToTsSource(constraint.name)} }` : "";
553
+ return `unique(${jsonToTsSource(constraint.columns)}${nameOpt})`;
554
+ }
555
+ }
556
+ }
557
+ function needsColOrConstraintImport(columns) {
558
+ return columns.length > 0;
559
+ }
560
+ function constraintImportSymbols(constraints) {
561
+ if (!constraints || constraints.length === 0) return [];
562
+ const symbols = /* @__PURE__ */ new Set();
563
+ for (const c of constraints) if (c.kind === "primary-key") symbols.add("primaryKey");
564
+ else if (c.kind === "foreign-key") symbols.add("foreignKey");
565
+ else if (c.kind === "unique") symbols.add("unique");
566
+ return [...symbols];
567
+ }
568
+ function defaultImportSymbols(columns) {
569
+ const symbols = /* @__PURE__ */ new Set();
570
+ for (const col of columns) if (col.default?.kind === "literal") symbols.add("lit");
571
+ else if (col.default?.kind === "function") symbols.add("fn");
572
+ return [...symbols];
573
+ }
574
+ var CreateTableCall = class extends PostgresOpFactoryCallNode {
575
+ factoryName = "createTable";
576
+ operationClass = "additive";
577
+ schemaName;
578
+ tableName;
579
+ columns;
580
+ constraints;
581
+ label;
582
+ constructor(schemaName, tableName, columns, constraints) {
583
+ super();
584
+ this.schemaName = schemaName;
585
+ this.tableName = tableName;
586
+ this.columns = Object.freeze([...columns]);
587
+ this.constraints = constraints ? Object.freeze([...constraints]) : void 0;
588
+ this.label = `Create table "${tableName}"`;
589
+ this.freeze();
590
+ }
591
+ toOp(lowerer) {
592
+ if (lowerer === void 0) throw new Error(`CreateTableCall.toOp: a DDL lowerer is required on the Postgres planner path (table "${this.tableName}"). Pass the control adapter to createPostgresMigrationPlanner.`);
593
+ const ddlNode = createTable({
594
+ ...this.schemaName !== UNBOUND_NAMESPACE_ID ? { schema: this.schemaName } : {},
595
+ table: this.tableName,
596
+ columns: this.columns,
597
+ ...this.constraints ? { constraints: this.constraints } : {}
598
+ });
599
+ const { sql } = lowerer.lower(ddlNode, { contract: {} });
600
+ const schemaName = this.schemaName;
601
+ const tableName = this.tableName;
602
+ return {
603
+ id: `table.${tableName}`,
604
+ label: `Create table "${tableName}"`,
605
+ summary: `Creates table "${tableName}"`,
606
+ operationClass: "additive",
607
+ target: targetDetails("table", tableName, schemaName),
608
+ precheck: [step(`ensure table "${tableName}" does not exist`, `SELECT to_regclass(${toRegclassLiteral(schemaName, tableName)}) IS NULL`)],
609
+ execute: [step(`create table "${tableName}"`, sql)],
610
+ postcheck: [step(`verify table "${tableName}" exists`, `SELECT to_regclass(${toRegclassLiteral(schemaName, tableName)}) IS NOT NULL`)]
611
+ };
612
+ }
613
+ renderTypeScript() {
614
+ const columnsList = this.columns.map(renderDdlColumnAsTsCall).join(", ");
615
+ const constraintsList = this.constraints ? this.constraints.map(renderDdlConstraintAsTsCall).join(", ") : void 0;
616
+ const opts = [];
617
+ if (this.schemaName !== UNBOUND_NAMESPACE_ID) opts.push(`schema: ${jsonToTsSource(this.schemaName)}`);
618
+ opts.push(`table: ${jsonToTsSource(this.tableName)}`);
619
+ opts.push(`columns: [${columnsList}]`);
620
+ if (constraintsList) opts.push(`constraints: [${constraintsList}]`);
621
+ return `this.createTable({ ${opts.join(", ")} })`;
622
+ }
623
+ importRequirements() {
624
+ const req = [];
625
+ if (needsColOrConstraintImport(this.columns)) {
626
+ req.push({
627
+ moduleSpecifier: RELATIONAL_CORE_CONTRACT_FREE,
628
+ symbol: "col"
629
+ });
630
+ for (const sym of defaultImportSymbols(this.columns)) req.push({
631
+ moduleSpecifier: RELATIONAL_CORE_CONTRACT_FREE,
632
+ symbol: sym
633
+ });
634
+ }
635
+ for (const sym of constraintImportSymbols(this.constraints)) req.push({
636
+ moduleSpecifier: RELATIONAL_CORE_CONTRACT_FREE,
637
+ symbol: sym
638
+ });
639
+ return req;
640
+ }
641
+ };
642
+ var DropTableCall = class extends PostgresOpFactoryCallNode {
643
+ factoryName = "dropTable";
644
+ operationClass = "destructive";
645
+ schemaName;
646
+ tableName;
647
+ label;
648
+ constructor(schemaName, tableName) {
649
+ super();
650
+ this.schemaName = schemaName;
651
+ this.tableName = tableName;
652
+ this.label = `Drop table "${tableName}"`;
653
+ this.freeze();
654
+ }
655
+ toOp() {
656
+ return dropTable(this.schemaName, this.tableName);
657
+ }
658
+ renderTypeScript() {
659
+ return `dropTable(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)})`;
660
+ }
661
+ };
662
+ var AddColumnCall = class extends PostgresOpFactoryCallNode {
663
+ factoryName = "addColumn";
664
+ operationClass = "additive";
665
+ schemaName;
666
+ tableName;
667
+ column;
668
+ label;
669
+ constructor(schemaName, tableName, column) {
670
+ super();
671
+ this.schemaName = schemaName;
672
+ this.tableName = tableName;
673
+ this.column = column;
674
+ this.label = `Add column "${column.name}" to "${tableName}"`;
675
+ this.freeze();
676
+ }
677
+ toOp() {
678
+ return addColumn(this.schemaName, this.tableName, this.column);
679
+ }
680
+ renderTypeScript() {
681
+ return `addColumn(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.column)})`;
682
+ }
683
+ };
684
+ var DropColumnCall = class extends PostgresOpFactoryCallNode {
685
+ factoryName = "dropColumn";
686
+ operationClass = "destructive";
687
+ schemaName;
688
+ tableName;
689
+ columnName;
690
+ label;
691
+ constructor(schemaName, tableName, columnName) {
692
+ super();
693
+ this.schemaName = schemaName;
694
+ this.tableName = tableName;
695
+ this.columnName = columnName;
696
+ this.label = `Drop column "${columnName}" from "${tableName}"`;
697
+ this.freeze();
698
+ }
699
+ toOp() {
700
+ return dropColumn(this.schemaName, this.tableName, this.columnName);
701
+ }
702
+ renderTypeScript() {
703
+ return `dropColumn(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.columnName)})`;
704
+ }
705
+ };
706
+ var AlterColumnTypeCall = class extends PostgresOpFactoryCallNode {
707
+ factoryName = "alterColumnType";
708
+ operationClass = "destructive";
709
+ schemaName;
710
+ tableName;
711
+ columnName;
712
+ options;
713
+ label;
714
+ constructor(schemaName, tableName, columnName, options) {
715
+ super();
716
+ this.schemaName = schemaName;
717
+ this.tableName = tableName;
718
+ this.columnName = columnName;
719
+ this.options = options;
720
+ this.label = `Alter type of "${tableName}"."${columnName}" to ${options.rawTargetTypeForLabel}`;
721
+ this.freeze();
722
+ }
723
+ toOp() {
724
+ return alterColumnType(this.schemaName, this.tableName, this.columnName, this.options);
725
+ }
726
+ renderTypeScript() {
727
+ return `alterColumnType(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.columnName)}, ${jsonToTsSource(this.options)})`;
728
+ }
729
+ };
730
+ var SetNotNullCall = class extends PostgresOpFactoryCallNode {
731
+ factoryName = "setNotNull";
732
+ operationClass = "destructive";
733
+ schemaName;
734
+ tableName;
735
+ columnName;
736
+ label;
737
+ constructor(schemaName, tableName, columnName) {
738
+ super();
739
+ this.schemaName = schemaName;
740
+ this.tableName = tableName;
741
+ this.columnName = columnName;
742
+ this.label = `Set NOT NULL on "${tableName}"."${columnName}"`;
743
+ this.freeze();
744
+ }
745
+ toOp() {
746
+ return setNotNull(this.schemaName, this.tableName, this.columnName);
747
+ }
748
+ renderTypeScript() {
749
+ return `setNotNull(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.columnName)})`;
750
+ }
751
+ };
752
+ var DropNotNullCall = class extends PostgresOpFactoryCallNode {
753
+ factoryName = "dropNotNull";
754
+ operationClass = "widening";
755
+ schemaName;
756
+ tableName;
757
+ columnName;
758
+ label;
759
+ constructor(schemaName, tableName, columnName) {
760
+ super();
761
+ this.schemaName = schemaName;
762
+ this.tableName = tableName;
763
+ this.columnName = columnName;
764
+ this.label = `Drop NOT NULL on "${tableName}"."${columnName}"`;
765
+ this.freeze();
766
+ }
767
+ toOp() {
768
+ return dropNotNull(this.schemaName, this.tableName, this.columnName);
769
+ }
770
+ renderTypeScript() {
771
+ return `dropNotNull(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.columnName)})`;
772
+ }
773
+ };
774
+ var SetDefaultCall = class extends PostgresOpFactoryCallNode {
775
+ factoryName = "setDefault";
776
+ operationClass;
777
+ schemaName;
778
+ tableName;
779
+ columnName;
780
+ defaultSql;
781
+ label;
782
+ constructor(schemaName, tableName, columnName, defaultSql, operationClass = "additive") {
783
+ super();
784
+ this.schemaName = schemaName;
785
+ this.tableName = tableName;
786
+ this.columnName = columnName;
787
+ this.defaultSql = defaultSql;
788
+ this.operationClass = operationClass;
789
+ this.label = `Set default on "${tableName}"."${columnName}"`;
790
+ this.freeze();
791
+ }
792
+ toOp() {
793
+ return setDefault(this.schemaName, this.tableName, this.columnName, this.defaultSql, this.operationClass);
794
+ }
795
+ renderTypeScript() {
796
+ const args = [
797
+ jsonToTsSource(this.schemaName),
798
+ jsonToTsSource(this.tableName),
799
+ jsonToTsSource(this.columnName),
800
+ jsonToTsSource(this.defaultSql)
801
+ ];
802
+ if (this.operationClass !== "additive") args.push(jsonToTsSource(this.operationClass));
803
+ return `setDefault(${args.join(", ")})`;
804
+ }
805
+ };
806
+ var DropDefaultCall = class extends PostgresOpFactoryCallNode {
807
+ factoryName = "dropDefault";
808
+ operationClass = "destructive";
809
+ schemaName;
810
+ tableName;
811
+ columnName;
812
+ label;
813
+ constructor(schemaName, tableName, columnName) {
814
+ super();
815
+ this.schemaName = schemaName;
816
+ this.tableName = tableName;
817
+ this.columnName = columnName;
818
+ this.label = `Drop default on "${tableName}"."${columnName}"`;
819
+ this.freeze();
820
+ }
821
+ toOp() {
822
+ return dropDefault(this.schemaName, this.tableName, this.columnName);
823
+ }
824
+ renderTypeScript() {
825
+ return `dropDefault(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.columnName)})`;
826
+ }
827
+ };
828
+ var AddPrimaryKeyCall = class extends PostgresOpFactoryCallNode {
829
+ factoryName = "addPrimaryKey";
830
+ operationClass = "additive";
831
+ schemaName;
832
+ tableName;
833
+ constraintName;
834
+ columns;
835
+ label;
836
+ constructor(schemaName, tableName, constraintName, columns) {
837
+ super();
838
+ this.schemaName = schemaName;
839
+ this.tableName = tableName;
840
+ this.constraintName = constraintName;
841
+ this.columns = columns;
842
+ this.label = `Add primary key on "${tableName}"`;
843
+ this.freeze();
844
+ }
845
+ toOp() {
846
+ return addPrimaryKey(this.schemaName, this.tableName, this.constraintName, this.columns);
847
+ }
848
+ renderTypeScript() {
849
+ return `addPrimaryKey(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.constraintName)}, ${jsonToTsSource(this.columns)})`;
850
+ }
851
+ };
852
+ var AddUniqueCall = class extends PostgresOpFactoryCallNode {
853
+ factoryName = "addUnique";
854
+ operationClass = "additive";
855
+ schemaName;
856
+ tableName;
857
+ constraintName;
858
+ columns;
859
+ label;
860
+ constructor(schemaName, tableName, constraintName, columns) {
861
+ super();
862
+ this.schemaName = schemaName;
863
+ this.tableName = tableName;
864
+ this.constraintName = constraintName;
865
+ this.columns = columns;
866
+ this.label = `Add unique constraint on "${tableName}" (${columns.join(", ")})`;
867
+ this.freeze();
868
+ }
869
+ toOp() {
870
+ return addUnique(this.schemaName, this.tableName, this.constraintName, this.columns);
871
+ }
872
+ renderTypeScript() {
873
+ return `addUnique(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.constraintName)}, ${jsonToTsSource(this.columns)})`;
874
+ }
875
+ };
876
+ var AddForeignKeyCall = class extends PostgresOpFactoryCallNode {
877
+ factoryName = "addForeignKey";
878
+ operationClass = "additive";
879
+ schemaName;
880
+ tableName;
881
+ fk;
882
+ label;
883
+ constructor(schemaName, tableName, fk) {
884
+ super();
885
+ this.schemaName = schemaName;
886
+ this.tableName = tableName;
887
+ this.fk = fk;
888
+ this.label = `Add foreign key "${fk.name}" on "${tableName}"`;
889
+ this.freeze();
890
+ }
891
+ toOp() {
892
+ return addForeignKey(this.schemaName, this.tableName, this.fk);
893
+ }
894
+ renderTypeScript() {
895
+ return `addForeignKey(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.fk)})`;
896
+ }
897
+ };
898
+ var DropConstraintCall = class extends PostgresOpFactoryCallNode {
899
+ factoryName = "dropConstraint";
900
+ operationClass = "destructive";
901
+ schemaName;
902
+ tableName;
903
+ constraintName;
904
+ kind;
905
+ label;
906
+ constructor(schemaName, tableName, constraintName, kind = "unique") {
907
+ super();
908
+ this.schemaName = schemaName;
909
+ this.tableName = tableName;
910
+ this.constraintName = constraintName;
911
+ this.kind = kind;
912
+ this.label = `Drop constraint "${constraintName}" on "${tableName}"`;
913
+ this.freeze();
914
+ }
915
+ toOp() {
916
+ return dropConstraint(this.schemaName, this.tableName, this.constraintName, this.kind);
917
+ }
918
+ renderTypeScript() {
919
+ const args = [
920
+ jsonToTsSource(this.schemaName),
921
+ jsonToTsSource(this.tableName),
922
+ jsonToTsSource(this.constraintName)
923
+ ];
924
+ if (this.kind !== "unique") args.push(jsonToTsSource(this.kind));
925
+ return `dropConstraint(${args.join(", ")})`;
926
+ }
927
+ };
928
+ var CreateIndexCall = class extends PostgresOpFactoryCallNode {
929
+ factoryName = "createIndex";
930
+ operationClass = "additive";
931
+ schemaName;
932
+ tableName;
933
+ indexName;
934
+ columns;
935
+ indexType;
936
+ options;
937
+ label;
938
+ constructor(schemaName, tableName, indexName, columns, extras) {
939
+ super();
940
+ this.schemaName = schemaName;
941
+ this.tableName = tableName;
942
+ this.indexName = indexName;
943
+ this.columns = columns;
944
+ this.indexType = extras?.type;
945
+ this.options = extras?.options;
946
+ this.label = `Create index "${indexName}" on "${tableName}"`;
947
+ this.freeze();
948
+ }
949
+ toOp() {
950
+ const extras = {};
951
+ if (this.indexType !== void 0) extras.type = this.indexType;
952
+ if (this.options !== void 0) extras.options = this.options;
953
+ return createIndex(this.schemaName, this.tableName, this.indexName, this.columns, extras);
954
+ }
955
+ renderTypeScript() {
956
+ const args = [
957
+ jsonToTsSource(this.schemaName),
958
+ jsonToTsSource(this.tableName),
959
+ jsonToTsSource(this.indexName),
960
+ jsonToTsSource(this.columns)
961
+ ];
962
+ if (this.indexType !== void 0 || this.options !== void 0) {
963
+ const extrasParts = [];
964
+ if (this.indexType !== void 0) extrasParts.push(`type: ${jsonToTsSource(this.indexType)}`);
965
+ if (this.options !== void 0) extrasParts.push(`options: ${jsonToTsSource(this.options)}`);
966
+ args.push(`{ ${extrasParts.join(", ")} }`);
967
+ }
968
+ return `createIndex(${args.join(", ")})`;
969
+ }
970
+ };
971
+ var DropIndexCall = class extends PostgresOpFactoryCallNode {
972
+ factoryName = "dropIndex";
973
+ operationClass = "destructive";
974
+ schemaName;
975
+ tableName;
976
+ indexName;
977
+ label;
978
+ constructor(schemaName, tableName, indexName) {
979
+ super();
980
+ this.schemaName = schemaName;
981
+ this.tableName = tableName;
982
+ this.indexName = indexName;
983
+ this.label = `Drop index "${indexName}"`;
984
+ this.freeze();
985
+ }
986
+ toOp() {
987
+ return dropIndex(this.schemaName, this.tableName, this.indexName);
988
+ }
989
+ renderTypeScript() {
990
+ return `dropIndex(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.tableName)}, ${jsonToTsSource(this.indexName)})`;
991
+ }
992
+ };
993
+ var CreateEnumTypeCall = class extends PostgresOpFactoryCallNode {
994
+ factoryName = "createEnumType";
995
+ operationClass = "additive";
996
+ schemaName;
997
+ typeName;
998
+ nativeType;
999
+ values;
1000
+ label;
1001
+ constructor(schemaName, typeName, values, nativeType = typeName) {
1002
+ super();
1003
+ this.schemaName = schemaName;
1004
+ this.typeName = typeName;
1005
+ this.nativeType = nativeType;
1006
+ this.values = values;
1007
+ this.label = `Create enum type "${typeName}"`;
1008
+ this.freeze();
1009
+ }
1010
+ toOp() {
1011
+ return createEnumType(this.schemaName, this.typeName, this.values, this.nativeType);
1012
+ }
1013
+ renderTypeScript() {
1014
+ const nativeArg = this.nativeType === this.typeName ? "" : `, ${jsonToTsSource(this.nativeType)}`;
1015
+ return `createEnumType(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.typeName)}, ${jsonToTsSource(this.values)}${nativeArg})`;
1016
+ }
1017
+ };
1018
+ var AddEnumValuesCall = class extends PostgresOpFactoryCallNode {
1019
+ factoryName = "addEnumValues";
1020
+ operationClass = "additive";
1021
+ schemaName;
1022
+ typeName;
1023
+ nativeType;
1024
+ values;
1025
+ label;
1026
+ constructor(schemaName, typeName, nativeType, values) {
1027
+ super();
1028
+ this.schemaName = schemaName;
1029
+ this.typeName = typeName;
1030
+ this.nativeType = nativeType;
1031
+ this.values = values;
1032
+ this.label = `Add values to enum type "${typeName}": ${values.join(", ")}`;
1033
+ this.freeze();
1034
+ }
1035
+ toOp() {
1036
+ return addEnumValues(this.schemaName, this.typeName, this.nativeType, this.values);
1037
+ }
1038
+ renderTypeScript() {
1039
+ return `addEnumValues(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.typeName)}, ${jsonToTsSource(this.nativeType)}, ${jsonToTsSource(this.values)})`;
1040
+ }
1041
+ };
1042
+ var DropEnumTypeCall = class extends PostgresOpFactoryCallNode {
1043
+ factoryName = "dropEnumType";
1044
+ operationClass = "destructive";
1045
+ schemaName;
1046
+ typeName;
1047
+ label;
1048
+ constructor(schemaName, typeName) {
1049
+ super();
1050
+ this.schemaName = schemaName;
1051
+ this.typeName = typeName;
1052
+ this.label = `Drop enum type "${typeName}"`;
1053
+ this.freeze();
1054
+ }
1055
+ toOp() {
1056
+ return dropEnumType(this.schemaName, this.typeName);
1057
+ }
1058
+ renderTypeScript() {
1059
+ return `dropEnumType(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.typeName)})`;
1060
+ }
1061
+ };
1062
+ var RenameTypeCall = class extends PostgresOpFactoryCallNode {
1063
+ factoryName = "renameType";
1064
+ operationClass = "destructive";
1065
+ schemaName;
1066
+ fromName;
1067
+ toName;
1068
+ label;
1069
+ constructor(schemaName, fromName, toName) {
1070
+ super();
1071
+ this.schemaName = schemaName;
1072
+ this.fromName = fromName;
1073
+ this.toName = toName;
1074
+ this.label = `Rename type "${fromName}" to "${toName}"`;
1075
+ this.freeze();
1076
+ }
1077
+ toOp() {
1078
+ return renameType(this.schemaName, this.fromName, this.toName);
1079
+ }
1080
+ renderTypeScript() {
1081
+ return `renameType(${jsonToTsSource(this.schemaName)}, ${jsonToTsSource(this.fromName)}, ${jsonToTsSource(this.toName)})`;
1082
+ }
1083
+ };
1084
+ /**
1085
+ * Laundered pre-built operation.
1086
+ *
1087
+ * Wraps an already-materialized `SqlMigrationPlanOperation` — typically one
1088
+ * produced by a SQL-family method or a codec control hook — so the planner
1089
+ * can carry it alongside IR nodes without reverse-engineering it into a
1090
+ * structured call class. Doubles as the user-facing escape hatch for raw
1091
+ * migrations: authors can pass a full op shape to `rawSql({...})`.
1092
+ *
1093
+ * `toOp()` returns the stored op unchanged. `renderTypeScript()` emits
1094
+ * `rawSql({...})` with the op serialized as a JSON literal — round-tripping
1095
+ * requires every field on the op to be JSON-serializable (no closures).
1096
+ */
1097
+ var RawSqlCall = class extends PostgresOpFactoryCallNode {
1098
+ factoryName = "rawSql";
1099
+ operationClass;
1100
+ label;
1101
+ op;
1102
+ constructor(op) {
1103
+ super();
1104
+ this.op = op;
1105
+ this.label = op.label;
1106
+ this.operationClass = op.operationClass;
1107
+ this.freeze();
1108
+ }
1109
+ toOp() {
1110
+ return this.op;
1111
+ }
1112
+ renderTypeScript() {
1113
+ return `rawSql(${jsonToTsSource(this.op)})`;
1114
+ }
1115
+ };
1116
+ var CreateExtensionCall = class extends PostgresOpFactoryCallNode {
1117
+ factoryName = "createExtension";
1118
+ operationClass = "additive";
1119
+ extensionName;
1120
+ label;
1121
+ constructor(extensionName) {
1122
+ super();
1123
+ this.extensionName = extensionName;
1124
+ this.label = `Create extension "${extensionName}"`;
1125
+ this.freeze();
1126
+ }
1127
+ toOp() {
1128
+ return createExtension(this.extensionName);
1129
+ }
1130
+ renderTypeScript() {
1131
+ return `createExtension(${jsonToTsSource(this.extensionName)})`;
1132
+ }
1133
+ };
1134
+ var CreateSchemaCall = class extends PostgresOpFactoryCallNode {
1135
+ factoryName = "createSchema";
1136
+ operationClass = "additive";
1137
+ schemaName;
1138
+ label;
1139
+ constructor(schemaName) {
1140
+ super();
1141
+ this.schemaName = schemaName;
1142
+ this.label = `Create schema "${schemaName}"`;
1143
+ this.freeze();
1144
+ }
1145
+ toOp(lowerer) {
1146
+ if (lowerer === void 0) throw new Error(`CreateSchemaCall.toOp: a DDL lowerer is required on the Postgres planner path (schema "${this.schemaName}"). Pass the control adapter to createPostgresMigrationPlanner.`);
1147
+ const ddlNode = createSchema({
1148
+ schema: this.schemaName,
1149
+ ifNotExists: true
1150
+ });
1151
+ const { sql } = lowerer.lower(ddlNode, { contract: {} });
1152
+ const schemaName = this.schemaName;
1153
+ return {
1154
+ id: `schema.${schemaName}`,
1155
+ label: `Create schema "${schemaName}"`,
1156
+ operationClass: "additive",
1157
+ target: { id: "postgres" },
1158
+ precheck: [],
1159
+ execute: [step(`Create schema "${schemaName}"`, sql)],
1160
+ postcheck: []
1161
+ };
1162
+ }
1163
+ renderTypeScript() {
1164
+ return `this.createSchema({ schema: ${jsonToTsSource(this.schemaName)} })`;
1165
+ }
1166
+ importRequirements() {
1167
+ return [];
1168
+ }
1169
+ };
1170
+ /**
1171
+ * A planner-generated data-transform stub. `checkSlot` and `runSlot` name
1172
+ * the unfilled authoring slots that the rendered `migration.ts` will expose
1173
+ * to the user via `placeholder("…")` calls. `toOp()` always throws
1174
+ * `PN-MIG-2001`: the planner cannot lower a stubbed transform to a runtime
1175
+ * op — the user must fill the rendered `migration.ts` and re-emit.
1176
+ */
1177
+ var DataTransformCall = class extends PostgresOpFactoryCallNode {
1178
+ factoryName = "dataTransform";
1179
+ operationClass;
1180
+ label;
1181
+ checkSlot;
1182
+ runSlot;
1183
+ constructor(label, checkSlot, runSlot, operationClass = "data") {
1184
+ super();
1185
+ this.label = label;
1186
+ this.checkSlot = checkSlot;
1187
+ this.runSlot = runSlot;
1188
+ this.operationClass = operationClass;
1189
+ this.freeze();
1190
+ }
1191
+ toOp() {
1192
+ throw errorUnfilledPlaceholder(this.label);
1193
+ }
1194
+ renderTypeScript() {
1195
+ return [
1196
+ `this.dataTransform(endContract, ${jsonToTsSource(this.label)}, {`,
1197
+ ` check: () => placeholder(${jsonToTsSource(this.checkSlot)}),`,
1198
+ ` run: () => placeholder(${jsonToTsSource(this.runSlot)}),`,
1199
+ "})"
1200
+ ].join("\n");
1201
+ }
1202
+ importRequirements() {
1203
+ return [{
1204
+ moduleSpecifier: TARGET_MIGRATION_MODULE,
1205
+ symbol: "placeholder"
1206
+ }, {
1207
+ moduleSpecifier: "./end-contract.json",
1208
+ symbol: "endContract",
1209
+ kind: "default",
1210
+ attributes: { type: "json" }
1211
+ }];
1212
+ }
1213
+ };
1214
+ //#endregion
1215
+ export { dropEnumType as A, dropColumn as B, SetNotNullCall as C, dropIndex as D, createIndex as E, addPrimaryKey as F, dropNotNull as H, addUnique as I, dropConstraint as L, createExtension as M, installExtension as N, addEnumValues as O, addForeignKey as P, addColumn as R, SetDefaultCall as S, dropTable as T, setDefault as U, dropDefault as V, setNotNull as W, DropIndexCall as _, AddUniqueCall as a, RawSqlCall as b, CreateExtensionCall as c, CreateTableCall as d, DataTransformCall as f, DropEnumTypeCall as g, DropDefaultCall as h, AddPrimaryKeyCall as i, renameType as j, createEnumType as k, CreateIndexCall as l, DropConstraintCall as m, AddEnumValuesCall as n, AlterColumnTypeCall as o, DropColumnCall as p, AddForeignKeyCall as r, CreateEnumTypeCall as s, AddColumnCall as t, CreateSchemaCall as u, DropNotNullCall as v, postgresDefaultToDdlColumnDefault as w, RenameTypeCall as x, DropTableCall as y, alterColumnType as z };
1216
+
1217
+ //# sourceMappingURL=op-factory-call-q9Tpw6wc.mjs.map