@prisma-next/target-postgres 0.13.0-dev.2 → 0.13.0-dev.20

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 (115) hide show
  1. package/dist/{codec-ids-B1vOchLE.d.mts → codec-ids-BzrFF-I4.d.mts} +3 -2
  2. package/dist/{codec-ids-B1vOchLE.d.mts.map → codec-ids-BzrFF-I4.d.mts.map} +1 -1
  3. package/dist/{codec-ids-CTikp1if.mjs → codec-ids-C_-Hj6bL.mjs} +3 -2
  4. package/dist/{codec-ids-CTikp1if.mjs.map → codec-ids-C_-Hj6bL.mjs.map} +1 -1
  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-B0WT0obB.d.mts} +3 -2
  8. package/dist/codec-types-B0WT0obB.d.mts.map +1 -0
  9. package/dist/codec-types.d.mts +1 -1
  10. package/dist/{codecs-CBpEv4s5.d.mts → codecs-CX56Smsj.d.mts} +26 -3
  11. package/dist/{codecs-CBpEv4s5.d.mts.map → codecs-CX56Smsj.d.mts.map} +1 -1
  12. package/dist/codecs.d.mts +2 -2
  13. package/dist/codecs.mjs +31 -2
  14. package/dist/codecs.mjs.map +1 -1
  15. package/dist/contract-free.mjs +1 -1
  16. package/dist/control.d.mts.map +1 -1
  17. package/dist/control.mjs +19 -22
  18. package/dist/control.mjs.map +1 -1
  19. package/dist/{data-transform-D25tLeYU.mjs → data-transform-BOWpliq8.mjs} +9 -17
  20. package/dist/data-transform-BOWpliq8.mjs.map +1 -0
  21. package/dist/{data-transform-DGOqcLrf.d.mts → data-transform-DDgWdB5o.d.mts} +2 -2
  22. package/dist/data-transform-DDgWdB5o.d.mts.map +1 -0
  23. package/dist/data-transform.d.mts +1 -1
  24. package/dist/data-transform.mjs +1 -1
  25. package/dist/{descriptor-meta-DKmj-IMN.mjs → descriptor-meta-26XbSdrs.mjs} +2 -2
  26. package/dist/{descriptor-meta-DKmj-IMN.mjs.map → descriptor-meta-26XbSdrs.mjs.map} +1 -1
  27. package/dist/{descriptor-meta-runtime-My8_s4cs.mjs → descriptor-meta-runtime-B5ZtrgVF.mjs} +40 -2
  28. package/dist/descriptor-meta-runtime-B5ZtrgVF.mjs.map +1 -0
  29. package/dist/{enum-planning-BCyvlFHk.mjs → enum-planning-eI1gxdKF.mjs} +0 -0
  30. package/dist/enum-planning-eI1gxdKF.mjs.map +1 -0
  31. package/dist/enum-planning.d.mts +13 -7
  32. package/dist/enum-planning.d.mts.map +1 -1
  33. package/dist/enum-planning.mjs +2 -2
  34. package/dist/{issue-planner-Br0pt1Ea.mjs → issue-planner-DsCMvGja.mjs} +14 -7
  35. package/dist/issue-planner-DsCMvGja.mjs.map +1 -0
  36. package/dist/issue-planner.d.mts +1 -1
  37. package/dist/issue-planner.d.mts.map +1 -1
  38. package/dist/issue-planner.mjs +1 -1
  39. package/dist/migration.d.mts +2 -2
  40. package/dist/migration.mjs +3 -3
  41. package/dist/{op-factory-call-D2aAUhmS.mjs → op-factory-call-BZ3xDgCY.mjs} +18 -10
  42. package/dist/op-factory-call-BZ3xDgCY.mjs.map +1 -0
  43. package/dist/{op-factory-call-DMA86_2D.d.mts → op-factory-call-DEvD9sbB.d.mts} +5 -5
  44. package/dist/op-factory-call-DEvD9sbB.d.mts.map +1 -0
  45. package/dist/op-factory-call.d.mts +1 -1
  46. package/dist/op-factory-call.mjs +1 -1
  47. package/dist/pack.d.mts +38 -1
  48. package/dist/pack.d.mts.map +1 -1
  49. package/dist/pack.mjs +1 -1
  50. package/dist/{planner-CAYPJObw.mjs → planner-QWZaVIrn.mjs} +6 -6
  51. package/dist/planner-QWZaVIrn.mjs.map +1 -0
  52. package/dist/{planner-produced-postgres-migration-NSEhWL0L.mjs → planner-produced-postgres-migration-BHH6-2-1.mjs} +6 -4
  53. package/dist/planner-produced-postgres-migration-BHH6-2-1.mjs.map +1 -0
  54. package/dist/{planner-produced-postgres-migration-B4EDvLdz.d.mts → planner-produced-postgres-migration-CIX9peJN.d.mts} +5 -6
  55. package/dist/planner-produced-postgres-migration-CIX9peJN.d.mts.map +1 -0
  56. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  57. package/dist/planner-produced-postgres-migration.mjs +1 -1
  58. package/dist/{planner-sql-checks-DAdhnI2c.mjs → planner-sql-checks-CrAbk7gX.mjs} +2 -2
  59. package/dist/{planner-sql-checks-DAdhnI2c.mjs.map → planner-sql-checks-CrAbk7gX.mjs.map} +1 -1
  60. package/dist/planner-sql-checks.mjs +1 -1
  61. package/dist/planner.d.mts +4 -4
  62. package/dist/planner.d.mts.map +1 -1
  63. package/dist/planner.mjs +1 -1
  64. package/dist/{postgres-contract-serializer-DYTyXjPf.mjs → postgres-contract-serializer-BDbqgeFb.mjs} +14 -7
  65. package/dist/postgres-contract-serializer-BDbqgeFb.mjs.map +1 -0
  66. package/dist/{postgres-migration-COore9Mz.mjs → postgres-migration-BN6TNJNf.mjs} +3 -3
  67. package/dist/{postgres-migration-COore9Mz.mjs.map → postgres-migration-BN6TNJNf.mjs.map} +1 -1
  68. package/dist/{postgres-migration-DZ_gLUOW.d.mts → postgres-migration-h_DA3LRq.d.mts} +5 -5
  69. package/dist/postgres-migration-h_DA3LRq.d.mts.map +1 -0
  70. package/dist/{postgres-schema-BuxCxbvB.mjs → postgres-schema-BAgkIU9u.mjs} +17 -6
  71. package/dist/postgres-schema-BAgkIU9u.mjs.map +1 -0
  72. package/dist/{render-ops-BpjstrKQ.mjs → render-ops-BREh1kHe.mjs} +10 -5
  73. package/dist/render-ops-BREh1kHe.mjs.map +1 -0
  74. package/dist/render-ops.d.mts +2 -2
  75. package/dist/render-ops.d.mts.map +1 -1
  76. package/dist/render-ops.mjs +1 -1
  77. package/dist/runtime.d.mts.map +1 -1
  78. package/dist/runtime.mjs +2 -2
  79. package/dist/types.d.mts +3 -1
  80. package/dist/types.d.mts.map +1 -1
  81. package/dist/types.mjs +1 -1
  82. package/package.json +17 -17
  83. package/src/core/authoring.ts +42 -0
  84. package/src/core/codec-ids.ts +1 -0
  85. package/src/core/codec-type-map.ts +2 -0
  86. package/src/core/codecs.ts +44 -0
  87. package/src/core/migrations/enum-planning.ts +33 -29
  88. package/src/core/migrations/issue-planner.ts +24 -3
  89. package/src/core/migrations/op-factory-call.ts +22 -9
  90. package/src/core/migrations/operations/data-transform.ts +15 -18
  91. package/src/core/migrations/planner-produced-postgres-migration.ts +15 -7
  92. package/src/core/migrations/planner.ts +6 -4
  93. package/src/core/migrations/postgres-migration.ts +3 -3
  94. package/src/core/migrations/render-ops.ts +26 -13
  95. package/src/core/migrations/runner.ts +26 -16
  96. package/src/core/postgres-contract-serializer.ts +15 -1
  97. package/src/core/postgres-schema.ts +35 -16
  98. package/src/exports/codecs.ts +2 -0
  99. package/src/exports/control.ts +9 -16
  100. package/src/exports/enum-planning.ts +0 -1
  101. package/dist/codec-types-CnFiNML4.d.mts.map +0 -1
  102. package/dist/data-transform-D25tLeYU.mjs.map +0 -1
  103. package/dist/data-transform-DGOqcLrf.d.mts.map +0 -1
  104. package/dist/descriptor-meta-runtime-My8_s4cs.mjs.map +0 -1
  105. package/dist/enum-planning-BCyvlFHk.mjs.map +0 -1
  106. package/dist/issue-planner-Br0pt1Ea.mjs.map +0 -1
  107. package/dist/op-factory-call-D2aAUhmS.mjs.map +0 -1
  108. package/dist/op-factory-call-DMA86_2D.d.mts.map +0 -1
  109. package/dist/planner-CAYPJObw.mjs.map +0 -1
  110. package/dist/planner-produced-postgres-migration-B4EDvLdz.d.mts.map +0 -1
  111. package/dist/planner-produced-postgres-migration-NSEhWL0L.mjs.map +0 -1
  112. package/dist/postgres-contract-serializer-DYTyXjPf.mjs.map +0 -1
  113. package/dist/postgres-migration-DZ_gLUOW.d.mts.map +0 -1
  114. package/dist/postgres-schema-BuxCxbvB.mjs.map +0 -1
  115. package/dist/render-ops-BpjstrKQ.mjs.map +0 -1
@@ -38,6 +38,7 @@ import {
38
38
  pgTimestampDescriptor,
39
39
  pgTimestamptzDescriptor,
40
40
  pgTimetzDescriptor,
41
+ pgUuidDescriptor,
41
42
  pgVarbitDescriptor,
42
43
  pgVarcharDescriptor,
43
44
  } from './codecs';
@@ -68,6 +69,7 @@ export const codecDescriptorMap = {
68
69
  bit: pgBitDescriptor,
69
70
  'bit varying': pgVarbitDescriptor,
70
71
  bytea: pgByteaDescriptor,
72
+ uuid: pgUuidDescriptor,
71
73
  interval: pgIntervalDescriptor,
72
74
  enum: pgEnumDescriptor,
73
75
  json: pgJsonDescriptor,
@@ -36,6 +36,7 @@ import {
36
36
  sqlTimestampDescriptor,
37
37
  sqlVarcharDescriptor,
38
38
  } from '@prisma-next/sql-relational-core/ast';
39
+ import { blindCast } from '@prisma-next/utils/casts';
39
40
  import type { StandardSchemaV1 } from '@standard-schema/spec';
40
41
  import { type as arktype } from 'arktype';
41
42
  import {
@@ -77,6 +78,7 @@ import {
77
78
  PG_TIMESTAMP_CODEC_ID,
78
79
  PG_TIMESTAMPTZ_CODEC_ID,
79
80
  PG_TIMETZ_CODEC_ID,
81
+ PG_UUID_CODEC_ID,
80
82
  PG_VARBIT_CODEC_ID,
81
83
  PG_VARCHAR_CODEC_ID,
82
84
  } from './codec-ids';
@@ -789,6 +791,47 @@ export const pgByteaColumn = () =>
789
791
  pgByteaColumn satisfies ColumnHelperFor<PgByteaDescriptor>;
790
792
  pgByteaColumn satisfies ColumnHelperForStrict<PgByteaDescriptor>;
791
793
 
794
+ const PG_UUID_META = { db: { sql: { postgres: { nativeType: 'uuid' } } } } as const;
795
+
796
+ export class PgUuidCodec extends CodecImpl<
797
+ typeof PG_UUID_CODEC_ID,
798
+ readonly ['equality', 'order'],
799
+ string,
800
+ string
801
+ > {
802
+ async encode(value: string, _ctx: CodecCallContext): Promise<string> {
803
+ return value;
804
+ }
805
+ async decode(wire: string, _ctx: CodecCallContext): Promise<string> {
806
+ return wire;
807
+ }
808
+ encodeJson(value: string): JsonValue {
809
+ return value;
810
+ }
811
+ decodeJson(json: JsonValue): string {
812
+ return blindCast<string, 'uuid columns serialize to JSON as their wire string form'>(json);
813
+ }
814
+ }
815
+
816
+ export class PgUuidDescriptor extends CodecDescriptorImpl<void> {
817
+ override readonly codecId = PG_UUID_CODEC_ID;
818
+ override readonly traits = ['equality', 'order'] as const;
819
+ override readonly targetTypes = ['uuid'] as const;
820
+ override readonly meta = PG_UUID_META;
821
+ override readonly paramsSchema: StandardSchemaV1<void> = voidParamsSchema;
822
+ override factory(): (ctx: CodecInstanceContext) => PgUuidCodec {
823
+ return () => new PgUuidCodec(this);
824
+ }
825
+ }
826
+
827
+ export const pgUuidDescriptor = new PgUuidDescriptor();
828
+
829
+ export const pgUuidColumn = () =>
830
+ column(pgUuidDescriptor.factory(), pgUuidDescriptor.codecId, undefined, 'uuid');
831
+
832
+ pgUuidColumn satisfies ColumnHelperFor<PgUuidDescriptor>;
833
+ pgUuidColumn satisfies ColumnHelperForStrict<PgUuidDescriptor>;
834
+
792
835
  export class PgIntervalCodec extends CodecImpl<
793
836
  typeof PG_INTERVAL_CODEC_ID,
794
837
  readonly ['equality', 'order'],
@@ -1075,6 +1118,7 @@ export const codecDescriptors: readonly AnyCodecDescriptor[] = [
1075
1118
  pgBitDescriptor,
1076
1119
  pgVarbitDescriptor,
1077
1120
  pgByteaDescriptor,
1121
+ pgUuidDescriptor,
1078
1122
  pgIntervalDescriptor,
1079
1123
  pgEnumDescriptor,
1080
1124
  pgJsonDescriptor,
@@ -14,17 +14,25 @@ import { isPostgresSchema } from '../postgres-schema';
14
14
 
15
15
  /**
16
16
  * Codec-typed enum entry shape stored under
17
- * `schema.annotations.pg.storageTypes[(schemaName, nativeType)]`.
17
+ * `schema.annotations.pg.enumTypes[schemaName][nativeType]`.
18
18
  */
19
19
  interface PgStorageTypeEntry {
20
20
  readonly codecId?: string;
21
21
  readonly typeParams?: { readonly values?: unknown };
22
22
  }
23
23
 
24
+ /**
25
+ * Live enum types keyed by `(schemaName, nativeType)` as a nested map, so two
26
+ * schemas sharing a native enum name stay distinct without packing the pair
27
+ * into a string. This is the same `(namespace, entityName)` coordinate the
28
+ * contract side addresses entities by.
29
+ */
30
+ type PgEnumTypesMap = Readonly<Record<string, Readonly<Record<string, PgStorageTypeEntry>>>>;
31
+
24
32
  /** Postgres-specific subtree on family `SqlSchemaIR.annotations`. */
25
33
  export interface PostgresSchemaIrAnnotations {
26
34
  readonly schema?: string;
27
- readonly storageTypes?: Readonly<Record<string, PgStorageTypeEntry>>;
35
+ readonly enumTypes?: PgEnumTypesMap;
28
36
  }
29
37
 
30
38
  function readOptionalString(value: unknown): string | undefined {
@@ -50,20 +58,27 @@ function readPgStorageTypeEntry(value: unknown): PgStorageTypeEntry | undefined
50
58
  };
51
59
  }
52
60
 
53
- function readPgStorageTypesMap(
54
- value: unknown,
55
- ): Readonly<Record<string, PgStorageTypeEntry>> | undefined {
61
+ function readPgEnumTypesMap(value: unknown): PgEnumTypesMap | undefined {
56
62
  if (value === null || typeof value !== 'object' || Array.isArray(value)) {
57
63
  return undefined;
58
64
  }
59
- const entries: Record<string, PgStorageTypeEntry> = {};
60
- for (const [key, entryValue] of Object.entries(value)) {
61
- const entry = readPgStorageTypeEntry(entryValue);
62
- if (entry !== undefined) {
63
- entries[key] = entry;
65
+ const bySchema: Record<string, Record<string, PgStorageTypeEntry>> = {};
66
+ for (const [schemaName, byTypeRaw] of Object.entries(value)) {
67
+ if (byTypeRaw === null || typeof byTypeRaw !== 'object' || Array.isArray(byTypeRaw)) {
68
+ continue;
69
+ }
70
+ const byType: Record<string, PgStorageTypeEntry> = {};
71
+ for (const [nativeType, entryValue] of Object.entries(byTypeRaw)) {
72
+ const entry = readPgStorageTypeEntry(entryValue);
73
+ if (entry !== undefined) {
74
+ byType[nativeType] = entry;
75
+ }
76
+ }
77
+ if (Object.keys(byType).length > 0) {
78
+ bySchema[schemaName] = byType;
64
79
  }
65
80
  }
66
- return Object.keys(entries).length > 0 ? entries : undefined;
81
+ return Object.keys(bySchema).length > 0 ? bySchema : undefined;
67
82
  }
68
83
 
69
84
  /**
@@ -78,25 +93,13 @@ export function readPostgresSchemaIrAnnotations(schema: SqlSchemaIR): PostgresSc
78
93
  return {};
79
94
  }
80
95
  const schemaField = readOptionalString(Reflect.get(raw, 'schema'));
81
- const storageTypes = readPgStorageTypesMap(Reflect.get(raw, 'storageTypes'));
96
+ const enumTypes = readPgEnumTypesMap(Reflect.get(raw, 'enumTypes'));
82
97
  return {
83
98
  ...(schemaField !== undefined ? { schema: schemaField } : {}),
84
- ...(storageTypes !== undefined ? { storageTypes } : {}),
99
+ ...(enumTypes !== undefined ? { enumTypes } : {}),
85
100
  };
86
101
  }
87
102
 
88
- /**
89
- * Separator for `(schemaName, nativeType)` keys in introspected
90
- * `schema.annotations.pg.storageTypes`. NUL cannot appear in Postgres
91
- * identifiers, so the pair is unambiguous.
92
- */
93
- export const ENUM_STORAGE_KEY_SEP = '\u0000';
94
-
95
- /** Builds the schema-qualified storageTypes map key for a live Postgres enum. */
96
- export function enumStorageCompoundKey(schemaName: string, nativeType: string): string {
97
- return `${schemaName}${ENUM_STORAGE_KEY_SEP}${nativeType}`;
98
- }
99
-
100
103
  /**
101
104
  * Resolves the live-schema name a namespace's enums are introspected under,
102
105
  * for keying `readExistingEnumValues` lookups. The unbound namespace's
@@ -149,9 +152,10 @@ export type EnumDiff =
149
152
 
150
153
  /**
151
154
  * Reads existing enum values for `(schemaName, nativeType)` from the
152
- * Postgres-introspected `schema.annotations.pg.storageTypes` map.
155
+ * Postgres-introspected `schema.annotations.pg.enumTypes` map, addressed by
156
+ * the `(schema, nativeType)` coordinate.
153
157
  *
154
- * Schema IR's `storageTypes` slots are always codec-typed
158
+ * Schema IR's enum entries are always codec-typed
155
159
  * (`{codecId: PG_ENUM_CODEC_ID, typeParams.values}`): the introspector
156
160
  * writes that shape, and the Contract→Schema IR projector resolves
157
161
  * `PostgresEnumType` instances down to the same codec-typed triple before
@@ -165,8 +169,8 @@ export function readExistingEnumValues(
165
169
  schemaName: string,
166
170
  nativeType: string,
167
171
  ): readonly string[] | null {
168
- const storageTypes = readPostgresSchemaIrAnnotations(schema).storageTypes;
169
- const existing = storageTypes?.[enumStorageCompoundKey(schemaName, nativeType)];
172
+ const enumTypes = readPostgresSchemaIrAnnotations(schema).enumTypes;
173
+ const existing = enumTypes?.[schemaName]?.[nativeType];
170
174
  if (!existing || existing.codecId !== PG_ENUM_CODEC_ID) {
171
175
  return null;
172
176
  }
@@ -8,7 +8,7 @@
8
8
  * through `mapIssueToCall` for the default case.
9
9
  */
10
10
 
11
- import type { Contract } from '@prisma-next/contract/types';
11
+ import type { Contract, JsonValue } from '@prisma-next/contract/types';
12
12
  import type {
13
13
  CodecControlHooks,
14
14
  MigrationOperationPolicy,
@@ -25,9 +25,11 @@ import type {
25
25
  StorageTable,
26
26
  StorageTypeInstance,
27
27
  } from '@prisma-next/sql-contract/types';
28
- import type { DdlColumn, DdlTableConstraint } from '@prisma-next/sql-relational-core/ast';
28
+ import type { CodecRef, DdlColumn, DdlTableConstraint } from '@prisma-next/sql-relational-core/ast';
29
29
  import * as contractFree from '@prisma-next/sql-relational-core/contract-free';
30
30
  import type { SqlSchemaIR } from '@prisma-next/sql-schema-ir/types';
31
+ import { blindCast } from '@prisma-next/utils/casts';
32
+ import { ifDefined } from '@prisma-next/utils/defined';
31
33
  import type { Result } from '@prisma-next/utils/result';
32
34
  import { notOk, ok } from '@prisma-next/utils/result';
33
35
  import { PostgresEnumType } from '../postgres-enum-type';
@@ -65,6 +67,7 @@ import {
65
67
  type StrategyContext,
66
68
  tableAt,
67
69
  } from './planner-strategies';
70
+ import { resolveColumnTypeMetadata } from './planner-type-resolution';
68
71
 
69
72
  export type { CallMigrationStrategy, StrategyContext };
70
73
 
@@ -214,9 +217,27 @@ function toDdlColumn(
214
217
  ): DdlColumn {
215
218
  const typeSql = buildColumnTypeSql(column, codecHooks, storageTypes);
216
219
  const ddlDefault = postgresDefaultToDdlColumnDefault(column.default);
220
+ const resolved = resolveColumnTypeMetadata(
221
+ column,
222
+ storageTypes as Record<string, StorageTypeInstance | PostgresEnumStorageEntry>,
223
+ );
224
+ const codecRef: CodecRef | undefined = resolved.codecId
225
+ ? {
226
+ codecId: resolved.codecId,
227
+ ...(resolved.typeParams !== undefined
228
+ ? {
229
+ typeParams: blindCast<
230
+ JsonValue,
231
+ 'resolved.typeParams is JsonValue-shaped storage metadata; the narrowed (non-undefined) value lands in CodecRef.typeParams which is JsonValue'
232
+ >(resolved.typeParams),
233
+ }
234
+ : {}),
235
+ }
236
+ : undefined;
217
237
  return contractFree.col(name, typeSql, {
218
238
  ...(!column.nullable ? { notNull: true } : {}),
219
- ...(ddlDefault ? { default: ddlDefault } : {}),
239
+ ...ifDefined('default', ddlDefault),
240
+ ...ifDefined('codecRef', codecRef),
220
241
  });
221
242
  }
222
243
 
@@ -22,7 +22,7 @@
22
22
 
23
23
  import { errorUnfilledPlaceholder } from '@prisma-next/errors/migration';
24
24
  import type { SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';
25
- import type { Lowerer } from '@prisma-next/family-sql/control-adapter';
25
+ import type { ExecuteRequestLowerer, Lowerer } from '@prisma-next/family-sql/control-adapter';
26
26
  import type {
27
27
  OpFactoryCall as FrameworkOpFactoryCall,
28
28
  MigrationOperationClass,
@@ -36,6 +36,7 @@ import type {
36
36
  import { FunctionColumnDefault, LiteralColumnDefault } from '@prisma-next/sql-relational-core/ast';
37
37
  import { type ImportRequirement, jsonToTsSource, TsExpression } from '@prisma-next/ts-render';
38
38
  import { blindCast } from '@prisma-next/utils/casts';
39
+ import { ifDefined } from '@prisma-next/utils/defined';
39
40
  import * as contractFreeDdl from '../../contract-free/ddl';
40
41
  import { escapeLiteral, quoteIdentifier } from '../sql-utils';
41
42
  import type { PostgresColumnDefault } from '../types';
@@ -85,7 +86,7 @@ abstract class PostgresOpFactoryCallNode extends TsExpression implements Framewo
85
86
  abstract readonly factoryName: string;
86
87
  abstract readonly operationClass: MigrationOperationClass;
87
88
  abstract readonly label: string;
88
- abstract toOp(lowerer?: Lowerer): Op;
89
+ abstract toOp(lowerer?: Lowerer): Op | Promise<Op>;
89
90
 
90
91
  importRequirements(): readonly ImportRequirement[] {
91
92
  return [{ moduleSpecifier: POSTGRES_MIGRATION_FACADE, symbol: this.factoryName }];
@@ -213,7 +214,7 @@ export class CreateTableCall extends PostgresOpFactoryCallNode {
213
214
  this.freeze();
214
215
  }
215
216
 
216
- toOp(lowerer?: Lowerer): Op {
217
+ async toOp(lowerer?: ExecuteRequestLowerer): Promise<Op> {
217
218
  if (lowerer === undefined) {
218
219
  throw new Error(
219
220
  `CreateTableCall.toOp: a DDL lowerer is required on the Postgres planner path (table "${this.tableName}"). Pass the control adapter to createPostgresMigrationPlanner.`,
@@ -223,9 +224,9 @@ export class CreateTableCall extends PostgresOpFactoryCallNode {
223
224
  ...(this.schemaName !== UNBOUND_NAMESPACE_ID ? { schema: this.schemaName } : {}),
224
225
  table: this.tableName,
225
226
  columns: this.columns,
226
- ...(this.constraints ? { constraints: this.constraints } : {}),
227
+ ...ifDefined('constraints', this.constraints),
227
228
  });
228
- const { sql } = lowerer.lower(ddlNode, { contract: {} });
229
+ const statement = await lowerer.lowerToExecuteRequest(ddlNode);
229
230
  const schemaName = this.schemaName;
230
231
  const tableName = this.tableName;
231
232
  return {
@@ -240,7 +241,13 @@ export class CreateTableCall extends PostgresOpFactoryCallNode {
240
241
  `SELECT to_regclass(${toRegclassLiteral(schemaName, tableName)}) IS NULL`,
241
242
  ),
242
243
  ],
243
- execute: [step(`create table "${tableName}"`, sql)],
244
+ execute: [
245
+ {
246
+ description: `create table "${tableName}"`,
247
+ sql: statement.sql,
248
+ params: statement.params ?? [],
249
+ },
250
+ ],
244
251
  postcheck: [
245
252
  step(
246
253
  `verify table "${tableName}" exists`,
@@ -1016,14 +1023,14 @@ export class CreateSchemaCall extends PostgresOpFactoryCallNode {
1016
1023
  this.freeze();
1017
1024
  }
1018
1025
 
1019
- toOp(lowerer?: Lowerer): Op {
1026
+ async toOp(lowerer?: ExecuteRequestLowerer): Promise<Op> {
1020
1027
  if (lowerer === undefined) {
1021
1028
  throw new Error(
1022
1029
  `CreateSchemaCall.toOp: a DDL lowerer is required on the Postgres planner path (schema "${this.schemaName}"). Pass the control adapter to createPostgresMigrationPlanner.`,
1023
1030
  );
1024
1031
  }
1025
1032
  const ddlNode = contractFreeDdl.createSchema({ schema: this.schemaName, ifNotExists: true });
1026
- const { sql } = lowerer.lower(ddlNode, { contract: {} });
1033
+ const statement = await lowerer.lowerToExecuteRequest(ddlNode);
1027
1034
  const schemaName = this.schemaName;
1028
1035
  return {
1029
1036
  id: `schema.${schemaName}`,
@@ -1031,7 +1038,13 @@ export class CreateSchemaCall extends PostgresOpFactoryCallNode {
1031
1038
  operationClass: 'additive',
1032
1039
  target: { id: 'postgres' },
1033
1040
  precheck: [],
1034
- execute: [step(`Create schema "${schemaName}"`, sql)],
1041
+ execute: [
1042
+ {
1043
+ description: `Create schema "${schemaName}"`,
1044
+ sql: statement.sql,
1045
+ params: statement.params ?? [],
1046
+ },
1047
+ ],
1035
1048
  postcheck: [],
1036
1049
  };
1037
1050
  }
@@ -61,8 +61,8 @@ import type {
61
61
  SqlMigrationPlanOperationStep,
62
62
  } from '@prisma-next/family-sql/control';
63
63
  import type { SqlControlAdapter } from '@prisma-next/family-sql/control-adapter';
64
- import type { SerializedQueryPlan } from '@prisma-next/framework-components/control';
65
64
  import type { SqlStorage } from '@prisma-next/sql-contract/types';
65
+ import type { SqlExecuteRequest } from '@prisma-next/sql-relational-core/ast';
66
66
  import type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';
67
67
  import { ifDefined } from '@prisma-next/utils/defined';
68
68
  import type { PostgresPlanTargetDetails } from '../planner-target-details';
@@ -96,25 +96,29 @@ export interface DataTransformOptions {
96
96
  readonly run: DataTransformClosure | readonly DataTransformClosure[];
97
97
  }
98
98
 
99
- export function dataTransform<TContract extends Contract<SqlStorage>>(
99
+ export async function dataTransform<TContract extends Contract<SqlStorage>>(
100
100
  contract: TContract,
101
101
  name: string,
102
102
  options: DataTransformOptions,
103
103
  adapter: SqlControlAdapter<'postgres'>,
104
- ): SqlMigrationPlanOperation<PostgresPlanTargetDetails> {
104
+ ): Promise<SqlMigrationPlanOperation<PostgresPlanTargetDetails>> {
105
105
  const runClosures: readonly DataTransformClosure[] = Array.isArray(options.run)
106
106
  ? options.run
107
107
  : [options.run as DataTransformClosure];
108
108
 
109
- const checkPlan = options.check ? invokeAndLower(options.check, contract, adapter, name) : null;
110
- const runPlans = runClosures.map((closure) => invokeAndLower(closure, contract, adapter, name));
109
+ const checkPlan = options.check
110
+ ? await invokeAndLower(options.check, contract, adapter, name)
111
+ : null;
112
+ const runPlans = await Promise.all(
113
+ runClosures.map((closure) => invokeAndLower(closure, contract, adapter, name)),
114
+ );
111
115
 
112
116
  const precheck: readonly SqlMigrationPlanOperationStep[] = checkPlan
113
117
  ? [
114
118
  {
115
119
  description: `Check ${name} has work to do`,
116
120
  sql: `SELECT EXISTS (${checkPlan.sql}) AS ok`,
117
- params: checkPlan.params,
121
+ params: checkPlan.params ?? [],
118
122
  },
119
123
  ]
120
124
  : [];
@@ -122,7 +126,7 @@ export function dataTransform<TContract extends Contract<SqlStorage>>(
122
126
  const execute: readonly SqlMigrationPlanOperationStep[] = runPlans.map((plan) => ({
123
127
  description: `Run ${name}`,
124
128
  sql: plan.sql,
125
- params: plan.params,
129
+ params: plan.params ?? [],
126
130
  }));
127
131
 
128
132
  const postcheck: readonly SqlMigrationPlanOperationStep[] = checkPlan
@@ -130,7 +134,7 @@ export function dataTransform<TContract extends Contract<SqlStorage>>(
130
134
  {
131
135
  description: `Verify ${name} resolved all violations`,
132
136
  sql: `SELECT NOT EXISTS (${checkPlan.sql}) AS ok`,
133
- params: checkPlan.params,
137
+ params: checkPlan.params ?? [],
134
138
  },
135
139
  ]
136
140
  : [];
@@ -147,23 +151,16 @@ export function dataTransform<TContract extends Contract<SqlStorage>>(
147
151
  };
148
152
  }
149
153
 
150
- function invokeAndLower(
154
+ async function invokeAndLower(
151
155
  closure: DataTransformClosure,
152
156
  contract: Contract<SqlStorage>,
153
157
  adapter: SqlControlAdapter<'postgres'>,
154
158
  name: string,
155
- ): SerializedQueryPlan {
159
+ ): Promise<SqlExecuteRequest> {
156
160
  const result = closure();
157
161
  const plan = isBuildable(result) ? result.build() : result;
158
162
  assertContractMatches(plan, contract, name);
159
- const lowered = adapter.lower(plan.ast, { contract });
160
- const params = lowered.params.map((slot) => {
161
- if (slot.kind === 'literal') return slot.value;
162
- throw new Error(
163
- `data-transform: bind-site slot '${slot.name}' is not allowed in migration plans`,
164
- );
165
- });
166
- return { sql: lowered.sql, params };
163
+ return adapter.lowerToExecuteRequest(plan.ast, { contract });
167
164
  }
168
165
 
169
166
  function isBuildable(value: unknown): value is Buildable {
@@ -24,7 +24,7 @@
24
24
  */
25
25
 
26
26
  import type { SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';
27
- import type { Lowerer } from '@prisma-next/family-sql/control-adapter';
27
+ import type { ExecuteRequestLowerer } from '@prisma-next/family-sql/control-adapter';
28
28
  import type {
29
29
  MigrationPlanWithAuthoringSurface,
30
30
  OpFactoryCall,
@@ -35,8 +35,6 @@ import { PostgresMigration } from './postgres-migration';
35
35
  import { renderOps } from './render-ops';
36
36
  import { renderCallsToTypeScript } from './render-typescript';
37
37
 
38
- type Op = SqlMigrationPlanOperation<PostgresPlanTargetDetails>;
39
-
40
38
  export class TypeScriptRenderablePostgresMigration
41
39
  extends PostgresMigration
42
40
  implements MigrationPlanWithAuthoringSurface
@@ -44,13 +42,19 @@ export class TypeScriptRenderablePostgresMigration
44
42
  readonly #calls: readonly OpFactoryCall[];
45
43
  readonly #meta: MigrationMeta;
46
44
  readonly #spaceId: string;
47
- readonly #lowerer: Lowerer | undefined;
45
+ readonly #lowerer: ExecuteRequestLowerer | undefined;
46
+ #operationsCache:
47
+ | readonly (
48
+ | SqlMigrationPlanOperation<PostgresPlanTargetDetails>
49
+ | Promise<SqlMigrationPlanOperation<PostgresPlanTargetDetails>>
50
+ )[]
51
+ | undefined;
48
52
 
49
53
  constructor(
50
54
  calls: readonly OpFactoryCall[],
51
55
  meta: MigrationMeta,
52
56
  spaceId: string,
53
- lowerer?: Lowerer,
57
+ lowerer?: ExecuteRequestLowerer,
54
58
  ) {
55
59
  super();
56
60
  this.#calls = calls;
@@ -59,8 +63,12 @@ export class TypeScriptRenderablePostgresMigration
59
63
  this.#lowerer = lowerer;
60
64
  }
61
65
 
62
- override get operations(): readonly Op[] {
63
- return renderOps(this.#calls, this.#lowerer);
66
+ override get operations(): readonly (
67
+ | SqlMigrationPlanOperation<PostgresPlanTargetDetails>
68
+ | Promise<SqlMigrationPlanOperation<PostgresPlanTargetDetails>>
69
+ )[] {
70
+ this.#operationsCache ??= renderOps(this.#calls, this.#lowerer);
71
+ return this.#operationsCache;
64
72
  }
65
73
 
66
74
  override describe(): MigrationMeta {
@@ -12,7 +12,7 @@ import {
12
12
  planFieldEventOperations,
13
13
  plannerFailure,
14
14
  } from '@prisma-next/family-sql/control';
15
- import type { Lowerer } from '@prisma-next/family-sql/control-adapter';
15
+ import type { ExecuteRequestLowerer } from '@prisma-next/family-sql/control-adapter';
16
16
  import { verifySqlSchema } from '@prisma-next/family-sql/schema-verify';
17
17
  import type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';
18
18
  import type {
@@ -52,7 +52,9 @@ type VerifySqlSchemaOptionsWithComponents = Parameters<typeof verifySqlSchema>[0
52
52
  readonly frameworkComponents: PlannerFrameworkComponents;
53
53
  };
54
54
 
55
- export function createPostgresMigrationPlanner(lowerer: Lowerer): PostgresMigrationPlanner {
55
+ export function createPostgresMigrationPlanner(
56
+ lowerer: ExecuteRequestLowerer,
57
+ ): PostgresMigrationPlanner {
56
58
  return new PostgresMigrationPlanner(lowerer);
57
59
  }
58
60
 
@@ -88,9 +90,9 @@ export type PostgresPlanResult =
88
90
  * authoring surface.
89
91
  */
90
92
  export class PostgresMigrationPlanner implements MigrationPlanner<'sql', 'postgres'> {
91
- readonly #lowerer: Lowerer | undefined;
93
+ readonly #lowerer: ExecuteRequestLowerer | undefined;
92
94
 
93
- constructor(lowerer?: Lowerer) {
95
+ constructor(lowerer?: ExecuteRequestLowerer) {
94
96
  this.#lowerer = lowerer;
95
97
  }
96
98
 
@@ -64,7 +64,7 @@ export abstract class PostgresMigration extends SqlMigration<
64
64
  contract: TContract,
65
65
  name: string,
66
66
  options: DataTransformOptions,
67
- ): SqlMigrationPlanOperation<PostgresPlanTargetDetails> {
67
+ ): Promise<SqlMigrationPlanOperation<PostgresPlanTargetDetails>> {
68
68
  if (!this.controlAdapter) {
69
69
  throw errorPostgresMigrationStackMissing();
70
70
  }
@@ -82,7 +82,7 @@ export abstract class PostgresMigration extends SqlMigration<
82
82
  readonly ifNotExists?: boolean;
83
83
  readonly columns: readonly DdlColumn[];
84
84
  readonly constraints?: readonly DdlTableConstraint[];
85
- }): SqlMigrationPlanOperation<PostgresPlanTargetDetails> {
85
+ }): Promise<SqlMigrationPlanOperation<PostgresPlanTargetDetails>> {
86
86
  if (!this.controlAdapter) {
87
87
  throw errorPostgresMigrationStackMissing();
88
88
  }
@@ -102,7 +102,7 @@ export abstract class PostgresMigration extends SqlMigration<
102
102
  protected createSchema(options: {
103
103
  readonly schema: string;
104
104
  readonly ifNotExists?: boolean;
105
- }): SqlMigrationPlanOperation<PostgresPlanTargetDetails> {
105
+ }): Promise<SqlMigrationPlanOperation<PostgresPlanTargetDetails>> {
106
106
  if (!this.controlAdapter) {
107
107
  throw errorPostgresMigrationStackMissing();
108
108
  }
@@ -1,7 +1,11 @@
1
1
  import type { SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';
2
- import type { Lowerer } from '@prisma-next/family-sql/control-adapter';
3
- import type { OpFactoryCall } from '@prisma-next/framework-components/control';
2
+ import type { ExecuteRequestLowerer } from '@prisma-next/family-sql/control-adapter';
3
+ import type {
4
+ MigrationPlanOperation,
5
+ OpFactoryCall,
6
+ } from '@prisma-next/framework-components/control';
4
7
  import { blindCast } from '@prisma-next/utils/casts';
8
+ import { isThenable } from '@prisma-next/utils/promise';
5
9
  import type { PostgresPlanTargetDetails } from './planner-target-details';
6
10
 
7
11
  type Op = SqlMigrationPlanOperation<PostgresPlanTargetDetails>;
@@ -13,11 +17,11 @@ type Op = SqlMigrationPlanOperation<PostgresPlanTargetDetails>;
13
17
  * silently flow through to postgres-shaped renderers — exactly the
14
18
  * place to fail loudly with op metadata (`id` + `target.id`).
15
19
  */
16
- function assertPostgresOp(
17
- op: ReturnType<OpFactoryCall['toOp']>,
18
- callFactoryName: string,
19
- ): asserts op is Op {
20
- const targetId = (op as Partial<Op>).target?.id;
20
+ function assertPostgresOp(op: MigrationPlanOperation, callFactoryName: string): asserts op is Op {
21
+ const targetId = blindCast<
22
+ { target?: { id?: string } },
23
+ 'op.target is present on concrete SqlMigrationPlanOperation but absent on the framework MigrationPlanOperation base'
24
+ >(op).target?.id;
21
25
  if (targetId !== 'postgres') {
22
26
  throw new Error(
23
27
  `renderOps: expected postgres op but got target.id="${String(targetId)}" for op.id="${op.id}" (factoryName="${callFactoryName}"). An OpFactoryCall produced an op for a different target on the postgres planner path; check the call's target binding.`,
@@ -25,13 +29,22 @@ function assertPostgresOp(
25
29
  }
26
30
  }
27
31
 
28
- export function renderOps(calls: readonly OpFactoryCall[], lowerer?: Lowerer): Op[] {
32
+ export function renderOps(
33
+ calls: readonly OpFactoryCall[],
34
+ lowerer?: ExecuteRequestLowerer,
35
+ ): (Op | Promise<Op>)[] {
29
36
  return calls.map((c) => {
30
- const op = blindCast<
31
- { toOp(lowerer?: Lowerer): ReturnType<OpFactoryCall['toOp']> },
32
- 'PG OpFactoryCall.toOp accepts an optional Lowerer; the framework interface omits it because not all targets need a lowerer — the PG target overrides with this extended signature'
37
+ const opOrPromise = blindCast<
38
+ { toOp(lowerer?: ExecuteRequestLowerer): Op | Promise<Op> },
39
+ 'PG OpFactoryCall.toOp accepts an optional ExecuteRequestLowerer; the framework interface omits it because not all targets need a lowerer — the PG target overrides with this extended signature'
33
40
  >(c).toOp(lowerer);
34
- assertPostgresOp(op, c.factoryName);
35
- return op;
41
+ if (isThenable(opOrPromise)) {
42
+ return opOrPromise.then((op) => {
43
+ assertPostgresOp(op, c.factoryName);
44
+ return op;
45
+ });
46
+ }
47
+ assertPostgresOp(opOrPromise, c.factoryName);
48
+ return opOrPromise;
36
49
  });
37
50
  }