@prisma-next/target-postgres 0.4.0-dev.8 → 0.4.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.
Files changed (44) hide show
  1. package/dist/control.d.mts +1 -9
  2. package/dist/control.d.mts.map +1 -1
  3. package/dist/control.mjs +1693 -4798
  4. package/dist/control.mjs.map +1 -1
  5. package/dist/migration.d.mts +164 -0
  6. package/dist/migration.d.mts.map +1 -0
  7. package/dist/migration.mjs +446 -0
  8. package/dist/migration.mjs.map +1 -0
  9. package/dist/planner-target-details-MXb3oeul.d.mts +11 -0
  10. package/dist/planner-target-details-MXb3oeul.d.mts.map +1 -0
  11. package/dist/postgres-migration-BsHJHV9O.mjs +2793 -0
  12. package/dist/postgres-migration-BsHJHV9O.mjs.map +1 -0
  13. package/package.json +21 -19
  14. package/src/core/migrations/issue-planner.ts +832 -0
  15. package/src/core/migrations/op-factory-call.ts +862 -0
  16. package/src/core/migrations/operations/columns.ts +285 -0
  17. package/src/core/migrations/operations/constraints.ts +191 -0
  18. package/src/core/migrations/operations/data-transform.ts +113 -0
  19. package/src/core/migrations/operations/dependencies.ts +36 -0
  20. package/src/core/migrations/operations/enums.ts +113 -0
  21. package/src/core/migrations/operations/indexes.ts +61 -0
  22. package/src/core/migrations/operations/raw.ts +15 -0
  23. package/src/core/migrations/operations/shared.ts +67 -0
  24. package/src/core/migrations/operations/tables.ts +63 -0
  25. package/src/core/migrations/planner-produced-postgres-migration.ts +67 -0
  26. package/src/core/migrations/planner-strategies.ts +592 -151
  27. package/src/core/migrations/planner-target-details.ts +0 -6
  28. package/src/core/migrations/planner.ts +63 -781
  29. package/src/core/migrations/postgres-migration.ts +20 -0
  30. package/src/core/migrations/render-ops.ts +9 -0
  31. package/src/core/migrations/render-typescript.ts +95 -0
  32. package/src/exports/control.ts +9 -142
  33. package/src/exports/migration.ts +40 -0
  34. package/dist/migration-builders.d.mts +0 -88
  35. package/dist/migration-builders.d.mts.map +0 -1
  36. package/dist/migration-builders.mjs +0 -3
  37. package/dist/operation-descriptors-CxymFSgK.mjs +0 -52
  38. package/dist/operation-descriptors-CxymFSgK.mjs.map +0 -1
  39. package/src/core/migrations/descriptor-planner.ts +0 -464
  40. package/src/core/migrations/operation-descriptors.ts +0 -166
  41. package/src/core/migrations/operation-resolver.ts +0 -929
  42. package/src/core/migrations/planner-reconciliation.ts +0 -798
  43. package/src/core/migrations/scaffolding.ts +0 -140
  44. package/src/exports/migration-builders.ts +0 -56
@@ -0,0 +1,832 @@
1
+ /**
2
+ * Postgres migration planner.
3
+ *
4
+ * Takes schema issues (from verifySqlSchema) and emits migration IR
5
+ * (`PostgresOpFactoryCall[]`). Strategies consume issues they recognize and
6
+ * produce specialized call sequences (e.g. NOT NULL backfill →
7
+ * addColumn(nullable) + dataTransform + setNotNull); remaining issues flow
8
+ * through `mapIssueToCall` for the default case.
9
+ */
10
+
11
+ import type { Contract } from '@prisma-next/contract/types';
12
+ import type {
13
+ CodecControlHooks,
14
+ MigrationOperationPolicy,
15
+ SqlPlannerConflict,
16
+ SqlPlannerConflictLocation,
17
+ } from '@prisma-next/family-sql/control';
18
+ import type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';
19
+ import type { SchemaIssue } from '@prisma-next/framework-components/control';
20
+ import type {
21
+ SqlStorage,
22
+ StorageColumn,
23
+ StorageTypeInstance,
24
+ } from '@prisma-next/sql-contract/types';
25
+ import type { SqlSchemaIR } from '@prisma-next/sql-schema-ir/types';
26
+ import type { Result } from '@prisma-next/utils/result';
27
+ import { notOk, ok } from '@prisma-next/utils/result';
28
+ import {
29
+ AddColumnCall,
30
+ AddForeignKeyCall,
31
+ AddPrimaryKeyCall,
32
+ AddUniqueCall,
33
+ AlterColumnTypeCall,
34
+ CreateEnumTypeCall,
35
+ CreateExtensionCall,
36
+ CreateIndexCall,
37
+ CreateSchemaCall,
38
+ CreateTableCall,
39
+ DropColumnCall,
40
+ DropConstraintCall,
41
+ DropDefaultCall,
42
+ DropIndexCall,
43
+ DropNotNullCall,
44
+ DropTableCall,
45
+ type PostgresOpFactoryCall,
46
+ SetDefaultCall,
47
+ SetNotNullCall,
48
+ } from './op-factory-call';
49
+ import type { ColumnSpec, ForeignKeySpec } from './operations/shared';
50
+ import { buildColumnDefaultSql, buildColumnTypeSql } from './planner-ddl-builders';
51
+ import { buildExpectedFormatType } from './planner-sql-checks';
52
+ import {
53
+ type CallMigrationStrategy,
54
+ postgresPlannerStrategies,
55
+ type StrategyContext,
56
+ } from './planner-strategies';
57
+
58
+ export type { CallMigrationStrategy, StrategyContext };
59
+
60
+ // ============================================================================
61
+ // Issue kind ordering (dependency order)
62
+ // ============================================================================
63
+
64
+ const ISSUE_KIND_ORDER: Record<string, number> = {
65
+ // Dependencies and types first
66
+ dependency_missing: 1,
67
+ type_missing: 2,
68
+ type_values_mismatch: 3,
69
+ enum_values_changed: 3,
70
+
71
+ // Drops (reconciliation — clear the way for creates)
72
+ // FKs dropped first (they depend on other constraints)
73
+ extra_foreign_key: 10,
74
+ extra_unique_constraint: 11,
75
+ extra_primary_key: 12,
76
+ extra_index: 13,
77
+ extra_default: 14,
78
+ extra_column: 15,
79
+ extra_table: 16,
80
+
81
+ // Tables before columns
82
+ missing_table: 20,
83
+
84
+ // Columns before constraints
85
+ missing_column: 30,
86
+
87
+ // Reconciliation alters (on existing objects)
88
+ type_mismatch: 40,
89
+ nullability_mismatch: 41,
90
+ default_missing: 42,
91
+ default_mismatch: 43,
92
+
93
+ // Constraints after columns exist
94
+ primary_key_mismatch: 50,
95
+ unique_constraint_mismatch: 51,
96
+ index_mismatch: 52,
97
+ foreign_key_mismatch: 60,
98
+ };
99
+
100
+ function issueOrder(issue: SchemaIssue): number {
101
+ return ISSUE_KIND_ORDER[issue.kind] ?? 99;
102
+ }
103
+
104
+ // ============================================================================
105
+ // Conflict helpers
106
+ // ============================================================================
107
+
108
+ function issueConflict(
109
+ kind: SqlPlannerConflict['kind'],
110
+ summary: string,
111
+ location?: SqlPlannerConflict['location'],
112
+ ): SqlPlannerConflict {
113
+ return {
114
+ kind,
115
+ summary,
116
+ why: 'Use `migration new` to author a custom migration for this change.',
117
+ ...(location ? { location } : {}),
118
+ };
119
+ }
120
+
121
+ function isMissing(issue: SchemaIssue): boolean {
122
+ if (issue.kind === 'enum_values_changed') return false;
123
+ return issue.actual === undefined;
124
+ }
125
+
126
+ // ============================================================================
127
+ // Issue planner
128
+ // ============================================================================
129
+
130
+ export interface IssuePlannerOptions {
131
+ readonly issues: readonly SchemaIssue[];
132
+ readonly toContract: Contract<SqlStorage>;
133
+ readonly fromContract: Contract<SqlStorage> | null;
134
+ readonly schemaName: string;
135
+ readonly codecHooks: ReadonlyMap<string, CodecControlHooks>;
136
+ readonly storageTypes: Readonly<Record<string, StorageTypeInstance>>;
137
+ /**
138
+ * Current database schema IR. Strategies read this to detect whether a
139
+ * structure already exists (e.g. `buildSchemaLookupMap` for shared-temp-
140
+ * default safety, extension dependency checks). Defaults to an empty schema
141
+ * when omitted so the planner can still run over "fresh DB" contract
142
+ * snapshots.
143
+ */
144
+ readonly schema?: SqlSchemaIR;
145
+ /**
146
+ * Operation-class policy. `planIssues` filters calls whose `operationClass`
147
+ * is not in `policy.allowedOperationClasses` and surfaces them as conflicts
148
+ * instead of emitting disallowed DDL. Defaults to additive-only.
149
+ */
150
+ readonly policy?: MigrationOperationPolicy;
151
+ /**
152
+ * Framework components participating in this composition. Used by the
153
+ * dependency-install strategy to dispatch `databaseDependencies.init` at
154
+ * plan time.
155
+ */
156
+ readonly frameworkComponents?: ReadonlyArray<TargetBoundComponentDescriptor<'sql', string>>;
157
+ readonly strategies?: readonly CallMigrationStrategy[];
158
+ }
159
+
160
+ export interface IssuePlannerValue {
161
+ readonly calls: readonly PostgresOpFactoryCall[];
162
+ }
163
+
164
+ function toColumnSpec(
165
+ name: string,
166
+ column: StorageColumn,
167
+ codecHooks: ReadonlyMap<string, CodecControlHooks>,
168
+ storageTypes: Readonly<Record<string, StorageTypeInstance>>,
169
+ ): ColumnSpec {
170
+ return {
171
+ name,
172
+ typeSql: buildColumnTypeSql(
173
+ column,
174
+ codecHooks as Map<string, CodecControlHooks>,
175
+ storageTypes as Record<string, StorageTypeInstance>,
176
+ ),
177
+ defaultSql: buildColumnDefaultSql(column.default, column),
178
+ nullable: column.nullable,
179
+ };
180
+ }
181
+
182
+ function mapIssueToCall(
183
+ issue: SchemaIssue,
184
+ ctx: StrategyContext,
185
+ ): Result<readonly PostgresOpFactoryCall[], SqlPlannerConflict> {
186
+ const { schemaName, codecHooks, storageTypes } = ctx;
187
+
188
+ switch (issue.kind) {
189
+ case 'missing_table': {
190
+ if (!issue.table)
191
+ return notOk(
192
+ issueConflict('unsupportedOperation', 'Missing table issue has no table name'),
193
+ );
194
+ const contractTable = ctx.toContract.storage.tables[issue.table];
195
+ if (!contractTable) {
196
+ return notOk(
197
+ issueConflict(
198
+ 'unsupportedOperation',
199
+ `Table "${issue.table}" reported missing but not found in destination contract`,
200
+ ),
201
+ );
202
+ }
203
+ const columns: ColumnSpec[] = Object.entries(contractTable.columns).map(([name, column]) =>
204
+ toColumnSpec(name, column, codecHooks, storageTypes),
205
+ );
206
+ const primaryKey = contractTable.primaryKey
207
+ ? { columns: contractTable.primaryKey.columns }
208
+ : undefined;
209
+ const calls: PostgresOpFactoryCall[] = [
210
+ new CreateTableCall(schemaName, issue.table, columns, primaryKey),
211
+ ];
212
+ for (const index of contractTable.indexes) {
213
+ const indexName = index.name ?? `${issue.table}_${index.columns.join('_')}_idx`;
214
+ calls.push(new CreateIndexCall(schemaName, issue.table, indexName, [...index.columns]));
215
+ }
216
+ const explicitIndexColumnSets = new Set(
217
+ contractTable.indexes.map((idx) => idx.columns.join(',')),
218
+ );
219
+ for (const fk of contractTable.foreignKeys) {
220
+ if (fk.constraint) {
221
+ const fkName = fk.name ?? `${issue.table}_${fk.columns.join('_')}_fkey`;
222
+ const fkSpec: ForeignKeySpec = {
223
+ name: fkName,
224
+ columns: fk.columns,
225
+ references: { table: fk.references.table, columns: fk.references.columns },
226
+ ...(fk.onDelete !== undefined && { onDelete: fk.onDelete }),
227
+ ...(fk.onUpdate !== undefined && { onUpdate: fk.onUpdate }),
228
+ };
229
+ calls.push(new AddForeignKeyCall(schemaName, issue.table, fkSpec));
230
+ }
231
+ if (fk.index && !explicitIndexColumnSets.has(fk.columns.join(','))) {
232
+ const indexName = `${issue.table}_${fk.columns.join('_')}_idx`;
233
+ calls.push(new CreateIndexCall(schemaName, issue.table, indexName, [...fk.columns]));
234
+ }
235
+ }
236
+ for (const unique of contractTable.uniques) {
237
+ const constraintName = unique.name ?? `${issue.table}_${unique.columns.join('_')}_key`;
238
+ calls.push(new AddUniqueCall(schemaName, issue.table, constraintName, [...unique.columns]));
239
+ }
240
+ return ok(calls);
241
+ }
242
+
243
+ case 'missing_column':
244
+ if (!issue.table || !issue.column)
245
+ return notOk(
246
+ issueConflict('unsupportedOperation', 'Missing column issue has no table/column name'),
247
+ );
248
+ {
249
+ const column = ctx.toContract.storage.tables[issue.table]?.columns[issue.column];
250
+ if (!column)
251
+ return notOk(
252
+ issueConflict(
253
+ 'unsupportedOperation',
254
+ `Column "${issue.table}"."${issue.column}" not in destination contract`,
255
+ ),
256
+ );
257
+ return ok([
258
+ new AddColumnCall(
259
+ schemaName,
260
+ issue.table,
261
+ toColumnSpec(issue.column, column, codecHooks, storageTypes),
262
+ ),
263
+ ]);
264
+ }
265
+
266
+ case 'default_missing':
267
+ if (!issue.table || !issue.column)
268
+ return notOk(
269
+ issueConflict('unsupportedOperation', 'Default missing issue has no table/column name'),
270
+ );
271
+ {
272
+ const column = ctx.toContract.storage.tables[issue.table]?.columns[issue.column];
273
+ if (!column?.default) {
274
+ return notOk(
275
+ issueConflict(
276
+ 'unsupportedOperation',
277
+ `Column "${issue.table}"."${issue.column}" has no default in contract`,
278
+ ),
279
+ );
280
+ }
281
+ const defaultSql = buildColumnDefaultSql(column.default, column);
282
+ if (!defaultSql) return ok([]);
283
+ return ok([new SetDefaultCall(schemaName, issue.table, issue.column, defaultSql)]);
284
+ }
285
+
286
+ case 'extra_table':
287
+ if (!issue.table)
288
+ return notOk(issueConflict('unsupportedOperation', 'Extra table issue has no table name'));
289
+ return ok([new DropTableCall(schemaName, issue.table)]);
290
+
291
+ case 'extra_column':
292
+ if (!issue.table || !issue.column)
293
+ return notOk(
294
+ issueConflict('unsupportedOperation', 'Extra column issue has no table/column name'),
295
+ );
296
+ return ok([new DropColumnCall(schemaName, issue.table, issue.column)]);
297
+
298
+ case 'extra_index':
299
+ if (!issue.table || !issue.indexOrConstraint)
300
+ return notOk(
301
+ issueConflict('unsupportedOperation', 'Extra index issue has no table/index name'),
302
+ );
303
+ return ok([new DropIndexCall(schemaName, issue.table, issue.indexOrConstraint)]);
304
+
305
+ case 'extra_unique_constraint':
306
+ case 'extra_foreign_key':
307
+ case 'extra_primary_key': {
308
+ if (!issue.table)
309
+ return notOk(
310
+ issueConflict(
311
+ 'unsupportedOperation',
312
+ 'Extra constraint issue has no table/constraint name',
313
+ ),
314
+ );
315
+ // `extra_primary_key` issues don't carry a constraint name — the
316
+ // verifier only has the table. Fall back to `<table>_pkey`, matching
317
+ // Postgres' default PK constraint naming and the old reconciliation
318
+ // planner's behavior.
319
+ const constraintName =
320
+ issue.indexOrConstraint ??
321
+ (issue.kind === 'extra_primary_key' ? `${issue.table}_pkey` : undefined);
322
+ if (!constraintName)
323
+ return notOk(
324
+ issueConflict(
325
+ 'unsupportedOperation',
326
+ 'Extra constraint issue has no table/constraint name',
327
+ ),
328
+ );
329
+ const kindMap = {
330
+ extra_unique_constraint: 'unique' as const,
331
+ extra_foreign_key: 'foreignKey' as const,
332
+ extra_primary_key: 'primaryKey' as const,
333
+ };
334
+ return ok([
335
+ new DropConstraintCall(schemaName, issue.table, constraintName, kindMap[issue.kind]),
336
+ ]);
337
+ }
338
+
339
+ case 'extra_default':
340
+ if (!issue.table || !issue.column)
341
+ return notOk(
342
+ issueConflict('unsupportedOperation', 'Extra default issue has no table/column name'),
343
+ );
344
+ return ok([new DropDefaultCall(schemaName, issue.table, issue.column)]);
345
+
346
+ case 'nullability_mismatch': {
347
+ if (!issue.table || !issue.column)
348
+ return notOk(
349
+ issueConflict('nullabilityConflict', 'Nullability mismatch has no table/column name'),
350
+ );
351
+ const column = ctx.toContract.storage.tables[issue.table]?.columns[issue.column];
352
+ if (!column)
353
+ return notOk(
354
+ issueConflict(
355
+ 'nullabilityConflict',
356
+ `Column "${issue.table}"."${issue.column}" not found in destination contract`,
357
+ ),
358
+ );
359
+ return ok(
360
+ column.nullable
361
+ ? [new DropNotNullCall(schemaName, issue.table, issue.column)]
362
+ : [new SetNotNullCall(schemaName, issue.table, issue.column)],
363
+ );
364
+ }
365
+
366
+ case 'type_mismatch':
367
+ if (!issue.table || !issue.column)
368
+ return notOk(issueConflict('typeMismatch', 'Type mismatch has no table/column name'));
369
+ {
370
+ const column = ctx.toContract.storage.tables[issue.table]?.columns[issue.column];
371
+ if (!column)
372
+ return notOk(
373
+ issueConflict(
374
+ 'typeMismatch',
375
+ `Column "${issue.table}"."${issue.column}" not in destination contract`,
376
+ ),
377
+ );
378
+ const hooksMap = codecHooks as Map<string, CodecControlHooks>;
379
+ const typesMap = storageTypes as Record<string, StorageTypeInstance>;
380
+ const qualifiedTargetType = buildColumnTypeSql(column, hooksMap, typesMap, false);
381
+ const formatTypeExpected = buildExpectedFormatType(column, hooksMap, typesMap);
382
+ return ok([
383
+ new AlterColumnTypeCall(schemaName, issue.table, issue.column, {
384
+ qualifiedTargetType,
385
+ formatTypeExpected,
386
+ rawTargetTypeForLabel: qualifiedTargetType,
387
+ }),
388
+ ]);
389
+ }
390
+
391
+ case 'default_mismatch':
392
+ if (!issue.table || !issue.column)
393
+ return notOk(
394
+ issueConflict('unsupportedOperation', 'Default mismatch has no table/column name'),
395
+ );
396
+ {
397
+ const column = ctx.toContract.storage.tables[issue.table]?.columns[issue.column];
398
+ if (!column?.default) return ok([]);
399
+ const defaultSql = buildColumnDefaultSql(column.default, column);
400
+ if (!defaultSql) return ok([]);
401
+ return ok([
402
+ new SetDefaultCall(schemaName, issue.table, issue.column, defaultSql, 'widening'),
403
+ ]);
404
+ }
405
+
406
+ case 'primary_key_mismatch':
407
+ if (!issue.table)
408
+ return notOk(issueConflict('indexIncompatible', 'Primary key issue has no table name'));
409
+ if (isMissing(issue)) {
410
+ const pk = ctx.toContract.storage.tables[issue.table]?.primaryKey;
411
+ if (!pk)
412
+ return notOk(
413
+ issueConflict('indexIncompatible', `No primary key in contract for "${issue.table}"`),
414
+ );
415
+ const constraintName = pk.name ?? `${issue.table}_pkey`;
416
+ return ok([new AddPrimaryKeyCall(schemaName, issue.table, constraintName, pk.columns)]);
417
+ }
418
+ return notOk(
419
+ issueConflict(
420
+ 'indexIncompatible',
421
+ `Primary key on "${issue.table}" has different columns (expected: ${issue.expected}, actual: ${issue.actual})`,
422
+ { table: issue.table },
423
+ ),
424
+ );
425
+
426
+ case 'unique_constraint_mismatch':
427
+ if (!issue.table)
428
+ return notOk(
429
+ issueConflict('indexIncompatible', 'Unique constraint issue has no table name'),
430
+ );
431
+ if (isMissing(issue) && issue.expected) {
432
+ const columns = issue.expected.split(', ');
433
+ const constraintName = `${issue.table}_${columns.join('_')}_key`;
434
+ return ok([new AddUniqueCall(schemaName, issue.table, constraintName, columns)]);
435
+ }
436
+ return notOk(
437
+ issueConflict(
438
+ 'indexIncompatible',
439
+ `Unique constraint on "${issue.table}" differs (expected: ${issue.expected}, actual: ${issue.actual})`,
440
+ { table: issue.table },
441
+ ),
442
+ );
443
+
444
+ case 'index_mismatch':
445
+ if (!issue.table)
446
+ return notOk(issueConflict('indexIncompatible', 'Index issue has no table name'));
447
+ if (isMissing(issue) && issue.expected) {
448
+ const columns = issue.expected.split(', ');
449
+ const indexName = `${issue.table}_${columns.join('_')}_idx`;
450
+ return ok([new CreateIndexCall(schemaName, issue.table, indexName, columns)]);
451
+ }
452
+ return notOk(
453
+ issueConflict(
454
+ 'indexIncompatible',
455
+ `Index on "${issue.table}" differs (expected: ${issue.expected}, actual: ${issue.actual})`,
456
+ { table: issue.table },
457
+ ),
458
+ );
459
+
460
+ case 'foreign_key_mismatch':
461
+ if (!issue.table)
462
+ return notOk(issueConflict('foreignKeyConflict', 'Foreign key issue has no table name'));
463
+ if (isMissing(issue) && issue.expected) {
464
+ const arrowIdx = issue.expected.indexOf(' -> ');
465
+ if (arrowIdx >= 0) {
466
+ const columns = issue.expected.slice(0, arrowIdx).split(', ');
467
+ const fkName = `${issue.table}_${columns.join('_')}_fkey`;
468
+ const fk = ctx.toContract.storage.tables[issue.table]?.foreignKeys.find(
469
+ (k) => k.columns.join(', ') === columns.join(', '),
470
+ );
471
+ if (fk) {
472
+ const fkSpec: ForeignKeySpec = {
473
+ name: fkName,
474
+ columns: fk.columns,
475
+ references: { table: fk.references.table, columns: fk.references.columns },
476
+ ...(fk.onDelete !== undefined && { onDelete: fk.onDelete }),
477
+ ...(fk.onUpdate !== undefined && { onUpdate: fk.onUpdate }),
478
+ };
479
+ return ok([new AddForeignKeyCall(schemaName, issue.table, fkSpec)]);
480
+ }
481
+ return notOk(
482
+ issueConflict(
483
+ 'foreignKeyConflict',
484
+ `Foreign key on "${issue.table}" (${columns.join(', ')}) not found in destination contract`,
485
+ { table: issue.table },
486
+ ),
487
+ );
488
+ }
489
+ }
490
+ return notOk(
491
+ issueConflict(
492
+ 'foreignKeyConflict',
493
+ `Foreign key on "${issue.table}" differs (expected: ${issue.expected}, actual: ${issue.actual})`,
494
+ { table: issue.table },
495
+ ),
496
+ );
497
+
498
+ case 'type_missing': {
499
+ if (!issue.typeName)
500
+ return notOk(issueConflict('unsupportedOperation', 'Type missing issue has no typeName'));
501
+ const typeInstance = ctx.toContract.storage.types?.[issue.typeName];
502
+ if (!typeInstance) {
503
+ return notOk(
504
+ issueConflict(
505
+ 'unsupportedOperation',
506
+ `Type "${issue.typeName}" reported missing but not found in destination contract`,
507
+ ),
508
+ );
509
+ }
510
+ if (typeInstance.codecId.startsWith('pg/enum')) {
511
+ const values = (typeInstance.typeParams['values'] ?? []) as readonly string[];
512
+ return ok([new CreateEnumTypeCall(schemaName, typeInstance.nativeType, values)]);
513
+ }
514
+ return notOk(
515
+ issueConflict(
516
+ 'unsupportedOperation',
517
+ `Type "${issue.typeName}" uses codec "${typeInstance.codecId}" — only enum types are supported`,
518
+ ),
519
+ );
520
+ }
521
+
522
+ case 'type_values_mismatch':
523
+ return notOk(
524
+ issueConflict(
525
+ 'unsupportedOperation',
526
+ `Type "${issue.typeName ?? 'unknown'}" values differ — type alteration not yet supported`,
527
+ ),
528
+ );
529
+
530
+ case 'dependency_missing':
531
+ if (!issue.dependencyId)
532
+ return notOk(
533
+ issueConflict('unsupportedOperation', 'Dependency missing issue has no dependencyId'),
534
+ );
535
+ if (issue.dependencyId.startsWith('ext:')) {
536
+ return ok([new CreateExtensionCall(issue.dependencyId.slice(4))]);
537
+ }
538
+ if (issue.dependencyId.startsWith('schema:')) {
539
+ return ok([new CreateSchemaCall(issue.dependencyId.slice(7))]);
540
+ }
541
+ return notOk(
542
+ issueConflict('unsupportedOperation', `Unknown dependency type: ${issue.dependencyId}`),
543
+ );
544
+
545
+ default:
546
+ return notOk(
547
+ issueConflict(
548
+ 'unsupportedOperation',
549
+ `Unhandled issue kind: ${(issue as SchemaIssue).kind}`,
550
+ ),
551
+ );
552
+ }
553
+ }
554
+
555
+ /**
556
+ * Classifies calls into dependency order categories for correct DDL sequencing.
557
+ */
558
+ type CallCategory =
559
+ | 'dep'
560
+ | 'drop'
561
+ | 'table'
562
+ | 'column'
563
+ | 'alter'
564
+ | 'primaryKey'
565
+ | 'unique'
566
+ | 'index'
567
+ | 'foreignKey';
568
+
569
+ /**
570
+ * Classifies calls into DDL sequencing buckets. The order matches the
571
+ * legacy walk-schema planner's emission order so `db init` and `db update`
572
+ * produce byte-identical plans for the shared shape (deps → drops → tables
573
+ * → columns → alters → PKs → uniques → indexes → FKs).
574
+ */
575
+ function classifyCall(call: PostgresOpFactoryCall): CallCategory {
576
+ switch (call.factoryName) {
577
+ case 'createExtension':
578
+ case 'createSchema':
579
+ case 'createEnumType':
580
+ case 'addEnumValues':
581
+ case 'dropEnumType':
582
+ case 'renameType':
583
+ return 'dep';
584
+ case 'dropTable':
585
+ case 'dropColumn':
586
+ case 'dropConstraint':
587
+ case 'dropIndex':
588
+ case 'dropDefault':
589
+ return 'drop';
590
+ case 'createTable':
591
+ return 'table';
592
+ case 'addColumn':
593
+ return 'column';
594
+ case 'alterColumnType':
595
+ case 'setNotNull':
596
+ case 'dropNotNull':
597
+ case 'setDefault':
598
+ return 'alter';
599
+ case 'addPrimaryKey':
600
+ return 'primaryKey';
601
+ case 'addUnique':
602
+ return 'unique';
603
+ case 'createIndex':
604
+ return 'index';
605
+ case 'addForeignKey':
606
+ return 'foreignKey';
607
+ case 'rawSql': {
608
+ // Install ops (`dependencyInstallCallStrategy`) and type ops
609
+ // (`storageTypePlanCallStrategy`) both lift raw `SqlMigrationPlanOperation`s
610
+ // through `RawSqlCall` to preserve the component-declared label and
611
+ // precheck/postcheck. Classification falls back to inspecting the
612
+ // underlying op's target details (`objectType: 'type'`) and id prefix
613
+ // (`extension.*` / `schema.*`).
614
+ const op = (
615
+ call as {
616
+ op?: {
617
+ id?: string;
618
+ target?: { details?: { objectType?: string } };
619
+ };
620
+ }
621
+ ).op;
622
+ const objectType = op?.target?.details?.objectType;
623
+ if (objectType === 'type') return 'dep';
624
+ const id = typeof op?.id === 'string' ? op.id : '';
625
+ if (id.startsWith('extension.') || id.startsWith('schema.')) return 'dep';
626
+ return 'alter';
627
+ }
628
+ default:
629
+ return 'alter';
630
+ }
631
+ }
632
+
633
+ /** Stable lexical key used to order issues within the same kind bucket. */
634
+ function issueKey(issue: SchemaIssue): string {
635
+ const table = 'table' in issue && typeof issue.table === 'string' ? issue.table : '';
636
+ const column = 'column' in issue && typeof issue.column === 'string' ? issue.column : '';
637
+ const name =
638
+ 'indexOrConstraint' in issue && typeof issue.indexOrConstraint === 'string'
639
+ ? issue.indexOrConstraint
640
+ : '';
641
+ return `${table}\u0000${column}\u0000${name}`;
642
+ }
643
+
644
+ // When no policy is explicitly supplied (test-only path; production callers
645
+ // always pass one), allow every class so strategies that gate on
646
+ // `'data'` (data-safe placeholders) still fire — the test is treated as
647
+ // trusted. Filtering of actual emitted calls only runs when a policy was
648
+ // explicitly provided (see `policyProvided` below).
649
+ const DEFAULT_POLICY: MigrationOperationPolicy = {
650
+ allowedOperationClasses: ['additive', 'widening', 'destructive', 'data'],
651
+ };
652
+
653
+ function emptySchemaIR(): SqlSchemaIR {
654
+ return { tables: {}, dependencies: [] };
655
+ }
656
+
657
+ function conflictKindForCall(call: PostgresOpFactoryCall): SqlPlannerConflict['kind'] {
658
+ switch (call.factoryName) {
659
+ case 'alterColumnType':
660
+ return 'typeMismatch';
661
+ case 'setNotNull':
662
+ case 'dropNotNull':
663
+ return 'nullabilityConflict';
664
+ case 'addForeignKey':
665
+ case 'dropConstraint':
666
+ return 'foreignKeyConflict';
667
+ case 'createIndex':
668
+ case 'dropIndex':
669
+ return 'indexIncompatible';
670
+ default:
671
+ return 'missingButNonAdditive';
672
+ }
673
+ }
674
+
675
+ function locationForCall(call: PostgresOpFactoryCall): SqlPlannerConflict['location'] | undefined {
676
+ // Most Postgres call classes expose `tableName`/`columnName`/`indexName`/
677
+ // `constraintName` as readonly fields. We avoid `toOp()` here because a
678
+ // `DataTransformCall` intentionally throws from `toOp`.
679
+ const anyCall = call as unknown as {
680
+ tableName?: string;
681
+ columnName?: string;
682
+ indexName?: string;
683
+ constraintName?: string;
684
+ typeName?: string;
685
+ };
686
+ const location: {
687
+ table?: string;
688
+ column?: string;
689
+ index?: string;
690
+ constraint?: string;
691
+ type?: string;
692
+ } = {};
693
+ if (anyCall.tableName) location.table = anyCall.tableName;
694
+ if (anyCall.columnName) location.column = anyCall.columnName;
695
+ if (anyCall.indexName) location.index = anyCall.indexName;
696
+ if (anyCall.constraintName) location.constraint = anyCall.constraintName;
697
+ if (anyCall.typeName) location.type = anyCall.typeName;
698
+ return Object.keys(location).length > 0 ? (location as SqlPlannerConflictLocation) : undefined;
699
+ }
700
+
701
+ function conflictForDisallowedCall(
702
+ call: PostgresOpFactoryCall,
703
+ allowed: readonly string[],
704
+ ): SqlPlannerConflict {
705
+ const summary = `Operation "${call.label}" requires class "${call.operationClass}", but policy allows only: ${allowed.join(', ')}`;
706
+ const location = locationForCall(call);
707
+ return {
708
+ kind: conflictKindForCall(call),
709
+ summary,
710
+ why: 'Use `migration new` to author a custom migration for this change.',
711
+ ...(location ? { location } : {}),
712
+ };
713
+ }
714
+
715
+ export function planIssues(
716
+ options: IssuePlannerOptions,
717
+ ): Result<IssuePlannerValue, readonly SqlPlannerConflict[]> {
718
+ // When no policy is supplied, `planIssues` treats the call as trusted (the
719
+ // caller — typically a test — has already vetted the issues). Only explicit
720
+ // policies gate operation classes into conflicts.
721
+ // `PostgresMigrationPlanner` always passes an explicit policy.
722
+ const policyProvided = options.policy !== undefined;
723
+ const policy = options.policy ?? DEFAULT_POLICY;
724
+ const schema = options.schema ?? emptySchemaIR();
725
+ const frameworkComponents = options.frameworkComponents ?? [];
726
+
727
+ const context: StrategyContext = {
728
+ toContract: options.toContract,
729
+ fromContract: options.fromContract,
730
+ schemaName: options.schemaName,
731
+ codecHooks: options.codecHooks,
732
+ storageTypes: options.storageTypes,
733
+ schema,
734
+ policy,
735
+ frameworkComponents,
736
+ };
737
+
738
+ const strategies = options.strategies ?? postgresPlannerStrategies;
739
+
740
+ let remaining = options.issues;
741
+ const recipeCalls: PostgresOpFactoryCall[] = [];
742
+ const bucketablePatternCalls: PostgresOpFactoryCall[] = [];
743
+
744
+ for (const strategy of strategies) {
745
+ const result = strategy(remaining, context);
746
+ if (result.kind === 'match') {
747
+ remaining = result.issues;
748
+ if (result.recipe) {
749
+ recipeCalls.push(...result.calls);
750
+ } else {
751
+ bucketablePatternCalls.push(...result.calls);
752
+ }
753
+ }
754
+ }
755
+
756
+ const sorted = [...remaining].sort((a, b) => {
757
+ const kindDelta = issueOrder(a) - issueOrder(b);
758
+ if (kindDelta !== 0) return kindDelta;
759
+ const keyA = issueKey(a);
760
+ const keyB = issueKey(b);
761
+ return keyA < keyB ? -1 : keyA > keyB ? 1 : 0;
762
+ });
763
+
764
+ const defaultCalls: PostgresOpFactoryCall[] = [];
765
+ const conflicts: SqlPlannerConflict[] = [];
766
+
767
+ for (const issue of sorted) {
768
+ const result = mapIssueToCall(issue, context);
769
+ if (result.ok) {
770
+ defaultCalls.push(...result.value);
771
+ } else {
772
+ conflicts.push(result.failure);
773
+ }
774
+ }
775
+
776
+ // Policy gating: drop calls whose operation class is not allowed and
777
+ // surface a conflict describing the disallowed op. Applies to both strategy
778
+ // output and default-mapped output. Only active when the caller explicitly
779
+ // supplied a policy — direct unit-test invocations (which pass no policy)
780
+ // stay as pass-through and keep destructive recipe steps intact.
781
+ const allowed = policy.allowedOperationClasses;
782
+ let gatedDefault = defaultCalls;
783
+ let gatedRecipe = recipeCalls;
784
+ let gatedBucketable = bucketablePatternCalls;
785
+ if (policyProvided) {
786
+ const keepIfAllowed = (bucket: PostgresOpFactoryCall[]) => (call: PostgresOpFactoryCall) => {
787
+ if (allowed.includes(call.operationClass)) {
788
+ bucket.push(call);
789
+ return;
790
+ }
791
+ conflicts.push(conflictForDisallowedCall(call, allowed));
792
+ };
793
+ const gatedDefaultBucket: PostgresOpFactoryCall[] = [];
794
+ const gatedRecipeBucket: PostgresOpFactoryCall[] = [];
795
+ const gatedBucketableBucket: PostgresOpFactoryCall[] = [];
796
+ defaultCalls.forEach(keepIfAllowed(gatedDefaultBucket));
797
+ recipeCalls.forEach(keepIfAllowed(gatedRecipeBucket));
798
+ bucketablePatternCalls.forEach(keepIfAllowed(gatedBucketableBucket));
799
+ gatedDefault = gatedDefaultBucket;
800
+ gatedRecipe = gatedRecipeBucket;
801
+ gatedBucketable = gatedBucketableBucket;
802
+ }
803
+
804
+ if (conflicts.length > 0) {
805
+ return notOk(conflicts);
806
+ }
807
+
808
+ // Recipe strategies (`enumChangeCallStrategy`, `notNullBackfillCallStrategy`,
809
+ // etc.) emit a cohesive sequence that must stay contiguous. They are
810
+ // inserted at a single pattern slot. Non-recipe pattern strategies
811
+ // (`dependencyInstallCallStrategy`, `storageTypePlanCallStrategy`,
812
+ // `notNullAddColumnCallStrategy`) produce individually classifiable calls
813
+ // that slot into DDL buckets alongside default-mapped calls.
814
+ const combinedBucketable = [...gatedDefault, ...gatedBucketable];
815
+ const byCategory = (cat: CallCategory) =>
816
+ combinedBucketable.filter((c) => classifyCall(c) === cat);
817
+
818
+ const calls: PostgresOpFactoryCall[] = [
819
+ ...byCategory('dep'),
820
+ ...byCategory('drop'),
821
+ ...byCategory('table'),
822
+ ...byCategory('column'),
823
+ ...gatedRecipe,
824
+ ...byCategory('alter'),
825
+ ...byCategory('primaryKey'),
826
+ ...byCategory('unique'),
827
+ ...byCategory('index'),
828
+ ...byCategory('foreignKey'),
829
+ ];
830
+
831
+ return ok({ calls });
832
+ }