@prisma-next/adapter-postgres 0.7.0 → 0.8.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/column-types.d.mts +2 -6
- package/dist/column-types.d.mts.map +1 -1
- package/dist/column-types.mjs +5 -4
- package/dist/column-types.mjs.map +1 -1
- package/dist/control.d.mts +8 -0
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +103 -3
- package/dist/control.mjs.map +1 -1
- package/dist/descriptor-meta-ZIv9PU-5.mjs +314 -0
- package/dist/descriptor-meta-ZIv9PU-5.mjs.map +1 -0
- package/dist/runtime.mjs +1 -1
- package/package.json +24 -24
- package/src/core/control-adapter.ts +15 -3
- package/src/core/descriptor-meta.ts +0 -3
- package/src/core/enum-control-hooks.ts +54 -657
- package/src/exports/column-types.ts +3 -7
- package/dist/descriptor-meta-DvzAuIyE.mjs +0 -799
- package/dist/descriptor-meta-DvzAuIyE.mjs.map +0 -1
|
@@ -1,22 +1,30 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { CodecControlHooks, SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';
|
|
3
|
-
import { arraysEqual } from '@prisma-next/family-sql/schema-verify';
|
|
4
|
-
import type { SqlStorage, StorageTypeInstance } from '@prisma-next/sql-contract/types';
|
|
5
|
-
import type { SqlSchemaIR } from '@prisma-next/sql-schema-ir/types';
|
|
1
|
+
import type { ControlDriverInstance } from '@prisma-next/framework-components/control';
|
|
6
2
|
import { PG_ENUM_CODEC_ID } from '@prisma-next/target-postgres/codec-ids';
|
|
7
|
-
import {
|
|
8
|
-
escapeLiteral,
|
|
9
|
-
qualifyName,
|
|
10
|
-
quoteIdentifier,
|
|
11
|
-
validateEnumValueLength,
|
|
12
|
-
} from '@prisma-next/target-postgres/sql-utils';
|
|
13
3
|
|
|
14
4
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* -
|
|
19
|
-
*
|
|
5
|
+
* Codec-typed annotation shape that the introspector writes under
|
|
6
|
+
* `schema.annotations.pg.storageTypes[<typeName>]`. Distinct from
|
|
7
|
+
* `StorageTypeInstance` because the introspector emits a plain literal
|
|
8
|
+
* (no class-instance API surface): only the fields downstream consumers
|
|
9
|
+
* actually read from the introspected envelope.
|
|
10
|
+
*/
|
|
11
|
+
export interface PostgresEnumStorageTypeAnnotation {
|
|
12
|
+
readonly codecId: typeof PG_ENUM_CODEC_ID;
|
|
13
|
+
readonly nativeType: string;
|
|
14
|
+
readonly typeParams: { readonly values: readonly string[] };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Postgres enum introspection.
|
|
19
|
+
*
|
|
20
|
+
* Migration planning and schema verification for enum types live at the
|
|
21
|
+
* SQL family layer + the Postgres target's planner-strategies layer (see
|
|
22
|
+
* `nativeEnumPlanCallStrategy` and the family-level `verifyEnumType`
|
|
23
|
+
* walk). Introspection is the only piece that remains here because the
|
|
24
|
+
* control adapter still calls into a codec-keyed dispatch surface
|
|
25
|
+
* (`storage.types` is rebuilt from this map in `control-adapter.ts`);
|
|
26
|
+
* the introspector returns the codec-typed shape that downstream
|
|
27
|
+
* `Contract` consumers expect.
|
|
20
28
|
*/
|
|
21
29
|
type EnumRow = {
|
|
22
30
|
schema_name: string;
|
|
@@ -24,15 +32,6 @@ type EnumRow = {
|
|
|
24
32
|
values: string[];
|
|
25
33
|
};
|
|
26
34
|
|
|
27
|
-
type EnumDiff =
|
|
28
|
-
| { kind: 'unchanged' }
|
|
29
|
-
| { kind: 'add_values'; values: readonly string[] }
|
|
30
|
-
| { kind: 'rebuild'; removedValues: readonly string[] };
|
|
31
|
-
|
|
32
|
-
// ============================================================================
|
|
33
|
-
// Introspection SQL
|
|
34
|
-
// ============================================================================
|
|
35
|
-
|
|
36
35
|
const ENUM_INTROSPECT_QUERY = `
|
|
37
36
|
SELECT
|
|
38
37
|
n.nspname AS schema_name,
|
|
@@ -46,13 +45,6 @@ const ENUM_INTROSPECT_QUERY = `
|
|
|
46
45
|
ORDER BY n.nspname, t.typname
|
|
47
46
|
`;
|
|
48
47
|
|
|
49
|
-
// ============================================================================
|
|
50
|
-
// Schema Helpers (Simplified)
|
|
51
|
-
// ============================================================================
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Type guard for string arrays. Used for runtime validation of introspected data.
|
|
55
|
-
*/
|
|
56
48
|
function isStringArray(value: unknown): value is string[] {
|
|
57
49
|
return Array.isArray(value) && value.every((entry) => typeof entry === 'string');
|
|
58
50
|
}
|
|
@@ -60,16 +52,14 @@ function isStringArray(value: unknown): value is string[] {
|
|
|
60
52
|
/**
|
|
61
53
|
* Parses a PostgreSQL array value into a JavaScript string array.
|
|
62
54
|
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
55
|
+
* The `pg` library returns `array_agg` results either as a JS array
|
|
56
|
+
* (when type parsers are configured) or as a string in PostgreSQL array
|
|
57
|
+
* literal format (`{value1,value2,...}`). Handles PG's quoting rules:
|
|
58
|
+
* - Elements containing commas, quotes, backslashes, or whitespace are
|
|
59
|
+
* double-quoted.
|
|
60
|
+
* - Inside quoted elements, `\"` represents `"` and `\\` represents `\`.
|
|
66
61
|
*
|
|
67
|
-
*
|
|
68
|
-
* - Elements containing commas, double quotes, backslashes, or whitespace are double-quoted
|
|
69
|
-
* - Inside quoted elements, `\"` represents `"` and `\\` represents `\`
|
|
70
|
-
*
|
|
71
|
-
* @param value - The value to parse (array or PostgreSQL array string)
|
|
72
|
-
* @returns A string array, or null if the value cannot be parsed
|
|
62
|
+
* Returns `null` when the input cannot be parsed as a PG array.
|
|
73
63
|
*/
|
|
74
64
|
export function parsePostgresArray(value: unknown): string[] | null {
|
|
75
65
|
if (isStringArray(value)) {
|
|
@@ -122,623 +112,30 @@ function parseArrayElements(input: string): string[] {
|
|
|
122
112
|
}
|
|
123
113
|
|
|
124
114
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
|
|
128
|
-
function getEnumValues(typeInstance: StorageTypeInstance): readonly string[] | null {
|
|
129
|
-
const values = typeInstance.typeParams?.['values'];
|
|
130
|
-
return isStringArray(values) ? values : null;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Reads existing enum values from the schema IR for a given native type.
|
|
135
|
-
* Uses optional chaining to simplify navigation through the annotations structure.
|
|
136
|
-
*/
|
|
137
|
-
function readExistingEnumValues(schema: SqlSchemaIR, nativeType: string): readonly string[] | null {
|
|
138
|
-
const storageTypes = (schema.annotations?.['pg'] as Record<string, unknown> | undefined)?.[
|
|
139
|
-
'storageTypes'
|
|
140
|
-
] as Record<string, StorageTypeInstance> | undefined;
|
|
141
|
-
|
|
142
|
-
const existing = storageTypes?.[nativeType];
|
|
143
|
-
if (!existing || existing.codecId !== PG_ENUM_CODEC_ID) {
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
return getEnumValues(existing);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Determines what changes are needed to transform existing enum values to desired values.
|
|
151
|
-
*
|
|
152
|
-
* Returns one of:
|
|
153
|
-
* - `unchanged`: No changes needed, values match exactly
|
|
154
|
-
* - `add_values`: New values can be safely appended (PostgreSQL supports this)
|
|
155
|
-
* - `rebuild`: Full enum rebuild required (value removal, reordering, or both)
|
|
156
|
-
*
|
|
157
|
-
* Note: PostgreSQL enums can only have values added (not removed or reordered) without
|
|
158
|
-
* a full type rebuild involving temp type creation and column migration.
|
|
159
|
-
*
|
|
160
|
-
* @param existing - Current enum values in the database
|
|
161
|
-
* @param desired - Target enum values from the contract
|
|
162
|
-
* @returns The type of change required
|
|
163
|
-
*/
|
|
164
|
-
function determineEnumDiff(existing: readonly string[], desired: readonly string[]): EnumDiff {
|
|
165
|
-
if (arraysEqual(existing, desired)) {
|
|
166
|
-
return { kind: 'unchanged' };
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Use Sets for O(1) lookups instead of O(n) array.includes()
|
|
170
|
-
const existingSet = new Set(existing);
|
|
171
|
-
const desiredSet = new Set(desired);
|
|
172
|
-
|
|
173
|
-
const missingValues = desired.filter((value) => !existingSet.has(value));
|
|
174
|
-
const removedValues = existing.filter((value) => !desiredSet.has(value));
|
|
175
|
-
const orderMismatch =
|
|
176
|
-
missingValues.length === 0 && removedValues.length === 0 && !arraysEqual(existing, desired);
|
|
177
|
-
|
|
178
|
-
if (removedValues.length > 0 || orderMismatch) {
|
|
179
|
-
return { kind: 'rebuild', removedValues };
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return { kind: 'add_values', values: missingValues };
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// ============================================================================
|
|
186
|
-
// SQL Helpers
|
|
187
|
-
// ============================================================================
|
|
188
|
-
|
|
189
|
-
function enumTypeExistsCheck(schemaName: string, typeName: string, exists = true): string {
|
|
190
|
-
const existsClause = exists ? 'EXISTS' : 'NOT EXISTS';
|
|
191
|
-
return `SELECT ${existsClause} (
|
|
192
|
-
SELECT 1
|
|
193
|
-
FROM pg_type t
|
|
194
|
-
JOIN pg_namespace n ON t.typnamespace = n.oid
|
|
195
|
-
WHERE n.nspname = '${escapeLiteral(schemaName)}'
|
|
196
|
-
AND t.typname = '${escapeLiteral(typeName)}'
|
|
197
|
-
)`;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// ============================================================================
|
|
201
|
-
// Operation Builders
|
|
202
|
-
// ============================================================================
|
|
203
|
-
|
|
204
|
-
function buildCreateEnumOperation(
|
|
205
|
-
typeName: string,
|
|
206
|
-
nativeType: string,
|
|
207
|
-
schemaName: string,
|
|
208
|
-
values: readonly string[],
|
|
209
|
-
): SqlMigrationPlanOperation<unknown> {
|
|
210
|
-
// Validate all enum values don't exceed PostgreSQL's label length limit
|
|
211
|
-
for (const value of values) {
|
|
212
|
-
validateEnumValueLength(value, typeName);
|
|
213
|
-
}
|
|
214
|
-
const literalValues = values.map((value) => `'${escapeLiteral(value)}'`).join(', ');
|
|
215
|
-
const qualifiedType = qualifyName(schemaName, nativeType);
|
|
216
|
-
return {
|
|
217
|
-
id: `type.${typeName}`,
|
|
218
|
-
label: `Create type ${typeName}`,
|
|
219
|
-
summary: `Creates enum type ${typeName}`,
|
|
220
|
-
operationClass: 'additive',
|
|
221
|
-
target: { id: 'postgres' },
|
|
222
|
-
precheck: [
|
|
223
|
-
{
|
|
224
|
-
description: `ensure type "${nativeType}" does not exist`,
|
|
225
|
-
sql: enumTypeExistsCheck(schemaName, nativeType, false),
|
|
226
|
-
},
|
|
227
|
-
],
|
|
228
|
-
execute: [
|
|
229
|
-
{
|
|
230
|
-
description: `create type "${nativeType}"`,
|
|
231
|
-
sql: `CREATE TYPE ${qualifiedType} AS ENUM (${literalValues})`,
|
|
232
|
-
},
|
|
233
|
-
],
|
|
234
|
-
postcheck: [
|
|
235
|
-
{
|
|
236
|
-
description: `verify type "${nativeType}" exists`,
|
|
237
|
-
sql: enumTypeExistsCheck(schemaName, nativeType),
|
|
238
|
-
},
|
|
239
|
-
],
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Computes the optimal position for inserting a new enum value to maintain
|
|
245
|
-
* the desired order relative to existing values.
|
|
246
|
-
*
|
|
247
|
-
* PostgreSQL's `ALTER TYPE ADD VALUE` supports BEFORE/AFTER positioning.
|
|
248
|
-
* This function finds the best reference value by:
|
|
249
|
-
* 1. Looking for the nearest preceding value that already exists
|
|
250
|
-
* 2. Falling back to the nearest following value if no preceding exists
|
|
251
|
-
* 3. Defaulting to end-of-list if no reference is found
|
|
252
|
-
*
|
|
253
|
-
* @param options.desired - The target ordered list of all enum values
|
|
254
|
-
* @param options.desiredIndex - Index of the value being inserted in the desired list
|
|
255
|
-
* @param options.current - Current list of enum values (being built up incrementally)
|
|
256
|
-
* @returns SQL clause (e.g., " AFTER 'x'") and insert position for tracking
|
|
257
|
-
*/
|
|
258
|
-
function computeInsertPosition(options: {
|
|
259
|
-
desired: readonly string[];
|
|
260
|
-
desiredIndex: number;
|
|
261
|
-
current: readonly string[];
|
|
262
|
-
}): { clause: string; insertAt: number } {
|
|
263
|
-
const { desired, desiredIndex, current } = options;
|
|
264
|
-
const currentSet = new Set(current);
|
|
265
|
-
const previous = desired
|
|
266
|
-
.slice(0, desiredIndex)
|
|
267
|
-
.reverse()
|
|
268
|
-
.find((candidate) => currentSet.has(candidate));
|
|
269
|
-
const next = desired.slice(desiredIndex + 1).find((candidate) => currentSet.has(candidate));
|
|
270
|
-
const clause = previous
|
|
271
|
-
? ` AFTER '${escapeLiteral(previous)}'`
|
|
272
|
-
: next
|
|
273
|
-
? ` BEFORE '${escapeLiteral(next)}'`
|
|
274
|
-
: '';
|
|
275
|
-
const insertAt = previous
|
|
276
|
-
? current.indexOf(previous) + 1
|
|
277
|
-
: next
|
|
278
|
-
? current.indexOf(next)
|
|
279
|
-
: current.length;
|
|
280
|
-
|
|
281
|
-
return { clause, insertAt };
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Builds operations to add new enum values to an existing PostgreSQL enum type.
|
|
286
|
-
*
|
|
287
|
-
* Each new value is added with `ALTER TYPE ... ADD VALUE IF NOT EXISTS` for idempotency.
|
|
288
|
-
* Values are inserted in the correct order using BEFORE/AFTER positioning to match
|
|
289
|
-
* the desired final order.
|
|
290
|
-
*
|
|
291
|
-
* This is a safe, non-destructive operation - existing data is not affected.
|
|
292
|
-
*
|
|
293
|
-
* @param options.typeName - Contract-level type name (e.g., 'Role')
|
|
294
|
-
* @param options.nativeType - PostgreSQL type name (e.g., 'role')
|
|
295
|
-
* @param options.schemaName - PostgreSQL schema (e.g., 'public')
|
|
296
|
-
* @param options.desired - Target ordered list of all enum values
|
|
297
|
-
* @param options.existing - Current enum values in the database
|
|
298
|
-
* @returns Array of migration operations to add each missing value
|
|
299
|
-
*/
|
|
300
|
-
function buildAddValueOperations(options: {
|
|
301
|
-
typeName: string;
|
|
302
|
-
nativeType: string;
|
|
303
|
-
schemaName: string;
|
|
304
|
-
desired: readonly string[];
|
|
305
|
-
existing: readonly string[];
|
|
306
|
-
}): SqlMigrationPlanOperation<unknown>[] {
|
|
307
|
-
const { typeName, nativeType, schemaName } = options;
|
|
308
|
-
const current = [...options.existing];
|
|
309
|
-
const currentSet = new Set(current);
|
|
310
|
-
const operations: SqlMigrationPlanOperation<unknown>[] = [];
|
|
311
|
-
for (let index = 0; index < options.desired.length; index += 1) {
|
|
312
|
-
const value = options.desired[index];
|
|
313
|
-
if (value === undefined) {
|
|
314
|
-
continue;
|
|
315
|
-
}
|
|
316
|
-
if (currentSet.has(value)) {
|
|
317
|
-
continue;
|
|
318
|
-
}
|
|
319
|
-
// Validate the new value doesn't exceed PostgreSQL's label length limit
|
|
320
|
-
validateEnumValueLength(value, typeName);
|
|
321
|
-
const { clause, insertAt } = computeInsertPosition({
|
|
322
|
-
desired: options.desired,
|
|
323
|
-
desiredIndex: index,
|
|
324
|
-
current,
|
|
325
|
-
});
|
|
326
|
-
// Use IF NOT EXISTS for idempotency - safe to re-run after partial failures.
|
|
327
|
-
// Supported in PostgreSQL 9.3+, and we require PostgreSQL 12+.
|
|
328
|
-
operations.push({
|
|
329
|
-
id: `type.${typeName}.value.${value}`,
|
|
330
|
-
label: `Add value ${value} to ${typeName}`,
|
|
331
|
-
summary: `Adds enum value ${value} to ${typeName}`,
|
|
332
|
-
operationClass: 'widening',
|
|
333
|
-
target: { id: 'postgres' },
|
|
334
|
-
precheck: [],
|
|
335
|
-
execute: [
|
|
336
|
-
{
|
|
337
|
-
description: `add value "${value}" if not exists`,
|
|
338
|
-
sql: `ALTER TYPE ${qualifyName(schemaName, nativeType)} ADD VALUE IF NOT EXISTS '${escapeLiteral(
|
|
339
|
-
value,
|
|
340
|
-
)}'${clause}`,
|
|
341
|
-
},
|
|
342
|
-
],
|
|
343
|
-
postcheck: [],
|
|
344
|
-
});
|
|
345
|
-
current.splice(insertAt, 0, value);
|
|
346
|
-
currentSet.add(value);
|
|
347
|
-
}
|
|
348
|
-
return operations;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Collects columns using the enum type from the contract (desired state).
|
|
353
|
-
* Used for type-safe reference tracking.
|
|
354
|
-
*/
|
|
355
|
-
function collectEnumColumnsFromContract(
|
|
356
|
-
contract: Contract<SqlStorage>,
|
|
357
|
-
typeName: string,
|
|
358
|
-
nativeType: string,
|
|
359
|
-
): ReadonlyArray<{ table: string; column: string }> {
|
|
360
|
-
const columns: Array<{ table: string; column: string }> = [];
|
|
361
|
-
for (const [tableName, table] of Object.entries(contract.storage.tables)) {
|
|
362
|
-
for (const [columnName, column] of Object.entries(table.columns)) {
|
|
363
|
-
if (
|
|
364
|
-
column.typeRef === typeName ||
|
|
365
|
-
(column.nativeType === nativeType && column.codecId === PG_ENUM_CODEC_ID)
|
|
366
|
-
) {
|
|
367
|
-
columns.push({ table: tableName, column: columnName });
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
return columns;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Collects columns using the enum type from the schema IR (live database state).
|
|
376
|
-
* This ensures we find ALL dependent columns, including those added outside the contract
|
|
377
|
-
* (e.g., manual DDL), which is critical for safe enum rebuild operations.
|
|
378
|
-
*/
|
|
379
|
-
function collectEnumColumnsFromSchema(
|
|
380
|
-
schema: SqlSchemaIR,
|
|
381
|
-
nativeType: string,
|
|
382
|
-
): ReadonlyArray<{ table: string; column: string }> {
|
|
383
|
-
const columns: Array<{ table: string; column: string }> = [];
|
|
384
|
-
for (const [tableName, table] of Object.entries(schema.tables)) {
|
|
385
|
-
for (const [columnName, column] of Object.entries(table.columns)) {
|
|
386
|
-
// Match by nativeType since schema IR doesn't have codecId/typeRef
|
|
387
|
-
if (column.nativeType === nativeType) {
|
|
388
|
-
columns.push({ table: tableName, column: columnName });
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
return columns;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* Collects all columns using the enum type from both contract AND live database.
|
|
397
|
-
* Merges and deduplicates to ensure we migrate ALL dependent columns during rebuild.
|
|
398
|
-
*
|
|
399
|
-
* This is critical for data integrity: if a column exists in the database using
|
|
400
|
-
* this enum but is not in the contract (e.g., added via manual DDL), we must
|
|
401
|
-
* still migrate it to avoid DROP TYPE failures.
|
|
115
|
+
* Reads enum types from the live Postgres schema and returns them in
|
|
116
|
+
* the codec-typed annotation shape consumed by `control-adapter.ts`
|
|
117
|
+
* (which writes them under `schema.annotations.pg.storageTypes`).
|
|
402
118
|
*/
|
|
403
|
-
function
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const
|
|
410
|
-
const
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const key = `${col.table}.${col.column}`;
|
|
418
|
-
if (!seen.has(key)) {
|
|
419
|
-
seen.add(key);
|
|
420
|
-
result.push(col);
|
|
119
|
+
export async function introspectPostgresEnumTypes(options: {
|
|
120
|
+
readonly driver: ControlDriverInstance<'sql', 'postgres'>;
|
|
121
|
+
readonly schemaName?: string;
|
|
122
|
+
}): Promise<Record<string, PostgresEnumStorageTypeAnnotation>> {
|
|
123
|
+
const namespace = options.schemaName ?? 'public';
|
|
124
|
+
const result = await options.driver.query<EnumRow>(ENUM_INTROSPECT_QUERY, [namespace]);
|
|
125
|
+
const types: Record<string, PostgresEnumStorageTypeAnnotation> = {};
|
|
126
|
+
for (const row of result.rows) {
|
|
127
|
+
const values = parsePostgresArray(row.values);
|
|
128
|
+
if (!values) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Failed to parse enum values for type "${row.type_name}": ` +
|
|
131
|
+
`unexpected format: ${JSON.stringify(row.values)}`,
|
|
132
|
+
);
|
|
421
133
|
}
|
|
134
|
+
types[row.type_name] = {
|
|
135
|
+
codecId: PG_ENUM_CODEC_ID,
|
|
136
|
+
nativeType: row.type_name,
|
|
137
|
+
typeParams: { values },
|
|
138
|
+
};
|
|
422
139
|
}
|
|
423
|
-
|
|
424
|
-
// Sort for deterministic operation order
|
|
425
|
-
return result.sort((a, b) => {
|
|
426
|
-
const tableCompare = a.table.localeCompare(b.table);
|
|
427
|
-
return tableCompare !== 0 ? tableCompare : a.column.localeCompare(b.column);
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Builds a SQL check to verify a column's type matches an expected type.
|
|
433
|
-
*/
|
|
434
|
-
function columnTypeCheck(options: {
|
|
435
|
-
schemaName: string;
|
|
436
|
-
tableName: string;
|
|
437
|
-
columnName: string;
|
|
438
|
-
expectedType: string;
|
|
439
|
-
}): string {
|
|
440
|
-
return `SELECT EXISTS (
|
|
441
|
-
SELECT 1
|
|
442
|
-
FROM information_schema.columns
|
|
443
|
-
WHERE table_schema = '${escapeLiteral(options.schemaName)}'
|
|
444
|
-
AND table_name = '${escapeLiteral(options.tableName)}'
|
|
445
|
-
AND column_name = '${escapeLiteral(options.columnName)}'
|
|
446
|
-
AND udt_name = '${escapeLiteral(options.expectedType)}'
|
|
447
|
-
)`;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
/** PostgreSQL maximum identifier length (NAMEDATALEN - 1) */
|
|
451
|
-
const MAX_IDENTIFIER_LENGTH = 63;
|
|
452
|
-
|
|
453
|
-
/** Suffix added to enum type names during rebuild operations */
|
|
454
|
-
const REBUILD_SUFFIX = '__pn_rebuild';
|
|
455
|
-
|
|
456
|
-
/**
|
|
457
|
-
* Builds an SQL check to verify no rows contain any of the removed enum values.
|
|
458
|
-
* This prevents data loss during enum rebuild operations.
|
|
459
|
-
*
|
|
460
|
-
* @param schemaName - PostgreSQL schema name
|
|
461
|
-
* @param tableName - Table containing the enum column
|
|
462
|
-
* @param columnName - Column using the enum type
|
|
463
|
-
* @param removedValues - Array of enum values being removed
|
|
464
|
-
* @returns SQL query that returns true if no rows contain removed values
|
|
465
|
-
*/
|
|
466
|
-
function noRemovedValuesExistCheck(
|
|
467
|
-
schemaName: string,
|
|
468
|
-
tableName: string,
|
|
469
|
-
columnName: string,
|
|
470
|
-
removedValues: readonly string[],
|
|
471
|
-
): string {
|
|
472
|
-
if (removedValues.length === 0) {
|
|
473
|
-
// No values being removed, always passes
|
|
474
|
-
return 'SELECT true';
|
|
475
|
-
}
|
|
476
|
-
const valuesList = removedValues.map((v) => `'${escapeLiteral(v)}'`).join(', ');
|
|
477
|
-
return `SELECT NOT EXISTS (
|
|
478
|
-
SELECT 1 FROM ${qualifyName(schemaName, tableName)}
|
|
479
|
-
WHERE ${quoteIdentifier(columnName)}::text IN (${valuesList})
|
|
480
|
-
LIMIT 1
|
|
481
|
-
)`;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* Builds a migration operation to recreate a PostgreSQL enum type with updated values.
|
|
486
|
-
*
|
|
487
|
-
* This is required when:
|
|
488
|
-
* - Enum values are removed (PostgreSQL doesn't support direct removal)
|
|
489
|
-
* - Enum values are reordered (PostgreSQL doesn't support reordering)
|
|
490
|
-
*
|
|
491
|
-
* The operation:
|
|
492
|
-
* 1. Creates a new enum type with the desired values (temp name)
|
|
493
|
-
* 2. Migrates all columns to use the new type via text cast
|
|
494
|
-
* 3. Drops the original type
|
|
495
|
-
* 4. Renames the temp type to the original name
|
|
496
|
-
*
|
|
497
|
-
* IMPORTANT: If values are being removed and data exists using those values,
|
|
498
|
-
* the operation will fail at the precheck stage with a clear error message.
|
|
499
|
-
* This prevents silent data loss.
|
|
500
|
-
*
|
|
501
|
-
* @param options.typeName - Contract-level type name
|
|
502
|
-
* @param options.nativeType - PostgreSQL type name
|
|
503
|
-
* @param options.schemaName - PostgreSQL schema
|
|
504
|
-
* @param options.values - Desired final enum values
|
|
505
|
-
* @param options.removedValues - Values being removed (for data loss checks)
|
|
506
|
-
* @param options.contract - Full contract for column discovery
|
|
507
|
-
* @param options.schema - Current schema IR for column discovery
|
|
508
|
-
* @returns Migration operation for full enum rebuild
|
|
509
|
-
*/
|
|
510
|
-
function buildRecreateEnumOperation(options: {
|
|
511
|
-
typeName: string;
|
|
512
|
-
nativeType: string;
|
|
513
|
-
schemaName: string;
|
|
514
|
-
values: readonly string[];
|
|
515
|
-
removedValues: readonly string[];
|
|
516
|
-
contract: Contract<SqlStorage>;
|
|
517
|
-
schema: SqlSchemaIR;
|
|
518
|
-
}): SqlMigrationPlanOperation<unknown> {
|
|
519
|
-
const tempTypeName = `${options.nativeType}${REBUILD_SUFFIX}`;
|
|
520
|
-
|
|
521
|
-
// Validate temp type name length won't exceed PostgreSQL's 63-character limit.
|
|
522
|
-
// If it would, PostgreSQL silently truncates which could cause conflicts.
|
|
523
|
-
if (tempTypeName.length > MAX_IDENTIFIER_LENGTH) {
|
|
524
|
-
const maxBaseLength = MAX_IDENTIFIER_LENGTH - REBUILD_SUFFIX.length;
|
|
525
|
-
throw new Error(
|
|
526
|
-
`Enum type name "${options.nativeType}" is too long for rebuild operation. ` +
|
|
527
|
-
`Maximum length is ${maxBaseLength} characters (type name + "${REBUILD_SUFFIX}" suffix ` +
|
|
528
|
-
`must fit within PostgreSQL's ${MAX_IDENTIFIER_LENGTH}-character identifier limit).`,
|
|
529
|
-
);
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
const qualifiedOriginal = qualifyName(options.schemaName, options.nativeType);
|
|
533
|
-
const qualifiedTemp = qualifyName(options.schemaName, tempTypeName);
|
|
534
|
-
const literalValues = options.values.map((value) => `'${escapeLiteral(value)}'`).join(', ');
|
|
535
|
-
|
|
536
|
-
// CRITICAL: Collect columns from BOTH contract AND live database.
|
|
537
|
-
// This ensures we migrate ALL dependent columns, including those added
|
|
538
|
-
// outside of Prisma Next (e.g., manual DDL). Without this, DROP TYPE
|
|
539
|
-
// would fail if the database has columns not tracked in the contract.
|
|
540
|
-
const columnRefs = collectAllEnumColumns(
|
|
541
|
-
options.contract,
|
|
542
|
-
options.schema,
|
|
543
|
-
options.typeName,
|
|
544
|
-
options.nativeType,
|
|
545
|
-
);
|
|
546
|
-
|
|
547
|
-
const alterColumns = columnRefs.map((ref) => ({
|
|
548
|
-
description: `alter ${ref.table}.${ref.column} to ${tempTypeName}`,
|
|
549
|
-
sql: `ALTER TABLE ${qualifyName(options.schemaName, ref.table)}
|
|
550
|
-
ALTER COLUMN ${quoteIdentifier(ref.column)}
|
|
551
|
-
TYPE ${qualifiedTemp}
|
|
552
|
-
USING ${quoteIdentifier(ref.column)}::text::${qualifiedTemp}`,
|
|
553
|
-
}));
|
|
554
|
-
|
|
555
|
-
// Build postchecks to verify:
|
|
556
|
-
// 1. The final type exists with the correct name
|
|
557
|
-
// 2. The temp type was cleaned up (renamed away)
|
|
558
|
-
// 3. All migrated columns now reference the final type
|
|
559
|
-
const postchecks = [
|
|
560
|
-
{
|
|
561
|
-
description: `verify type "${options.nativeType}" exists`,
|
|
562
|
-
sql: enumTypeExistsCheck(options.schemaName, options.nativeType),
|
|
563
|
-
},
|
|
564
|
-
{
|
|
565
|
-
description: `verify temp type "${tempTypeName}" was removed`,
|
|
566
|
-
sql: enumTypeExistsCheck(options.schemaName, tempTypeName, false),
|
|
567
|
-
},
|
|
568
|
-
// Verify each column was successfully migrated to the final type
|
|
569
|
-
...columnRefs.map((ref) => ({
|
|
570
|
-
description: `verify ${ref.table}.${ref.column} uses type "${options.nativeType}"`,
|
|
571
|
-
sql: columnTypeCheck({
|
|
572
|
-
schemaName: options.schemaName,
|
|
573
|
-
tableName: ref.table,
|
|
574
|
-
columnName: ref.column,
|
|
575
|
-
expectedType: options.nativeType,
|
|
576
|
-
}),
|
|
577
|
-
})),
|
|
578
|
-
];
|
|
579
|
-
|
|
580
|
-
return {
|
|
581
|
-
id: `type.${options.typeName}.rebuild`,
|
|
582
|
-
label: `Rebuild type ${options.typeName}`,
|
|
583
|
-
summary: `Recreates enum type ${options.typeName} with updated values`,
|
|
584
|
-
operationClass: 'destructive',
|
|
585
|
-
target: { id: 'postgres' },
|
|
586
|
-
precheck: [
|
|
587
|
-
{
|
|
588
|
-
description: `ensure type "${options.nativeType}" exists`,
|
|
589
|
-
sql: enumTypeExistsCheck(options.schemaName, options.nativeType),
|
|
590
|
-
},
|
|
591
|
-
// Note: We don't precheck that temp type doesn't exist because we handle
|
|
592
|
-
// orphaned temp types in the execute step below.
|
|
593
|
-
|
|
594
|
-
// CRITICAL: If values are being removed, verify no data exists using those values.
|
|
595
|
-
// This prevents silent data loss during the rebuild - the USING cast would fail
|
|
596
|
-
// at runtime if rows contain values that don't exist in the new enum.
|
|
597
|
-
...(options.removedValues.length > 0
|
|
598
|
-
? columnRefs.map((ref) => ({
|
|
599
|
-
description: `ensure no rows in ${ref.table}.${ref.column} contain removed values (${options.removedValues.join(', ')})`,
|
|
600
|
-
sql: noRemovedValuesExistCheck(
|
|
601
|
-
options.schemaName,
|
|
602
|
-
ref.table,
|
|
603
|
-
ref.column,
|
|
604
|
-
options.removedValues,
|
|
605
|
-
),
|
|
606
|
-
}))
|
|
607
|
-
: []),
|
|
608
|
-
],
|
|
609
|
-
execute: [
|
|
610
|
-
// Clean up any orphaned temp type from a previous failed migration.
|
|
611
|
-
// This makes the operation recoverable without manual intervention.
|
|
612
|
-
// DROP TYPE IF EXISTS is safe - it's a no-op if the type doesn't exist.
|
|
613
|
-
{
|
|
614
|
-
description: `drop orphaned temp type "${tempTypeName}" if exists`,
|
|
615
|
-
sql: `DROP TYPE IF EXISTS ${qualifiedTemp}`,
|
|
616
|
-
},
|
|
617
|
-
{
|
|
618
|
-
description: `create temp type "${tempTypeName}"`,
|
|
619
|
-
sql: `CREATE TYPE ${qualifiedTemp} AS ENUM (${literalValues})`,
|
|
620
|
-
},
|
|
621
|
-
...alterColumns,
|
|
622
|
-
{
|
|
623
|
-
description: `drop type "${options.nativeType}"`,
|
|
624
|
-
sql: `DROP TYPE ${qualifiedOriginal}`,
|
|
625
|
-
},
|
|
626
|
-
{
|
|
627
|
-
description: `rename type "${tempTypeName}" to "${options.nativeType}"`,
|
|
628
|
-
sql: `ALTER TYPE ${qualifiedTemp} RENAME TO ${quoteIdentifier(options.nativeType)}`,
|
|
629
|
-
},
|
|
630
|
-
],
|
|
631
|
-
postcheck: postchecks,
|
|
632
|
-
};
|
|
140
|
+
return types;
|
|
633
141
|
}
|
|
634
|
-
|
|
635
|
-
// ============================================================================
|
|
636
|
-
// Codec Control Hooks
|
|
637
|
-
// ============================================================================
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* Postgres enum hooks for planning, verifying, and introspecting `storage.types`.
|
|
641
|
-
*/
|
|
642
|
-
export const pgEnumControlHooks: CodecControlHooks = {
|
|
643
|
-
planTypeOperations: ({ typeName, typeInstance, contract, schema, schemaName }) => {
|
|
644
|
-
const desired = getEnumValues(typeInstance);
|
|
645
|
-
if (!desired || desired.length === 0) {
|
|
646
|
-
return { operations: [] };
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
const schemaNamespace = schemaName ?? 'public';
|
|
650
|
-
const existing = readExistingEnumValues(schema, typeInstance.nativeType);
|
|
651
|
-
if (!existing) {
|
|
652
|
-
return {
|
|
653
|
-
operations: [
|
|
654
|
-
buildCreateEnumOperation(typeName, typeInstance.nativeType, schemaNamespace, desired),
|
|
655
|
-
],
|
|
656
|
-
};
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
const diff = determineEnumDiff(existing, desired);
|
|
660
|
-
if (diff.kind === 'unchanged') {
|
|
661
|
-
return { operations: [] };
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
if (diff.kind === 'rebuild') {
|
|
665
|
-
return {
|
|
666
|
-
operations: [
|
|
667
|
-
buildRecreateEnumOperation({
|
|
668
|
-
typeName,
|
|
669
|
-
nativeType: typeInstance.nativeType,
|
|
670
|
-
schemaName: schemaNamespace,
|
|
671
|
-
values: desired,
|
|
672
|
-
removedValues: diff.removedValues,
|
|
673
|
-
contract,
|
|
674
|
-
schema,
|
|
675
|
-
}),
|
|
676
|
-
],
|
|
677
|
-
};
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
return {
|
|
681
|
-
operations: buildAddValueOperations({
|
|
682
|
-
typeName,
|
|
683
|
-
nativeType: typeInstance.nativeType,
|
|
684
|
-
schemaName: schemaNamespace,
|
|
685
|
-
desired,
|
|
686
|
-
existing,
|
|
687
|
-
}),
|
|
688
|
-
};
|
|
689
|
-
},
|
|
690
|
-
verifyType: ({ typeName, typeInstance, schema }) => {
|
|
691
|
-
const desired = getEnumValues(typeInstance);
|
|
692
|
-
if (!desired) {
|
|
693
|
-
return [];
|
|
694
|
-
}
|
|
695
|
-
const existing = readExistingEnumValues(schema, typeInstance.nativeType);
|
|
696
|
-
if (!existing) {
|
|
697
|
-
return [
|
|
698
|
-
{
|
|
699
|
-
kind: 'type_missing',
|
|
700
|
-
typeName,
|
|
701
|
-
message: `Type "${typeName}" is missing from database`,
|
|
702
|
-
},
|
|
703
|
-
];
|
|
704
|
-
}
|
|
705
|
-
const diff = determineEnumDiff(existing, desired);
|
|
706
|
-
if (diff.kind === 'unchanged') return [];
|
|
707
|
-
const existingSet = new Set(existing);
|
|
708
|
-
const desiredSet = new Set(desired);
|
|
709
|
-
const addedValues = desired.filter((v) => !existingSet.has(v));
|
|
710
|
-
const removedValues = existing.filter((v) => !desiredSet.has(v));
|
|
711
|
-
return [
|
|
712
|
-
{
|
|
713
|
-
kind: 'enum_values_changed' as const,
|
|
714
|
-
typeName,
|
|
715
|
-
addedValues,
|
|
716
|
-
removedValues,
|
|
717
|
-
message:
|
|
718
|
-
diff.kind === 'add_values'
|
|
719
|
-
? `Enum type "${typeName}" needs new values: ${addedValues.join(', ')}`
|
|
720
|
-
: `Enum type "${typeName}" values changed (requires rebuild): +[${addedValues.join(', ')}] -[${removedValues.join(', ')}]`,
|
|
721
|
-
},
|
|
722
|
-
];
|
|
723
|
-
},
|
|
724
|
-
introspectTypes: async ({ driver, schemaName }) => {
|
|
725
|
-
const namespace = schemaName ?? 'public';
|
|
726
|
-
const result = await driver.query<EnumRow>(ENUM_INTROSPECT_QUERY, [namespace]);
|
|
727
|
-
const types: Record<string, StorageTypeInstance> = {};
|
|
728
|
-
for (const row of result.rows) {
|
|
729
|
-
const values = parsePostgresArray(row.values);
|
|
730
|
-
if (!values) {
|
|
731
|
-
throw new Error(
|
|
732
|
-
`Failed to parse enum values for type "${row.type_name}": ` +
|
|
733
|
-
`unexpected format: ${JSON.stringify(row.values)}`,
|
|
734
|
-
);
|
|
735
|
-
}
|
|
736
|
-
types[row.type_name] = {
|
|
737
|
-
codecId: PG_ENUM_CODEC_ID,
|
|
738
|
-
nativeType: row.type_name,
|
|
739
|
-
typeParams: { values },
|
|
740
|
-
};
|
|
741
|
-
}
|
|
742
|
-
return types;
|
|
743
|
-
},
|
|
744
|
-
};
|