@prisma-next/target-postgres 0.4.1 → 0.5.0-dev.2

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 (178) hide show
  1. package/dist/codec-ids-CojIXVf9.mjs +29 -0
  2. package/dist/codec-ids-CojIXVf9.mjs.map +1 -0
  3. package/dist/codec-ids.d.mts +28 -0
  4. package/dist/codec-ids.d.mts.map +1 -0
  5. package/dist/codec-ids.mjs +3 -0
  6. package/dist/codec-types.d.mts +42 -0
  7. package/dist/codec-types.d.mts.map +1 -0
  8. package/dist/codec-types.mjs +3 -0
  9. package/dist/codecs-BoahtY_Q.mjs +385 -0
  10. package/dist/codecs-BoahtY_Q.mjs.map +1 -0
  11. package/dist/codecs-D-F2KJqt.d.mts +299 -0
  12. package/dist/codecs-D-F2KJqt.d.mts.map +1 -0
  13. package/dist/codecs.d.mts +2 -0
  14. package/dist/codecs.mjs +3 -0
  15. package/dist/control.d.mts +1 -1
  16. package/dist/control.mjs +9 -1982
  17. package/dist/control.mjs.map +1 -1
  18. package/dist/data-transform-CxFRBIUp.d.mts +32 -0
  19. package/dist/data-transform-CxFRBIUp.d.mts.map +1 -0
  20. package/dist/data-transform-VfEGzXWt.mjs +39 -0
  21. package/dist/data-transform-VfEGzXWt.mjs.map +1 -0
  22. package/dist/data-transform.d.mts +2 -0
  23. package/dist/data-transform.mjs +3 -0
  24. package/dist/default-normalizer-DNOpRoOF.mjs +131 -0
  25. package/dist/default-normalizer-DNOpRoOF.mjs.map +1 -0
  26. package/dist/default-normalizer.d.mts +19 -0
  27. package/dist/default-normalizer.d.mts.map +1 -0
  28. package/dist/default-normalizer.mjs +3 -0
  29. package/dist/{descriptor-meta-DkvCmY98.mjs → descriptor-meta-BVoVtyp-.mjs} +1 -1
  30. package/dist/{descriptor-meta-DkvCmY98.mjs.map → descriptor-meta-BVoVtyp-.mjs.map} +1 -1
  31. package/dist/errors-AFvEPZ1R.mjs +34 -0
  32. package/dist/errors-AFvEPZ1R.mjs.map +1 -0
  33. package/dist/errors.d.mts +27 -0
  34. package/dist/errors.d.mts.map +1 -0
  35. package/dist/errors.mjs +3 -0
  36. package/dist/issue-planner-CFjB0_oO.mjs +879 -0
  37. package/dist/issue-planner-CFjB0_oO.mjs.map +1 -0
  38. package/dist/issue-planner.d.mts +85 -0
  39. package/dist/issue-planner.d.mts.map +1 -0
  40. package/dist/issue-planner.mjs +3 -0
  41. package/dist/migration.d.mts +5 -79
  42. package/dist/migration.d.mts.map +1 -1
  43. package/dist/migration.mjs +6 -428
  44. package/dist/migration.mjs.map +1 -1
  45. package/dist/native-type-normalizer-CInai_oY.mjs +38 -0
  46. package/dist/native-type-normalizer-CInai_oY.mjs.map +1 -0
  47. package/dist/native-type-normalizer.d.mts +18 -0
  48. package/dist/native-type-normalizer.d.mts.map +1 -0
  49. package/dist/native-type-normalizer.mjs +3 -0
  50. package/dist/op-factory-call-BKlruaiC.mjs +605 -0
  51. package/dist/op-factory-call-BKlruaiC.mjs.map +1 -0
  52. package/dist/op-factory-call-C3bWXKSP.d.mts +304 -0
  53. package/dist/op-factory-call-C3bWXKSP.d.mts.map +1 -0
  54. package/dist/op-factory-call.d.mts +3 -0
  55. package/dist/op-factory-call.mjs +3 -0
  56. package/dist/pack.d.mts +1 -1
  57. package/dist/pack.mjs +1 -1
  58. package/dist/planner-CLUvVhUN.mjs +98 -0
  59. package/dist/planner-CLUvVhUN.mjs.map +1 -0
  60. package/dist/planner-ddl-builders-Dxvw1LHw.mjs +132 -0
  61. package/dist/planner-ddl-builders-Dxvw1LHw.mjs.map +1 -0
  62. package/dist/planner-ddl-builders.d.mts +22 -0
  63. package/dist/planner-ddl-builders.d.mts.map +1 -0
  64. package/dist/planner-ddl-builders.mjs +3 -0
  65. package/dist/planner-identity-values-Dju-o5GF.mjs +91 -0
  66. package/dist/planner-identity-values-Dju-o5GF.mjs.map +1 -0
  67. package/dist/planner-identity-values.d.mts +20 -0
  68. package/dist/planner-identity-values.d.mts.map +1 -0
  69. package/dist/planner-identity-values.mjs +3 -0
  70. package/dist/planner-produced-postgres-migration-CRRTno6Z.d.mts +20 -0
  71. package/dist/planner-produced-postgres-migration-CRRTno6Z.d.mts.map +1 -0
  72. package/dist/planner-produced-postgres-migration-DSSPq8QS.mjs +33 -0
  73. package/dist/planner-produced-postgres-migration-DSSPq8QS.mjs.map +1 -0
  74. package/dist/planner-produced-postgres-migration.d.mts +5 -0
  75. package/dist/planner-produced-postgres-migration.mjs +3 -0
  76. package/dist/planner-schema-lookup-B7lkypwn.mjs +29 -0
  77. package/dist/planner-schema-lookup-B7lkypwn.mjs.map +1 -0
  78. package/dist/planner-schema-lookup.d.mts +22 -0
  79. package/dist/planner-schema-lookup.d.mts.map +1 -0
  80. package/dist/planner-schema-lookup.mjs +3 -0
  81. package/dist/planner-sql-checks-7jkgm9TX.mjs +241 -0
  82. package/dist/planner-sql-checks-7jkgm9TX.mjs.map +1 -0
  83. package/dist/planner-sql-checks.d.mts +55 -0
  84. package/dist/planner-sql-checks.d.mts.map +1 -0
  85. package/dist/planner-sql-checks.mjs +3 -0
  86. package/dist/{planner-target-details-MXb3oeul.d.mts → planner-target-details-DH-azLu-.d.mts} +1 -1
  87. package/dist/{planner-target-details-MXb3oeul.d.mts.map → planner-target-details-DH-azLu-.d.mts.map} +1 -1
  88. package/dist/planner-target-details.d.mts +2 -0
  89. package/dist/planner-target-details.mjs +1 -0
  90. package/dist/planner.d.mts +68 -0
  91. package/dist/planner.d.mts.map +1 -0
  92. package/dist/planner.mjs +4 -0
  93. package/dist/postgres-migration-BjA3Zmts.d.mts +50 -0
  94. package/dist/postgres-migration-BjA3Zmts.d.mts.map +1 -0
  95. package/dist/postgres-migration-qtmtbONe.mjs +52 -0
  96. package/dist/postgres-migration-qtmtbONe.mjs.map +1 -0
  97. package/dist/render-ops-D6_DHdOK.mjs +8 -0
  98. package/dist/render-ops-D6_DHdOK.mjs.map +1 -0
  99. package/dist/render-ops.d.mts +11 -0
  100. package/dist/render-ops.d.mts.map +1 -0
  101. package/dist/render-ops.mjs +3 -0
  102. package/dist/render-typescript-1rF_SB4g.mjs +85 -0
  103. package/dist/render-typescript-1rF_SB4g.mjs.map +1 -0
  104. package/dist/render-typescript.d.mts +15 -0
  105. package/dist/render-typescript.d.mts.map +1 -0
  106. package/dist/render-typescript.mjs +3 -0
  107. package/dist/runtime.d.mts +15 -3
  108. package/dist/runtime.d.mts.map +1 -1
  109. package/dist/runtime.mjs +10 -1
  110. package/dist/runtime.mjs.map +1 -1
  111. package/dist/shared-Bxkt8pNO.d.mts +41 -0
  112. package/dist/shared-Bxkt8pNO.d.mts.map +1 -0
  113. package/dist/sql-utils-r-Lw535w.mjs +76 -0
  114. package/dist/sql-utils-r-Lw535w.mjs.map +1 -0
  115. package/dist/sql-utils.d.mts +59 -0
  116. package/dist/sql-utils.d.mts.map +1 -0
  117. package/dist/sql-utils.mjs +3 -0
  118. package/dist/statement-builders-BPnmt6wx.mjs +116 -0
  119. package/dist/statement-builders-BPnmt6wx.mjs.map +1 -0
  120. package/dist/statement-builders.d.mts +23 -0
  121. package/dist/statement-builders.d.mts.map +1 -0
  122. package/dist/statement-builders.mjs +3 -0
  123. package/dist/tables-BmdW_FWO.mjs +477 -0
  124. package/dist/tables-BmdW_FWO.mjs.map +1 -0
  125. package/dist/types-ClK03Ojd.d.mts +10 -0
  126. package/dist/types-ClK03Ojd.d.mts.map +1 -0
  127. package/dist/types.d.mts +2 -0
  128. package/dist/types.mjs +1 -0
  129. package/package.json +37 -20
  130. package/src/core/codec-ids.ts +30 -0
  131. package/src/core/codecs.ts +645 -0
  132. package/src/core/default-normalizer.ts +131 -0
  133. package/src/core/descriptor-meta.ts +1 -1
  134. package/src/core/errors.ts +33 -0
  135. package/src/core/json-schema-type-expression.ts +131 -0
  136. package/src/core/migrations/op-factory-call.ts +1 -5
  137. package/src/core/migrations/operations/columns.ts +1 -1
  138. package/src/core/migrations/operations/constraints.ts +1 -1
  139. package/src/core/migrations/operations/data-transform.ts +27 -21
  140. package/src/core/migrations/operations/dependencies.ts +1 -1
  141. package/src/core/migrations/operations/enums.ts +1 -1
  142. package/src/core/migrations/operations/indexes.ts +1 -1
  143. package/src/core/migrations/operations/shared.ts +1 -1
  144. package/src/core/migrations/operations/tables.ts +1 -1
  145. package/src/core/migrations/planner-ddl-builders.ts +1 -1
  146. package/src/core/migrations/planner-recipes.ts +1 -1
  147. package/src/core/migrations/planner-sql-checks.ts +1 -1
  148. package/src/core/migrations/planner.ts +2 -4
  149. package/src/core/migrations/postgres-migration.ts +54 -1
  150. package/src/core/migrations/render-typescript.ts +22 -12
  151. package/src/core/migrations/runner.ts +2 -4
  152. package/src/core/native-type-normalizer.ts +49 -0
  153. package/src/core/sql-utils.ts +104 -0
  154. package/src/exports/codec-ids.ts +1 -0
  155. package/src/exports/codec-types.ts +51 -0
  156. package/src/exports/codecs.ts +2 -0
  157. package/src/exports/data-transform.ts +1 -0
  158. package/src/exports/default-normalizer.ts +1 -0
  159. package/src/exports/errors.ts +1 -0
  160. package/src/exports/issue-planner.ts +1 -0
  161. package/src/exports/migration.ts +6 -0
  162. package/src/exports/native-type-normalizer.ts +1 -0
  163. package/src/exports/op-factory-call.ts +25 -0
  164. package/src/exports/planner-ddl-builders.ts +8 -0
  165. package/src/exports/planner-identity-values.ts +1 -0
  166. package/src/exports/planner-produced-postgres-migration.ts +1 -0
  167. package/src/exports/planner-schema-lookup.ts +6 -0
  168. package/src/exports/planner-sql-checks.ts +11 -0
  169. package/src/exports/planner-target-details.ts +1 -0
  170. package/src/exports/planner.ts +1 -0
  171. package/src/exports/render-ops.ts +1 -0
  172. package/src/exports/render-typescript.ts +1 -0
  173. package/src/exports/runtime.ts +19 -4
  174. package/src/exports/sql-utils.ts +7 -0
  175. package/src/exports/statement-builders.ts +7 -0
  176. package/src/exports/types.ts +1 -0
  177. package/dist/postgres-migration-BsHJHV9O.mjs +0 -2793
  178. package/dist/postgres-migration-BsHJHV9O.mjs.map +0 -1
@@ -0,0 +1,131 @@
1
+ import type { ColumnDefault } from '@prisma-next/contract/types';
2
+
3
+ /**
4
+ * Pre-compiled regex patterns for performance.
5
+ * These are compiled once at module load time rather than on each function call.
6
+ */
7
+ const NEXTVAL_PATTERN = /^nextval\s*\(/i;
8
+ const NOW_FUNCTION_PATTERN = /^(now\s*\(\s*\)|CURRENT_TIMESTAMP)$/i;
9
+ const CLOCK_TIMESTAMP_PATTERN = /^clock_timestamp\s*\(\s*\)$/i;
10
+ const TIMESTAMP_CAST_SUFFIX = /::timestamp(?:tz|\s+(?:with|without)\s+time\s+zone)?$/i;
11
+ const TEXT_CAST_SUFFIX = /::text$/i;
12
+ const NOW_LITERAL_PATTERN = /^'now'$/i;
13
+ const UUID_PATTERN = /^gen_random_uuid\s*\(\s*\)$/i;
14
+ const UUID_OSSP_PATTERN = /^uuid_generate_v4\s*\(\s*\)$/i;
15
+ const NULL_PATTERN = /^NULL(?:::.+)?$/i;
16
+ const TRUE_PATTERN = /^true$/i;
17
+ const FALSE_PATTERN = /^false$/i;
18
+ const NUMERIC_PATTERN = /^-?\d+(\.\d+)?$/;
19
+ const STRING_LITERAL_PATTERN = /^'((?:[^']|'')*)'(?:::(?:"[^"]+"|[\w\s]+)(?:\(\d+\))?)?$/;
20
+
21
+ /**
22
+ * Returns the canonical expression for a timestamp default function, or undefined
23
+ * if the expression is not a recognized timestamp default.
24
+ *
25
+ * Keeps now()/CURRENT_TIMESTAMP and clock_timestamp() distinct:
26
+ * - now(), CURRENT_TIMESTAMP, ('now'::text)::timestamp... → 'now()'
27
+ * - clock_timestamp(), clock_timestamp()::timestamptz → 'clock_timestamp()'
28
+ *
29
+ * These are semantically different in Postgres: now() returns the transaction
30
+ * start time (constant within a transaction), while clock_timestamp() returns
31
+ * the actual wall-clock time (can differ across rows in a single INSERT).
32
+ */
33
+ function canonicalizeTimestampDefault(expr: string): string | undefined {
34
+ if (NOW_FUNCTION_PATTERN.test(expr)) return 'now()';
35
+ if (CLOCK_TIMESTAMP_PATTERN.test(expr)) return 'clock_timestamp()';
36
+
37
+ if (!TIMESTAMP_CAST_SUFFIX.test(expr)) return undefined;
38
+
39
+ let inner = expr.replace(TIMESTAMP_CAST_SUFFIX, '').trim();
40
+
41
+ if (inner.startsWith('(') && inner.endsWith(')')) {
42
+ inner = inner.slice(1, -1).trim();
43
+ }
44
+
45
+ if (NOW_FUNCTION_PATTERN.test(inner)) return 'now()';
46
+ if (CLOCK_TIMESTAMP_PATTERN.test(inner)) return 'clock_timestamp()';
47
+
48
+ inner = inner.replace(TEXT_CAST_SUFFIX, '').trim();
49
+ if (NOW_LITERAL_PATTERN.test(inner)) return 'now()';
50
+
51
+ return undefined;
52
+ }
53
+
54
+ /**
55
+ * Parses a raw Postgres column default expression into a normalized ColumnDefault.
56
+ * This enables semantic comparison between contract defaults and introspected schema defaults.
57
+ *
58
+ * Used by the migration diff layer to normalize raw database defaults during comparison,
59
+ * keeping the introspection layer focused on faithful data capture.
60
+ *
61
+ * @param rawDefault - Raw default expression from information_schema.columns.column_default
62
+ * @param nativeType - Native column type, used for type-aware parsing (bigint tagging, JSON detection)
63
+ * @returns Normalized ColumnDefault or undefined if the expression cannot be parsed
64
+ */
65
+ export function parsePostgresDefault(
66
+ rawDefault: string,
67
+ nativeType?: string,
68
+ ): ColumnDefault | undefined {
69
+ const trimmed = rawDefault.trim();
70
+ const normalizedType = nativeType?.toLowerCase();
71
+ const isBigInt = normalizedType === 'bigint' || normalizedType === 'int8';
72
+
73
+ if (NEXTVAL_PATTERN.test(trimmed)) {
74
+ return { kind: 'function', expression: 'autoincrement()' };
75
+ }
76
+
77
+ const canonicalTimestamp = canonicalizeTimestampDefault(trimmed);
78
+ if (canonicalTimestamp) {
79
+ return { kind: 'function', expression: canonicalTimestamp };
80
+ }
81
+
82
+ if (UUID_PATTERN.test(trimmed)) {
83
+ return { kind: 'function', expression: 'gen_random_uuid()' };
84
+ }
85
+
86
+ if (UUID_OSSP_PATTERN.test(trimmed)) {
87
+ return { kind: 'function', expression: 'gen_random_uuid()' };
88
+ }
89
+
90
+ if (NULL_PATTERN.test(trimmed)) {
91
+ return { kind: 'literal', value: null };
92
+ }
93
+
94
+ if (TRUE_PATTERN.test(trimmed)) {
95
+ return { kind: 'literal', value: true };
96
+ }
97
+ if (FALSE_PATTERN.test(trimmed)) {
98
+ return { kind: 'literal', value: false };
99
+ }
100
+
101
+ if (NUMERIC_PATTERN.test(trimmed)) {
102
+ const num = Number(trimmed);
103
+ if (!Number.isFinite(num)) return undefined;
104
+ if (isBigInt && !Number.isSafeInteger(num)) {
105
+ return { kind: 'literal', value: trimmed };
106
+ }
107
+ return { kind: 'literal', value: num };
108
+ }
109
+
110
+ const stringMatch = trimmed.match(STRING_LITERAL_PATTERN);
111
+ if (stringMatch?.[1] !== undefined) {
112
+ const unescaped = stringMatch[1].replace(/''/g, "'");
113
+ if (normalizedType === 'json' || normalizedType === 'jsonb') {
114
+ try {
115
+ return { kind: 'literal', value: JSON.parse(unescaped) };
116
+ } catch {
117
+ // Keep legacy behavior for malformed/non-JSON string content.
118
+ }
119
+ }
120
+ if (isBigInt && NUMERIC_PATTERN.test(unescaped)) {
121
+ const num = Number(unescaped);
122
+ if (Number.isSafeInteger(num)) {
123
+ return { kind: 'literal', value: num };
124
+ }
125
+ return { kind: 'literal', value: unescaped };
126
+ }
127
+ return { kind: 'literal', value: unescaped };
128
+ }
129
+
130
+ return { kind: 'function', expression: trimmed };
131
+ }
@@ -1,5 +1,5 @@
1
- import type { CodecTypes } from '@prisma-next/adapter-postgres/codec-types';
2
1
  import { postgresAuthoringFieldPresets, postgresAuthoringTypes } from './authoring';
2
+ import type { CodecTypes } from './codecs';
3
3
 
4
4
  const postgresTargetDescriptorMetaBase = {
5
5
  kind: 'target',
@@ -0,0 +1,33 @@
1
+ import { CliStructuredError } from '@prisma-next/errors/control';
2
+
3
+ /**
4
+ * A `PostgresMigration` instance method that needs the materialized control
5
+ * adapter (currently only `this.dataTransform(...)`) was invoked, but the
6
+ * migration was constructed without a `ControlStack`. Concrete authoring
7
+ * usage always goes through the migration CLI entrypoint, which assembles
8
+ * a stack from the loaded `prisma-next.config.ts`; reaching this error
9
+ * means a test fixture or ad-hoc consumer instantiated `PostgresMigration`
10
+ * with the no-arg form (legal for `operations` / `describe` introspection
11
+ * only).
12
+ *
13
+ * Distinct from `PN-MIG-2001` (placeholder not filled) and `PN-MIG-2005`
14
+ * (data-transform query plan against wrong contract) because the missing
15
+ * input is the stack itself, not the per-operation contract.
16
+ *
17
+ * Lives in `@prisma-next/target-postgres/errors` rather than the shared
18
+ * framework migration errors module because the failure is target-specific:
19
+ * the contract it talks about (`PostgresMigration`, the Postgres control
20
+ * adapter, the Postgres-target stack) only exists in this package.
21
+ */
22
+ export function errorPostgresMigrationStackMissing(): CliStructuredError {
23
+ return new CliStructuredError(
24
+ '2007',
25
+ 'PostgresMigration.dataTransform requires a control adapter',
26
+ {
27
+ domain: 'MIG',
28
+ why: 'PostgresMigration.dataTransform was invoked on an instance constructed without a ControlStack. The stored controlAdapter is undefined, so dataTransform cannot lower its query plan.',
29
+ fix: 'Construct the migration via the migration CLI entrypoint (which assembles a ControlStack from the loaded prisma-next.config.ts), or pass a ControlStack containing a Postgres adapter to the migration constructor in test fixtures.',
30
+ meta: {},
31
+ },
32
+ );
33
+ }
@@ -0,0 +1,131 @@
1
+ type JsonSchemaRecord = Record<string, unknown>;
2
+
3
+ const MAX_DEPTH = 32;
4
+
5
+ function isRecord(value: unknown): value is JsonSchemaRecord {
6
+ return typeof value === 'object' && value !== null;
7
+ }
8
+
9
+ function escapeStringLiteral(str: string): string {
10
+ return str
11
+ .replace(/\\/g, '\\\\')
12
+ .replace(/'/g, "\\'")
13
+ .replace(/\n/g, '\\n')
14
+ .replace(/\r/g, '\\r');
15
+ }
16
+
17
+ function quotePropertyKey(key: string): string {
18
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : `'${escapeStringLiteral(key)}'`;
19
+ }
20
+
21
+ function renderLiteral(value: unknown): string {
22
+ if (typeof value === 'string') {
23
+ return `'${escapeStringLiteral(value)}'`;
24
+ }
25
+ if (typeof value === 'number' || typeof value === 'boolean') {
26
+ return String(value);
27
+ }
28
+ if (value === null) {
29
+ return 'null';
30
+ }
31
+ return 'unknown';
32
+ }
33
+
34
+ function renderUnion(items: readonly unknown[], depth: number): string {
35
+ const rendered = items.map((item) => render(item, depth));
36
+ return rendered.join(' | ');
37
+ }
38
+
39
+ function renderObjectType(schema: JsonSchemaRecord, depth: number): string {
40
+ const properties = isRecord(schema['properties']) ? schema['properties'] : {};
41
+ const required = Array.isArray(schema['required'])
42
+ ? new Set(schema['required'].filter((key): key is string => typeof key === 'string'))
43
+ : new Set<string>();
44
+ const keys = Object.keys(properties).sort((left, right) => left.localeCompare(right));
45
+
46
+ if (keys.length === 0) {
47
+ const additionalProperties = schema['additionalProperties'];
48
+ if (additionalProperties === true || additionalProperties === undefined) {
49
+ return 'Record<string, unknown>';
50
+ }
51
+ return `Record<string, ${render(additionalProperties, depth)}>`;
52
+ }
53
+
54
+ const renderedProperties = keys.map((key) => {
55
+ const valueSchema = (properties as JsonSchemaRecord)[key];
56
+ const optionalMarker = required.has(key) ? '' : '?';
57
+ return `${quotePropertyKey(key)}${optionalMarker}: ${render(valueSchema, depth)}`;
58
+ });
59
+
60
+ return `{ ${renderedProperties.join('; ')} }`;
61
+ }
62
+
63
+ function renderArrayType(schema: JsonSchemaRecord, depth: number): string {
64
+ if (Array.isArray(schema['items'])) {
65
+ return `readonly [${schema['items'].map((item) => render(item, depth)).join(', ')}]`;
66
+ }
67
+
68
+ if (schema['items'] !== undefined) {
69
+ const itemType = render(schema['items'], depth);
70
+ const needsParens = itemType.includes(' | ') || itemType.includes(' & ');
71
+ return needsParens ? `(${itemType})[]` : `${itemType}[]`;
72
+ }
73
+
74
+ return 'unknown[]';
75
+ }
76
+
77
+ function render(schema: unknown, depth: number): string {
78
+ if (depth > MAX_DEPTH || !isRecord(schema)) {
79
+ return 'JsonValue';
80
+ }
81
+
82
+ const nextDepth = depth + 1;
83
+
84
+ if ('const' in schema) {
85
+ return renderLiteral(schema['const']);
86
+ }
87
+
88
+ if (Array.isArray(schema['enum'])) {
89
+ return schema['enum'].map((value) => renderLiteral(value)).join(' | ');
90
+ }
91
+
92
+ if (Array.isArray(schema['oneOf'])) {
93
+ return renderUnion(schema['oneOf'], nextDepth);
94
+ }
95
+
96
+ if (Array.isArray(schema['anyOf'])) {
97
+ return renderUnion(schema['anyOf'], nextDepth);
98
+ }
99
+
100
+ if (Array.isArray(schema['allOf'])) {
101
+ return schema['allOf'].map((item) => render(item, nextDepth)).join(' & ');
102
+ }
103
+
104
+ if (Array.isArray(schema['type'])) {
105
+ return schema['type'].map((item) => render({ ...schema, type: item }, nextDepth)).join(' | ');
106
+ }
107
+
108
+ switch (schema['type']) {
109
+ case 'string':
110
+ return 'string';
111
+ case 'number':
112
+ case 'integer':
113
+ return 'number';
114
+ case 'boolean':
115
+ return 'boolean';
116
+ case 'null':
117
+ return 'null';
118
+ case 'array':
119
+ return renderArrayType(schema, nextDepth);
120
+ case 'object':
121
+ return renderObjectType(schema, nextDepth);
122
+ default:
123
+ break;
124
+ }
125
+
126
+ return 'JsonValue';
127
+ }
128
+
129
+ export function renderTypeScriptTypeFromJsonSchema(schema: unknown): string {
130
+ return render(schema, 0);
131
+ }
@@ -793,7 +793,7 @@ export class DataTransformCall extends PostgresOpFactoryCallNode {
793
793
 
794
794
  renderTypeScript(): string {
795
795
  return [
796
- `dataTransform(endContract, ${jsonToTsSource(this.label)}, {`,
796
+ `this.dataTransform(endContract, ${jsonToTsSource(this.label)}, {`,
797
797
  ` check: () => placeholder(${jsonToTsSource(this.checkSlot)}),`,
798
798
  ` run: () => placeholder(${jsonToTsSource(this.runSlot)}),`,
799
799
  '})',
@@ -802,10 +802,6 @@ export class DataTransformCall extends PostgresOpFactoryCallNode {
802
802
 
803
803
  override importRequirements(): readonly ImportRequirement[] {
804
804
  return [
805
- { moduleSpecifier: TARGET_MIGRATION_MODULE, symbol: this.factoryName },
806
- // `placeholder` is re-exported from `@prisma-next/target-postgres/migration`
807
- // so the user's migration.ts only depends on a single migration-authoring
808
- // entrypoint.
809
805
  { moduleSpecifier: TARGET_MIGRATION_MODULE, symbol: 'placeholder' },
810
806
  {
811
807
  moduleSpecifier: './end-contract.json',
@@ -1,4 +1,4 @@
1
- import { quoteIdentifier } from '@prisma-next/adapter-postgres/control';
1
+ import { quoteIdentifier } from '../../sql-utils';
2
2
  import {
3
3
  columnDefaultExistsCheck,
4
4
  columnExistsCheck,
@@ -1,5 +1,5 @@
1
- import { quoteIdentifier } from '@prisma-next/adapter-postgres/control';
2
1
  import type { ReferentialAction } from '@prisma-next/sql-contract/types';
2
+ import { quoteIdentifier } from '../../sql-utils';
3
3
  import { constraintExistsCheck, qualifyTableName } from '../planner-sql-checks';
4
4
  import { type ForeignKeySpec, type Op, step, targetDetails } from './shared';
5
5
 
@@ -1,34 +1,42 @@
1
1
  /**
2
2
  * User-facing `dataTransform` factory for the Postgres migration authoring
3
- * surface. Invoked directly inside a `migration.ts` file:
3
+ * surface. Invoked directly inside a `migration.ts` file via the
4
+ * `PostgresMigration` instance method (`this.dataTransform(...)`), which
5
+ * supplies the control adapter from the migration's injected stack:
4
6
  *
5
7
  * ```ts
6
8
  * import endContract from './end-contract.json' with { type: 'json' };
7
- * import { dataTransform } from '@prisma-next/target-postgres/migration';
8
9
  *
9
- * dataTransform(endContract, 'backfill emails', {
10
- * check: () => db.users.count().where(({ email }) => email.isNull()),
11
- * run: () => db.users.update({ email: '' }).where(({ email }) => email.isNull()),
12
- * });
10
+ * class M extends Migration {
11
+ * override get operations() {
12
+ * return [
13
+ * this.dataTransform(endContract, 'backfill emails', {
14
+ * check: () => db.users.count().where(({ email }) => email.isNull()),
15
+ * run: () => db.users.update({ email: '' }).where(({ email }) => email.isNull()),
16
+ * }),
17
+ * ];
18
+ * }
19
+ * }
13
20
  * ```
14
21
  *
15
22
  * The factory accepts lazy closures (`() => SqlQueryPlan | Buildable`),
16
23
  * invokes each one, asserts that its `meta.storageHash` matches the
17
24
  * `contract` it was handed (→ `PN-MIG-2005` on mismatch), and lowers the
18
- * plan via the Postgres adapter to a serialized `{sql, params}` payload
19
- * for `ops.json`.
25
+ * plan via the supplied control adapter to a serialized `{sql, params}`
26
+ * payload for `ops.json`. The free factory remains usable standalone
27
+ * (tests, ad-hoc tooling, non-class contexts) by passing the adapter
28
+ * explicitly as the fourth argument.
20
29
  */
21
30
 
22
- import { createPostgresAdapter } from '@prisma-next/adapter-postgres/adapter';
23
31
  import type { Contract } from '@prisma-next/contract/types';
24
32
  import { errorDataTransformContractMismatch } from '@prisma-next/errors/migration';
33
+ import type { SqlControlAdapter } from '@prisma-next/family-sql/control-adapter';
25
34
  import type {
26
35
  DataTransformOperation,
27
36
  SerializedQueryPlan,
28
37
  } from '@prisma-next/framework-components/control';
29
38
  import type { SqlStorage } from '@prisma-next/sql-contract/types';
30
39
  import type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';
31
- import { lowerSqlPlan } from '@prisma-next/sql-runtime';
32
40
 
33
41
  interface Buildable<R = unknown> {
34
42
  build(): SqlQueryPlan<R>;
@@ -47,21 +55,19 @@ export interface DataTransformOptions {
47
55
  readonly run: DataTransformClosure | readonly DataTransformClosure[];
48
56
  }
49
57
 
50
- /** Single shared adapter for apply/CLI; sufficient for single-threaded migration execution. */
51
- let adapterSingleton: ReturnType<typeof createPostgresAdapter> | null = null;
52
- function getAdapter(): ReturnType<typeof createPostgresAdapter> {
53
- if (adapterSingleton === null) {
54
- adapterSingleton = createPostgresAdapter();
55
- }
56
- return adapterSingleton;
57
- }
58
+ /**
59
+ * Concrete Postgres flavor of `DataTransformOperation`, re-exported so the
60
+ * `PostgresMigration.dataTransform` instance method can name it without
61
+ * leaking the framework-components symbol into call sites.
62
+ */
63
+ export type PostgresDataTransformOperation = DataTransformOperation;
58
64
 
59
65
  export function dataTransform<TContract extends Contract<SqlStorage>>(
60
66
  contract: TContract,
61
67
  name: string,
62
68
  options: DataTransformOptions,
69
+ adapter: SqlControlAdapter<'postgres'>,
63
70
  ): DataTransformOperation {
64
- const adapter = getAdapter();
65
71
  const runClosures: readonly DataTransformClosure[] = Array.isArray(options.run)
66
72
  ? options.run
67
73
  : [options.run as DataTransformClosure];
@@ -79,13 +85,13 @@ export function dataTransform<TContract extends Contract<SqlStorage>>(
79
85
  function invokeAndLower(
80
86
  closure: DataTransformClosure,
81
87
  contract: Contract<SqlStorage>,
82
- adapter: ReturnType<typeof createPostgresAdapter>,
88
+ adapter: SqlControlAdapter<'postgres'>,
83
89
  name: string,
84
90
  ): SerializedQueryPlan {
85
91
  const result = closure();
86
92
  const plan = isBuildable(result) ? result.build() : result;
87
93
  assertContractMatches(plan, contract, name);
88
- const lowered = lowerSqlPlan(adapter, contract, plan);
94
+ const lowered = adapter.lower(plan.ast, { contract });
89
95
  return { sql: lowered.sql, params: lowered.params };
90
96
  }
91
97
 
@@ -1,4 +1,4 @@
1
- import { quoteIdentifier } from '@prisma-next/adapter-postgres/control';
1
+ import { quoteIdentifier } from '../../sql-utils';
2
2
  import { type Op, step } from './shared';
3
3
 
4
4
  export function createExtension(extensionName: string): Op {
@@ -1,4 +1,4 @@
1
- import { escapeLiteral, qualifyName, quoteIdentifier } from '@prisma-next/adapter-postgres/control';
1
+ import { escapeLiteral, qualifyName, quoteIdentifier } from '../../sql-utils';
2
2
  import { type Op, step, targetDetails } from './shared';
3
3
 
4
4
  function enumTypeExistsCheck(schemaName: string, nativeType: string, exists = true): string {
@@ -1,4 +1,4 @@
1
- import { quoteIdentifier } from '@prisma-next/adapter-postgres/control';
1
+ import { quoteIdentifier } from '../../sql-utils';
2
2
  import { qualifyTableName, toRegclassLiteral } from '../planner-sql-checks';
3
3
  import { type Op, step, targetDetails } from './shared';
4
4
 
@@ -1,7 +1,7 @@
1
- import { quoteIdentifier } from '@prisma-next/adapter-postgres/control';
2
1
  import type { SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';
3
2
  import type { ReferentialAction } from '@prisma-next/sql-contract/types';
4
3
  import { ifDefined } from '@prisma-next/utils/defined';
4
+ import { quoteIdentifier } from '../../sql-utils';
5
5
  import type { OperationClass, PostgresPlanTargetDetails } from '../planner-target-details';
6
6
 
7
7
  export type Op = SqlMigrationPlanOperation<PostgresPlanTargetDetails>;
@@ -1,4 +1,4 @@
1
- import { quoteIdentifier } from '@prisma-next/adapter-postgres/control';
1
+ import { quoteIdentifier } from '../../sql-utils';
2
2
  import { qualifyTableName, toRegclassLiteral } from '../planner-sql-checks';
3
3
  import { type ColumnSpec, type Op, renderColumnDefinition, step, targetDetails } from './shared';
4
4
 
@@ -1,4 +1,3 @@
1
- import { escapeLiteral, quoteIdentifier } from '@prisma-next/adapter-postgres/control';
2
1
  import type { CodecControlHooks } from '@prisma-next/family-sql/control';
3
2
  import type {
4
3
  ForeignKey,
@@ -7,6 +6,7 @@ import type {
7
6
  StorageTable,
8
7
  StorageTypeInstance,
9
8
  } from '@prisma-next/sql-contract/types';
9
+ import { escapeLiteral, quoteIdentifier } from '../sql-utils';
10
10
  import type { PostgresColumnDefault } from '../types';
11
11
  import { qualifyTableName } from './planner-sql-checks';
12
12
  import { resolveColumnTypeMetadata } from './planner-type-resolution';
@@ -1,6 +1,6 @@
1
- import { quoteIdentifier } from '@prisma-next/adapter-postgres/control';
2
1
  import type { CodecControlHooks, SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';
3
2
  import type { StorageColumn, StorageTypeInstance } from '@prisma-next/sql-contract/types';
3
+ import { quoteIdentifier } from '../sql-utils';
4
4
  import { buildAddColumnSql } from './planner-ddl-builders';
5
5
  import {
6
6
  columnExistsCheck,
@@ -1,6 +1,6 @@
1
- import { escapeLiteral, quoteIdentifier } from '@prisma-next/adapter-postgres/control';
2
1
  import type { CodecControlHooks } from '@prisma-next/family-sql/control';
3
2
  import type { StorageColumn, StorageTypeInstance } from '@prisma-next/sql-contract/types';
3
+ import { escapeLiteral, quoteIdentifier } from '../sql-utils';
4
4
  import { resolveColumnTypeMetadata } from './planner-type-resolution';
5
5
 
6
6
  export function qualifyTableName(schema: string, table: string): string {
@@ -1,7 +1,3 @@
1
- import {
2
- normalizeSchemaNativeType,
3
- parsePostgresDefault,
4
- } from '@prisma-next/adapter-postgres/control';
5
1
  import type {
6
2
  MigrationOperationPolicy,
7
3
  SqlMigrationPlannerPlanOptions,
@@ -16,6 +12,8 @@ import type {
16
12
  MigrationScaffoldContext,
17
13
  SchemaIssue,
18
14
  } from '@prisma-next/framework-components/control';
15
+ import { parsePostgresDefault } from '../default-normalizer';
16
+ import { normalizeSchemaNativeType } from '../native-type-normalizer';
19
17
  import { planIssues } from './issue-planner';
20
18
  import { TypeScriptRenderablePostgresMigration } from './planner-produced-postgres-migration';
21
19
  import { postgresPlannerStrategies } from './planner-strategies';
@@ -1,4 +1,14 @@
1
+ import type { Contract } from '@prisma-next/contract/types';
2
+ import type { SqlControlAdapter } from '@prisma-next/family-sql/control-adapter';
1
3
  import { Migration as SqlMigration } from '@prisma-next/family-sql/migration';
4
+ import type { ControlStack } from '@prisma-next/framework-components/control';
5
+ import type { SqlStorage } from '@prisma-next/sql-contract/types';
6
+ import { errorPostgresMigrationStackMissing } from '../errors';
7
+ import {
8
+ type DataTransformOptions,
9
+ dataTransform,
10
+ type PostgresDataTransformOperation,
11
+ } from './operations/data-transform';
2
12
  import type { PostgresPlanTargetDetails } from './planner-target-details';
3
13
 
4
14
  /**
@@ -14,7 +24,50 @@ import type { PostgresPlanTargetDetails } from './planner-target-details';
14
24
  * emits `extends Migration` against a target-specific re-export of this
15
25
  * class from `@prisma-next/target-postgres/migration`, keeping the
16
26
  * authoring surface target-scoped rather than family-scoped.
27
+ *
28
+ * The constructor materializes a single Postgres `SqlControlAdapter` from
29
+ * `stack.adapter.create(stack)` and stores it; the protected `dataTransform`
30
+ * instance method forwards to the free `dataTransform` factory with that
31
+ * stored adapter, so user migrations can write `this.dataTransform(...)`
32
+ * without threading the adapter through every call.
17
33
  */
18
- export abstract class PostgresMigration extends SqlMigration<PostgresPlanTargetDetails> {
34
+ export abstract class PostgresMigration extends SqlMigration<
35
+ PostgresPlanTargetDetails,
36
+ 'postgres'
37
+ > {
19
38
  readonly targetId = 'postgres' as const;
39
+
40
+ /**
41
+ * Materialized Postgres control adapter, created once per migration
42
+ * instance from the injected stack. `undefined` only when the migration
43
+ * was instantiated without a stack (test fixtures); `dataTransform`
44
+ * throws in that case to surface the misuse.
45
+ */
46
+ protected readonly controlAdapter: SqlControlAdapter<'postgres'> | undefined;
47
+
48
+ constructor(stack?: ControlStack<'sql', 'postgres'>) {
49
+ super(stack);
50
+ // The descriptor `create()` is typed as the wider `ControlAdapterInstance`;
51
+ // the Postgres descriptor concretely returns a `SqlControlAdapter<'postgres'>`,
52
+ // so the cast holds for any Postgres-target stack assembled at runtime.
53
+ this.controlAdapter = stack?.adapter
54
+ ? (stack.adapter.create(stack) as SqlControlAdapter<'postgres'>)
55
+ : undefined;
56
+ }
57
+
58
+ /**
59
+ * Instance-method wrapper around the free `dataTransform` factory that
60
+ * supplies the stored control adapter. Authors call this from inside
61
+ * `get operations()`; the adapter argument is hidden from the call site.
62
+ */
63
+ protected dataTransform<TContract extends Contract<SqlStorage>>(
64
+ contract: TContract,
65
+ name: string,
66
+ options: DataTransformOptions,
67
+ ): PostgresDataTransformOperation {
68
+ if (!this.controlAdapter) {
69
+ throw errorPostgresMigrationStackMissing();
70
+ }
71
+ return dataTransform(contract, name, options, this.controlAdapter);
72
+ }
20
73
  }
@@ -21,17 +21,27 @@ export interface RenderMigrationMeta {
21
21
  }
22
22
 
23
23
  /**
24
- * Always-present base import the rendered scaffold always extends the
25
- * target-owned `Migration` (i.e. `PostgresMigration`) re-exported from
26
- * `@prisma-next/target-postgres/migration`. That re-export fixes the
27
- * `SqlMigration` generic to `PostgresPlanTargetDetails` and the abstract
28
- * `targetId` to `'postgres'`, so user-authored migrations don't need to
29
- * thread target-details or redeclare `targetId`.
24
+ * Always-present base imports for the rendered scaffold. Both come from
25
+ * `@prisma-next/target-postgres/migration` so an authored Postgres
26
+ * `migration.ts` only needs a single dependency for its base class and
27
+ * its CLI entrypoint:
28
+ *
29
+ * - `Migration` — the target-owned re-export fixes the `SqlMigration`
30
+ * generic to `PostgresPlanTargetDetails` and the abstract `targetId`
31
+ * to `'postgres'`, so user-authored migrations don't need to thread
32
+ * target-details or redeclare `targetId`.
33
+ * - `MigrationCLI` — the migration-file CLI entrypoint, re-exported from
34
+ * `@prisma-next/cli/migration-cli`. Loads `prisma-next.config.ts`,
35
+ * assembles a `ControlStack`, and instantiates the migration class.
36
+ * The migration file owns this dependency directly: pulling CLI
37
+ * machinery in at script run time is acceptable because the script's
38
+ * whole purpose is to be invoked from the project that owns the
39
+ * config.
30
40
  */
31
- const BASE_IMPORT: ImportRequirement = {
32
- moduleSpecifier: '@prisma-next/target-postgres/migration',
33
- symbol: 'Migration',
34
- };
41
+ const BASE_IMPORTS: readonly ImportRequirement[] = [
42
+ { moduleSpecifier: '@prisma-next/target-postgres/migration', symbol: 'Migration' },
43
+ { moduleSpecifier: '@prisma-next/target-postgres/migration', symbol: 'MigrationCLI' },
44
+ ];
35
45
 
36
46
  export function renderCallsToTypeScript(
37
47
  calls: ReadonlyArray<PostgresOpFactoryCall>,
@@ -53,13 +63,13 @@ export function renderCallsToTypeScript(
53
63
  ' }',
54
64
  '}',
55
65
  '',
56
- 'Migration.run(import.meta.url, M);',
66
+ 'MigrationCLI.run(import.meta.url, M);',
57
67
  '',
58
68
  ].join('\n');
59
69
  }
60
70
 
61
71
  function buildImports(calls: ReadonlyArray<PostgresOpFactoryCall>): string {
62
- const requirements: ImportRequirement[] = [BASE_IMPORT];
72
+ const requirements: ImportRequirement[] = [...BASE_IMPORTS];
63
73
  for (const call of calls) {
64
74
  for (const req of call.importRequirements()) {
65
75
  requirements.push(req);