@prisma/orm-target-mongo 8.0.0-rc.24-dev.15 → 8.0.0-rc.24-dev.16

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 (28) hide show
  1. package/dist/{control-BlMckWwK.mjs → control-ADZk3Q1S.mjs} +3059 -307
  2. package/dist/control-ADZk3Q1S.mjs.map +1 -0
  3. package/dist/{control-BG4g6muN.mjs → control-C9gGCopP.mjs} +2 -2
  4. package/dist/{control-BG4g6muN.mjs.map → control-C9gGCopP.mjs.map} +1 -1
  5. package/dist/{control-OiC131Dv.d.mts → control-Dc2P_CN-.d.mts} +15 -6
  6. package/dist/control-Dc2P_CN-.d.mts.map +1 -0
  7. package/dist/driver.mjs +1 -1
  8. package/dist/driver__control.mjs +1 -1
  9. package/dist/driver__runtime.mjs +1 -1
  10. package/dist/driver__runtime.mjs.map +1 -1
  11. package/dist/{migration-DASk9QNA.d.mts → migration-D78n7EUy.d.mts} +1 -1
  12. package/dist/{migration-factories-C5ktmt8c-BP8k9SnZ.mjs → migration-factories-RJZq8NhE-CigCh286.mjs} +621 -13
  13. package/dist/migration-factories-RJZq8NhE-CigCh286.mjs.map +1 -0
  14. package/dist/{mongo-driver-CfJ69N8C-B5aKUbjW.mjs → mongo-driver-BV3a7kcA-BJrV90Qw.mjs} +3 -3
  15. package/dist/{mongo-driver-CfJ69N8C-B5aKUbjW.mjs.map → mongo-driver-BV3a7kcA-BJrV90Qw.mjs.map} +1 -1
  16. package/dist/{op-factory-call-BCgwbVJe-DhcJRxAb.d.mts → op-factory-call-vacb7XmE-sHhC1Hit.d.mts} +151 -5
  17. package/dist/op-factory-call-vacb7XmE-sHhC1Hit.d.mts.map +1 -0
  18. package/dist/target.d.mts +4 -4
  19. package/dist/target.mjs +3 -3
  20. package/dist/target__control.d.mts +2 -2
  21. package/dist/target__control.mjs +1 -1
  22. package/dist/target__migration.d.mts +3 -3
  23. package/dist/target__migration.mjs +2 -2
  24. package/package.json +10 -10
  25. package/dist/control-BlMckWwK.mjs.map +0 -1
  26. package/dist/control-OiC131Dv.d.mts.map +0 -1
  27. package/dist/migration-factories-C5ktmt8c-BP8k9SnZ.mjs.map +0 -1
  28. package/dist/op-factory-call-BCgwbVJe-DhcJRxAb.d.mts.map +0 -1
@@ -1,6 +1,6 @@
1
1
  import { t as mongoTargetError } from "./mongo-target-errors-Btf3ML1w-QOtEG7EX.mjs";
2
2
  import { t as mongoTargetDescriptorMeta } from "./descriptor-meta-QyKsSxUQ-Cl1ZCbq3.mjs";
3
- import { a as createIndex, c as documentsNotOfType, d as fillField, f as isConvertTarget, g as renameField, h as renameCollection, i as createCollection, l as dropCollection, n as castsFrom, r as collMod, s as documentsMissingOrNull, t as castField, u as dropIndex, v as unrewritableFieldNameReason, y as unsetField } from "./migration-factories-C5ktmt8c-BP8k9SnZ.mjs";
3
+ import { A as unrewritableKeyReason, C as renameCollection, E as replaceStoredValue, O as storedKeyPath, S as removeStoredValue, T as renameStoredKey, _ as keysOf, a as createIndex, c as documentsMissingOrNull, d as dropCollection, f as dropIndex, g as jsonPathText, h as isConvertTarget, i as createCollection, j as unsetField, k as unrewritableFieldNameReason, l as documentsNotOfType, m as fillStoredKey, n as castsFrom, p as fillField, r as collMod, s as deleteDocuments, t as castField, u as documentsWithObject, v as lacksKey, w as renameField, x as removeStoredKey } from "./migration-factories-RJZq8NhE-CigCh286.mjs";
4
4
  import { assembleDataTypes } from "@prisma/orm-framework/components/codec";
5
5
  import { bsonTypesOfCodec, temporaryValueOfCodec } from "@prisma/orm-family-mongo/contract/data-type";
6
6
  import { ifDefined } from "@prisma/orm-framework/utils/defined";
@@ -8,12 +8,12 @@ import { type } from "arktype";
8
8
  import { blindCast } from "@prisma/orm-framework/utils/casts";
9
9
  import { setFallbackTemporal } from "@prisma/orm-framework/type-implementations/temporal";
10
10
  import { NamespaceBase, UNBOUND_NAMESPACE_ID, elementCoordinates, freezeNode, hydrateNamespaceEntities } from "@prisma/orm-framework/components/ir";
11
- import { MongoCollection, MongoStorage, canonicalize, collectionControlPoliciesOf, collectionControlPolicyOf, collectionOfModel, defaultMongoIndexName, embeddingModelOf, hasOwnFieldStoredAs, indexLookupKey, modelAndBasesOf, modelFieldStoredAs, mongoModelsOf, otherModelsReached, ownEmbedStoredNamesOf, ownStoredNameOf, ownStoredNamesOf, propertyBsonTypesOf, validatorDocumentShapesOf, validatorFieldNamesOf, validatorRootShapesOf, variantFilterOf } from "@prisma/orm-family-mongo/contract";
11
+ import { notOk, ok } from "@prisma/orm-framework/utils/result";
12
+ import { MongoCollection, MongoStorage, canonicalize, collectionControlPoliciesOf, collectionControlPolicyOf, collectionOfModel, defaultMongoIndexName, embeddingModelOf, hasOwnFieldStoredAs, indexLookupKey, isEmbedRelation, modelAndBasesOf, modelFieldStoredAs, modelKey, mongoModelsOf, otherModelsReached, ownEmbedStoredNamesOf, ownStoredNameOf, ownStoredNamesOf, propertyBsonTypesOf, recursiveGroupsOf, sameKindOfDocumentIn, selectedVariantOf, validatorDocumentShapesOf, validatorFieldNamesOf, validatorRootShapesOf, valueObjectKey, valueObjectStoredNamesOf, variantFilter } from "@prisma/orm-family-mongo/contract";
12
13
  import { CollModCommand, CreateCollectionCommand, CreateIndexCommand, DropCollectionCommand, DropIndexCommand, ListCollectionsCommand, ListIndexesCommand, MongoAndExpr, MongoExistsExpr, MongoFieldFilter, MongoNotExpr, MongoOrExpr, RenameCollectionCommand, isInspectionCheckSource, isMongoDmlOperation } from "@prisma/orm-family-mongo/query-ast/control";
13
14
  import { AggregateCommand, MongoAddFieldsStage, MongoLimitStage, MongoLookupStage, MongoMatchStage, MongoMergeStage, MongoProjectStage, MongoSortStage, RawAggregateCommand, RawDeleteManyCommand, RawDeleteOneCommand, RawFindOneAndDeleteCommand, RawFindOneAndUpdateCommand, RawInsertManyCommand, RawInsertOneCommand, RawUpdateManyCommand, RawUpdateOneCommand } from "@prisma/orm-family-mongo/query-ast/execution";
14
15
  import { canonicalizeSchemasForVerification, contractToMongoSchemaIR, diffMongoSchemas, fieldRewriteOf } from "@prisma/orm-family-mongo/family/control";
15
- import { APP_SPACE_ID, VERIFY_CODE_SCHEMA_FAILURE, contractSnapshotJsonSpecifier, contractSnapshotTypesSpecifier, describeMigrationStatement, expectAbsent, expectPresent, extractCodecLookup, failedPostcheckWhy, failedPrecheckWhy, fieldStatementText, issueOutcome, migrationRunnerFailure, modelDisplayName, schemaCheckWhy, withDatabaseUnchanged, withFailingSpace } from "@prisma/orm-framework/components/control";
16
- import { notOk, ok } from "@prisma/orm-framework/utils/result";
16
+ import { APP_SPACE_ID, VERIFY_CODE_SCHEMA_FAILURE, contractSnapshotJsonSpecifier, contractSnapshotTypesSpecifier, describeMigrationStatement, enumValueCoordinateFromJson, enumValueCoordinateJson, enumValueStatementText, expectAbsent, expectPresent, extractCodecLookup, failedPostcheckWhy, failedPrecheckWhy, fieldStatementText, issueOutcome, migrationRunnerFailure, migrationSubjectKey, modelDisplayName, modelStatementText, schemaCheckWhy, valueObjectFieldStatementText, withDatabaseUnchanged, withFailingSpace } from "@prisma/orm-framework/components/control";
17
17
  import { Temporal } from "temporal-polyfill/full/implementation";
18
18
  import { MongoContractSerializerBase, MongoSchemaVerifierBase } from "@prisma/orm-family-mongo/family/ir";
19
19
  import { composeMongoEntityKinds } from "@prisma/orm-family-mongo/contract/entity-kinds";
@@ -22,7 +22,7 @@ import { MongoSchemaCollection, MongoSchemaIR, deepEqual } from "@prisma/orm-fam
22
22
  import { InternalError } from "@prisma/orm-framework/utils/internal-error";
23
23
  import { CliStructuredError } from "@prisma/orm-framework/errors/control";
24
24
  import { errorUnfilledPlaceholder } from "@prisma/orm-framework/errors/migration";
25
- import { TsExpression, jsonToTsSource, renderImports } from "@prisma/orm-framework/ts-render";
25
+ import { TsExpression, jsonToTsSource, renderImports, tsObjectSource, tsStringLiteral } from "@prisma/orm-framework/ts-render";
26
26
  import { Migration } from "@prisma/orm-toolchain/migration-tools/migration";
27
27
  import { resolveRequirementSpecifiers } from "@prisma/orm-framework/components/emission";
28
28
  import { detectScaffoldRuntime, shebangLineFor } from "@prisma/orm-toolchain/migration-tools/migration-ts";
@@ -153,7 +153,400 @@ var MongoTargetContractSerializer = class extends MongoContractSerializerBase {
153
153
  });
154
154
  }
155
155
  };
156
- const SEPARATE_THE_MODELS = "move the models to separate collections, or carry out the change in a dataTransform operation of a migration.ts, with a filter that selects only their documents";
156
+ const SHELL_SAFE = /^[A-Za-z0-9_.:/@%+,-]+$/;
157
+ /** A statement written as the CLI writes its flag: the text single-quoted when a shell would read one of its characters. */
158
+ function statementFlag(verb, text) {
159
+ return `--${verb} ${SHELL_SAFE.test(text) ? text : `'${text.replaceAll("'", `'\\''`)}'`}`;
160
+ }
161
+ /** A subject as the question names it: a field through the name a rename statement gives its model, as `subjectText` in the CLI's `plan-questions.ts` writes it. */
162
+ function subjectText(subject, contracts) {
163
+ switch (subject.kind) {
164
+ case "model": return modelStatementText(contracts.fromContract, subject);
165
+ case "field": {
166
+ const renamed = renamedModelOf(subject, contracts);
167
+ return renamed === void 0 ? fieldStatementText(contracts.fromContract, subject) : fieldStatementText(contracts.contract, {
168
+ ...renamed,
169
+ field: subject.field
170
+ });
171
+ }
172
+ case "enumValue": return enumValueStatementText(contracts.fromContract, subject);
173
+ case "valueObjectField": return valueObjectFieldStatementText(contracts.fromContract, subject);
174
+ case "storage": return subject.name;
175
+ }
176
+ }
177
+ function newFieldText(subject, contract) {
178
+ switch (subject.kind) {
179
+ case "field": return fieldStatementText(contract, subject);
180
+ case "valueObjectField": return valueObjectFieldStatementText(contract, subject);
181
+ case "model": return modelStatementText(contract, subject);
182
+ case "enumValue": return enumValueStatementText(contract, subject);
183
+ case "storage": return subject.name;
184
+ }
185
+ }
186
+ function removalText(subject, text) {
187
+ switch (subject.kind) {
188
+ case "model": return `model "${text}", whose documents the plan deletes`;
189
+ case "field": return `field "${text}", which the plan removes`;
190
+ case "enumValue": return `enum value "${text}", which the plan removes from the data`;
191
+ case "valueObjectField": return `value object field "${text}", which the plan removes`;
192
+ case "storage": return `"${text}", which the plan removes`;
193
+ }
194
+ }
195
+ /** The model a rename statement gives an origin model, if any. */
196
+ function renamedModelOf(origin, contracts) {
197
+ for (const statement of contracts.statements) if (statement.kind === "rename" && statement.entity === "model" && statement.from.namespaceId === origin.namespaceId && statement.from.model === origin.model) return statement.to;
198
+ }
199
+ /** The destination model an origin model is, under the name a rename statement gives it, when the destination has it. */
200
+ function destinationModelOf$1(origin, contracts) {
201
+ const destination = renamedModelOf(origin, contracts) ?? origin;
202
+ return modelAt(contracts.contract, destination) === void 0 ? void 0 : destination;
203
+ }
204
+ function membersOf$1(contract, value) {
205
+ return (contract.domain.namespaces[value.namespaceId]?.enum?.[value.enum]?.members ?? []).map(({ name }) => name);
206
+ }
207
+ function valueObjectFieldsOf(contract, field) {
208
+ const valueObject = valueObjectAt(contract.storage, {
209
+ plane: "storage",
210
+ namespaceId: field.namespaceId,
211
+ entityKind: "valueObject",
212
+ entityName: field.valueObject
213
+ });
214
+ return Object.keys(valueObject?.fields ?? {});
215
+ }
216
+ /** The names of `destination` that `origin` does not have and no rename statement names. */
217
+ function added(destination, origin, renamedTo) {
218
+ return destination.filter((name) => !origin.includes(name) && !renamedTo.includes(name));
219
+ }
220
+ /** The fields of the destination model a removed field can have been renamed to: those the origin model does not have and no rename names, as `newModelFields` in the CLI's `plan-questions.ts` finds them. */
221
+ function newModelFields(subject, contracts) {
222
+ const destination = destinationModelOf$1(subject, contracts);
223
+ if (destination === void 0) return [];
224
+ return added(Object.keys(modelAt(contracts.contract, destination)?.fields ?? {}), Object.keys(modelAt(contracts.fromContract, subject)?.fields ?? {}), contracts.statements.flatMap((statement) => statement.kind === "rename" && statement.entity === "field" && statement.to.namespaceId === destination.namespaceId && statement.to.model === destination.model ? [statement.to.field] : []));
225
+ }
226
+ /** The members a removed enum value can have been renamed to, as `onlyNewMember` in the CLI's `plan-questions.ts` finds them. */
227
+ function newMembers(subject, contracts) {
228
+ return added(membersOf$1(contracts.contract, subject), membersOf$1(contracts.fromContract, subject), contracts.statements.flatMap((statement) => statement.kind === "rename" && statement.entity === "enumValue" ? [statement.to.member] : []));
229
+ }
230
+ /** The fields a removed value object field can have been renamed to, as `newValueObjectFields` in the CLI's `plan-questions.ts` finds them. */
231
+ function newValueObjectFields(subject, contracts) {
232
+ return added(valueObjectFieldsOf(contracts.contract, subject), valueObjectFieldsOf(contracts.fromContract, subject), contracts.statements.flatMap((statement) => statement.kind === "rename" && statement.entity === "valueObjectField" ? [statement.to.field] : []));
233
+ }
234
+ /** The variants a removed variant can have been renamed to, as `newVariantsOfBase` in the CLI's `variants.ts` finds them. */
235
+ function newVariants(subject, contracts) {
236
+ const base = modelAt(contracts.fromContract, subject)?.base;
237
+ if (base === void 0) return [];
238
+ const coordinate = {
239
+ namespaceId: asNamespaceId(base.namespace),
240
+ model: base.model
241
+ };
242
+ const variantsOf = (contract) => Object.values(modelAt(contract, coordinate)?.variants ?? {}).map((variant) => ({
243
+ namespaceId: asNamespaceId(variant.namespace),
244
+ model: variant.model
245
+ }));
246
+ const key = ({ namespaceId, model }) => `${namespaceId}\0${model}`;
247
+ const renamedTo = contracts.statements.flatMap((statement) => statement.kind === "rename" && statement.entity === "model" ? [key(statement.to)] : []);
248
+ const before = variantsOf(contracts.fromContract).map(key);
249
+ return variantsOf(contracts.contract).filter((variant) => !before.includes(key(variant)) && !renamedTo.includes(key(variant)));
250
+ }
251
+ function only(candidates) {
252
+ return candidates.length === 1 ? candidates[0] : void 0;
253
+ }
254
+ /** The statement the question offers first for a removal, and whether the destination can keep its data, as `renameForm` in the CLI's `plan-questions.ts` writes it. */
255
+ function answerOf(subject, text, contracts) {
256
+ const parent = text.slice(0, text.lastIndexOf("."));
257
+ const renaming = (to) => statementFlag("rename", `${text}:${parent}.${to ?? "<new name>"}`);
258
+ switch (subject.kind) {
259
+ case "field": {
260
+ const candidates = newModelFields(subject, contracts);
261
+ return {
262
+ canKeep: candidates.length > 0,
263
+ keeping: renaming(only(candidates))
264
+ };
265
+ }
266
+ case "model": {
267
+ const destination = destinationModelOf$1(subject, contracts);
268
+ if (destination !== void 0) return {
269
+ canKeep: true,
270
+ keeping: statementFlag("convert", modelStatementText(contracts.contract, destination))
271
+ };
272
+ const variant = only(newVariants(subject, contracts));
273
+ return {
274
+ canKeep: variant !== void 0,
275
+ keeping: statementFlag("rename", `${text}:${variant === void 0 ? "<new name>" : modelStatementText(contracts.contract, variant)}`)
276
+ };
277
+ }
278
+ case "enumValue": return {
279
+ canKeep: false,
280
+ keeping: renaming(only(newMembers(subject, contracts)))
281
+ };
282
+ case "valueObjectField": return {
283
+ canKeep: false,
284
+ keeping: renaming(only(newValueObjectFields(subject, contracts)))
285
+ };
286
+ case "storage": return {
287
+ canKeep: false,
288
+ keeping: statementFlag("rename", `${text}:<new name>`)
289
+ };
290
+ }
291
+ }
292
+ const ORDER = {
293
+ field: 0,
294
+ valueObjectField: 1,
295
+ enumValue: 2,
296
+ storage: 3,
297
+ model: 4
298
+ };
299
+ /**
300
+ * What a plan-wide refusal leads with when the plan holds changes no statement carries: those
301
+ * changes, as their questions name them, and the statements that keep their data, as the questions
302
+ * offer them first. The planner cannot tell an unanswered change from one answered with
303
+ * `--delete`, so it says only that no `--rename` or `--convert` carries them. A field of a model the
304
+ * destination does not have is left out, since no field statement can keep its data. `undefined` when no
305
+ * removal has a field or model in the destination to keep its data in.
306
+ */
307
+ function unansweredChangesAdvice(changes, contracts) {
308
+ const unique = changes.filter((change, index) => changes.findIndex((other) => other.kind === change.kind && migrationSubjectKey(other.subject) === migrationSubjectKey(change.subject)) === index);
309
+ const removed = unique.filter(({ kind, subject }) => kind === "removed" && (subject.kind !== "field" || destinationModelOf$1(subject, contracts) !== void 0)).sort((a, b) => ORDER[a.subject.kind] - ORDER[b.subject.kind]).map(({ subject }) => {
310
+ const text = subjectText(subject, contracts);
311
+ return {
312
+ description: removalText(subject, text),
313
+ answer: answerOf(subject, text, contracts)
314
+ };
315
+ });
316
+ if (!removed.some(({ answer }) => answer.canKeep)) return void 0;
317
+ const filled = unique.filter((change) => change.kind === "newRequired").map(({ subject }) => `new required ${subject.kind === "valueObjectField" ? "value object field" : "field"} "${newFieldText(subject, contracts.contract)}", which the plan fills with a temporary value`);
318
+ const named = [...removed.map(({ description }) => description), ...filled];
319
+ const keeping = removed.map(({ answer }) => answer.keeping);
320
+ return `The plan holds changes that no --rename or --convert statement carries: ${named.join("; ")}. To keep their data, run the command again with: ${keeping.join("; ")}.`;
321
+ }
322
+ const ONE_AT_A_TIME = "MongoDB applies a migration's operations one at a time, without a transaction, and knows that an operation already ran only from the documents.";
323
+ /** Advice to make a change as two changes to the contract, the first applied before the second is made. */
324
+ function inTwoSteps(first, second) {
325
+ return `Make the change as two changes to the contract, and apply the first, by a migration or by db update, before you make the second: first ${first}; then ${second}.`;
326
+ }
327
+ const RENAME_ADVICE = inTwoSteps("rename the keys or fields to names nothing else uses", "rename them to their final names");
328
+ const FILL_ADVICE = inTwoSteps("make the rename, without the new required field", "add the new field");
329
+ const fillAfterRemovalAdvice = (noun) => inTwoSteps(`remove the old ${noun}`, "add the new one");
330
+ const removeBeforeRenameAdvice = (noun) => inTwoSteps(`remove the ${noun} whose name the rename takes`, "make the rename");
331
+ function variantDiscriminatorFilter(variant) {
332
+ return variant === void 0 ? void 0 : {
333
+ kind: "variant",
334
+ ...variant
335
+ };
336
+ }
337
+ function key(collection, field, path = void 0) {
338
+ return {
339
+ kind: "key",
340
+ collection,
341
+ field,
342
+ path: path === void 0 ? void 0 : parsedPath(field, path).text
343
+ };
344
+ }
345
+ function parsedPath(field, path) {
346
+ const parsed = storedKeyPath(field, path);
347
+ if (!parsed.ok) throw new InternalError(`The planner wrote path "${path}" for field ${field}, which does not parse`);
348
+ return {
349
+ ...parsed.value,
350
+ text: jsonPathText(parsed.value.path.steps)
351
+ };
352
+ }
353
+ function renamedPath(field, path, to) {
354
+ return jsonPathText([...parsedPath(field, path).objects, {
355
+ kind: "member",
356
+ name: to
357
+ }]);
358
+ }
359
+ function value(at, stored) {
360
+ return {
361
+ kind: "value",
362
+ collection: at.collection,
363
+ field: at.field,
364
+ path: at.path === void 0 ? void 0 : parsedPath(at.field, at.path).text,
365
+ value: stored
366
+ };
367
+ }
368
+ function filterReadsOf(collection, filter) {
369
+ if (filter === void 0) return [];
370
+ const values = filter.kind === "variant" ? [filter.value] : filter.values;
371
+ return [key(collection, filter.field), ...values.map((each) => value({
372
+ collection,
373
+ field: filter.field
374
+ }, each))];
375
+ }
376
+ function accessOf(rewrite) {
377
+ if (rewrite.kind === "deleteDocuments") {
378
+ const { collection } = rewrite.call.options;
379
+ const { field, value: deleted } = rewrite.deletedVariant;
380
+ return {
381
+ role: "empties",
382
+ empties: [value({
383
+ collection,
384
+ field
385
+ }, deleted)],
386
+ writes: [],
387
+ filterReads: [key(collection, field)]
388
+ };
389
+ }
390
+ const selecting = (role, collection, empties, writes) => ({
391
+ role,
392
+ empties,
393
+ writes,
394
+ filterReads: filterReadsOf(collection, rewrite.discriminatorFilter)
395
+ });
396
+ switch (rewrite.kind) {
397
+ case "renameField": {
398
+ const { collection, from, to } = rewrite.call;
399
+ return selecting("moves", collection, [key(collection, from)], [key(collection, to)]);
400
+ }
401
+ case "unsetField": {
402
+ const { collection, field } = rewrite.call;
403
+ return selecting("empties", collection, [key(collection, field)], []);
404
+ }
405
+ case "fillField":
406
+ case "castField": {
407
+ const { collection, field } = rewrite.call;
408
+ return selecting("writes", collection, [], [key(collection, field)]);
409
+ }
410
+ case "dataTransform": {
411
+ const { collection } = rewrite.call;
412
+ return selecting("writes", collection, [], [key(collection, rewrite.transformedField)]);
413
+ }
414
+ case "renameStoredKey": {
415
+ const { collection, field, path, to } = rewrite.call.options;
416
+ return selecting("moves", collection, [key(collection, field, path)], [key(collection, field, renamedPath(field, path, to))]);
417
+ }
418
+ case "removeStoredKey": {
419
+ const { collection, field, path } = rewrite.call.options;
420
+ return selecting("empties", collection, [key(collection, field, path)], []);
421
+ }
422
+ case "fillStoredKey": {
423
+ const { collection, field, path } = rewrite.call.options;
424
+ return selecting("writes", collection, [], [key(collection, field, path)]);
425
+ }
426
+ case "replaceStoredValue": {
427
+ const { options } = rewrite.call;
428
+ return selecting("moves", options.collection, [value(options, options.from)], [value(options, rewrite.writtenValue)]);
429
+ }
430
+ case "removeStoredValue": {
431
+ const { options } = rewrite.call;
432
+ return selecting("empties", options.collection, [value(options, options.value)], []);
433
+ }
434
+ default: throw new InternalError(`No reads and writes are known for ${String(rewrite)}`);
435
+ }
436
+ }
437
+ function samePlace(write, read) {
438
+ if (write.kind !== read.kind || write.collection !== read.collection || write.field !== read.field || write.path !== read.path) return false;
439
+ return write.kind === "key" || read.kind === "value" && write.value === read.value;
440
+ }
441
+ function placeNoun(place) {
442
+ if (place.kind === "value") return "value";
443
+ return place.path === void 0 ? "field" : "key";
444
+ }
445
+ function touchedFieldsOf(accessed) {
446
+ return new Set(accessed.flatMap(({ access }) => [...access.empties, ...access.writes].flatMap((place) => place.path === void 0 ? [`${place.collection}\0${place.field}`] : [])));
447
+ }
448
+ function discriminatorFilterOf(rewrite) {
449
+ return rewrite.kind === "deleteDocuments" ? {
450
+ kind: "variant",
451
+ ...rewrite.deletedVariant
452
+ } : rewrite.discriminatorFilter;
453
+ }
454
+ /**
455
+ * Whether two operations select documents of different variants by the same discriminator field,
456
+ * in a collection where no operation of the plan changes that field or its values, so that they
457
+ * never act on the same documents.
458
+ */
459
+ function selectDifferentVariants(a, b, collection, touched) {
460
+ const first = discriminatorFilterOf(a);
461
+ const second = discriminatorFilterOf(b);
462
+ return first?.kind === "variant" && second?.kind === "variant" && first.field === second.field && first.value !== second.value && !touched.has(`${collection}\0${first.field}`);
463
+ }
464
+ function overlapsOf(accessed) {
465
+ const touched = touchedFieldsOf(accessed);
466
+ return accessed.flatMap((writer) => accessed.flatMap((reader) => {
467
+ if (writer.rewrite.call === reader.rewrite.call) return [];
468
+ const place = writer.access.writes.find((write) => reader.access.empties.some((emptied) => samePlace(write, emptied)) && !selectDifferentVariants(writer.rewrite, reader.rewrite, write.collection, touched));
469
+ return place === void 0 ? [] : [{
470
+ writer,
471
+ reader,
472
+ place
473
+ }];
474
+ }));
475
+ }
476
+ function adviceOf(overlap) {
477
+ const noun = placeNoun(overlap.place);
478
+ if (overlap.place.kind === "value") return overlap.reader.rewrite.twoMigrationsAdvice ?? overlap.writer.rewrite.twoMigrationsAdvice ?? RENAME_ADVICE;
479
+ const writer = overlap.writer.access.role;
480
+ const reader = overlap.reader.access.role;
481
+ if (writer === "writes") return reader === "empties" ? fillAfterRemovalAdvice(noun) : FILL_ADVICE;
482
+ return reader === "empties" ? removeBeforeRenameAdvice(noun) : RENAME_ADVICE;
483
+ }
484
+ /** Whether `before` must run before `after`, because `before` writes what `after`'s filter reads, or `after` empties what `before`'s filter reads. */
485
+ function filterNeedsFirst(before, after) {
486
+ return before !== after && (before.access.writes.some((write) => after.access.filterReads.some((read) => samePlace(write, read))) || after.access.empties.some((emptied) => before.access.filterReads.some((read) => samePlace(emptied, read))));
487
+ }
488
+ /** The first pair in which an operation runs before another that must run first, because of what their filters read. */
489
+ function filterConflictOf(accessed) {
490
+ for (const [index, later] of accessed.entries()) {
491
+ const earlier = accessed.slice(0, index).find((each) => filterNeedsFirst(later, each));
492
+ if (earlier !== void 0) return {
493
+ earlier,
494
+ later
495
+ };
496
+ }
497
+ }
498
+ function refusal(involved, reason, why, contracts) {
499
+ const [refused] = involved.flatMap((rewrite) => rewrite.statements);
500
+ if (refused === void 0) return {
501
+ kind: "unsupportedOperation",
502
+ summary: `Cannot plan the migration: ${reason}`,
503
+ why
504
+ };
505
+ return {
506
+ kind: "statementRefused",
507
+ summary: `Cannot ${describeMigrationStatement(refused, contracts.fromContract, contracts.contract)}: ${reason}`,
508
+ why,
509
+ refusedStatement: refused
510
+ };
511
+ }
512
+ /**
513
+ * The refusal of a plan in which one operation writes a field, key or value that another operation
514
+ * of the same plan empties: a chain or a swap of renames or replacements, a fill of a key a rename
515
+ * moves away, a rename onto a name whose field the plan removes, or a delete of documents whose
516
+ * discriminator value a replacement writes. MongoDB applies a migration's operations one at a
517
+ * time, without a transaction, and a re-run after a later operation stops runs again every
518
+ * operation whose postcheck does not hold, so such an operation would act on what the other wrote.
519
+ * Two operations that select different variants by a discriminator field the plan does not change
520
+ * never meet. Also refused: an operation whose filter reads what an earlier operation empties or a
521
+ * later one writes, which the planner's order of the rewrites does not avoid. The refusal names the first statement
522
+ * involved and lists the operations. It does not offer to leave the statements out and write the
523
+ * operations by hand, since one hand-written migration would meet the same re-run. When an operation
524
+ * involved makes a change no statement answers, and a statement could keep the data of a removal,
525
+ * the refusal leads with the plan's unanswered changes and the statements that keep their data, and
526
+ * ends with the route that works when every answer is `--delete`, which the planner cannot see.
527
+ */
528
+ function overlapRefusal(rewrites, contracts) {
529
+ const accessed = rewrites.map((rewrite) => ({
530
+ rewrite,
531
+ access: accessOf(rewrite)
532
+ }));
533
+ const why = (involved, reason, advice) => {
534
+ const unanswered = involved.some(({ unanswered }) => unanswered !== void 0) ? unansweredChangesAdvice(rewrites.flatMap(({ unanswered }) => unanswered === void 0 ? [] : [unanswered]), contracts) : void 0;
535
+ return unanswered === void 0 ? `${reason} ${advice}` : `${unanswered} ${reason} If every answer is --delete, the plan stays as it is: ${advice.charAt(0).toLowerCase()}${advice.slice(1)}`;
536
+ };
537
+ const overlaps = overlapsOf(accessed);
538
+ const [first] = overlaps;
539
+ if (first !== void 0) {
540
+ const involved = rewrites.filter((rewrite) => overlaps.some(({ writer, reader }) => writer.rewrite === rewrite || reader.rewrite === rewrite));
541
+ const noun = placeNoun(first.place);
542
+ return refusal(involved, `the migration writes a ${noun} that another of its operations reads (${involved.map(({ call }) => call.label).join("; ")})`, why(involved, `${ONE_AT_A_TIME} An operation that reads a ${noun} another operation of the same migration writes acts on what that operation wrote: at once when it runs later, and when the migration runs again after a later operation stops.`, adviceOf(first)), contracts);
543
+ }
544
+ const conflict = filterConflictOf(accessed);
545
+ if (conflict === void 0) return void 0;
546
+ const involved = [conflict.earlier.rewrite, conflict.later.rewrite];
547
+ return refusal(involved, `the filter of one of its operations reads what another changes (${involved.map(({ call }) => call.label).join("; ")})`, why(involved, "One of these operations finds its documents by what another changes, and the order the plan needs runs it on the wrong side of that change.", inTwoSteps(`make only the part of the change that "${conflict.later.rewrite.call.label}" carries out`, "the rest")), contracts);
548
+ }
549
+ const SEPARATE_THE_MODELS = "move the models to separate collections, or carry out the change yourself, with a filter that selects only their documents, in a dataTransform operation of the migration.ts that migration plan writes or in mongosh";
157
550
  function sharedCollectionWhy(collection, others, change) {
158
551
  const names = others.map((name) => JSON.stringify(name)).join(", ");
159
552
  return `Collection ${JSON.stringify(collection)} stores several models and no discriminator tells their documents apart, so ${change} of ${names} too.`;
@@ -161,6 +554,7 @@ function sharedCollectionWhy(collection, others, change) {
161
554
  const TIME_SERIES_UPDATES = "MongoDB updates the documents of a time-series collection only through its metaField";
162
555
  const WORDS = {
163
556
  removal: {
557
+ keyVerb: void 0,
164
558
  conflictKind: "fieldRemovalRefused",
165
559
  verb: "remove",
166
560
  preposition: "from",
@@ -170,6 +564,7 @@ const WORDS = {
170
564
  sharedGoal: (_model, others) => `To keep it in ${others} documents only`
171
565
  },
172
566
  cast: {
567
+ keyVerb: void 0,
173
568
  conflictKind: "typeChangeRefused",
174
569
  verb: "cast",
175
570
  preposition: "in",
@@ -179,6 +574,7 @@ const WORDS = {
179
574
  sharedGoal: (model) => `To change it in ${model} documents only`
180
575
  },
181
576
  conversion: {
577
+ keyVerb: void 0,
182
578
  conflictKind: "typeChangeRefused",
183
579
  verb: "convert",
184
580
  preposition: "in",
@@ -188,6 +584,7 @@ const WORDS = {
188
584
  sharedGoal: (model) => `To change it in ${model} documents only`
189
585
  },
190
586
  fill: {
587
+ keyVerb: void 0,
191
588
  conflictKind: "fieldFillRefused",
192
589
  verb: "fill",
193
590
  preposition: "in",
@@ -197,6 +594,7 @@ const WORDS = {
197
594
  sharedGoal: (model) => `To fill it in ${model} documents only`
198
595
  },
199
596
  backfill: {
597
+ keyVerb: void 0,
200
598
  conflictKind: "fieldFillRefused",
201
599
  verb: "backfill",
202
600
  preposition: "in",
@@ -204,16 +602,71 @@ const WORDS = {
204
602
  timeSeries: "so a data transform cannot fill the field. Make the field optional in the contract, then run this command again.",
205
603
  sharedChange: "the data transform would fill it in the documents",
206
604
  sharedGoal: (model) => `To fill it in ${model} documents only`
605
+ },
606
+ renameStoredKey: {
607
+ keyVerb: "rename",
608
+ conflictKind: "statementRefused",
609
+ verb: "rename a key inside",
610
+ preposition: "in",
611
+ reaches: "holding",
612
+ timeSeries: "so the key cannot be renamed in the documents. Keep the key's name in the contract, then run this command again.",
613
+ sharedChange: "renaming the key would rename it in the documents",
614
+ sharedGoal: (model) => `To rename it in ${model} documents only`
615
+ },
616
+ removeStoredKey: {
617
+ keyVerb: "remove",
618
+ conflictKind: "fieldRemovalRefused",
619
+ verb: "remove a key inside",
620
+ preposition: "in",
621
+ reaches: "holding",
622
+ timeSeries: "so the key cannot be removed from the documents that hold it. Keep the key in the contract, as an optional field if new documents leave it out, then run this command again.",
623
+ sharedChange: "removing the key would remove it from the documents",
624
+ sharedGoal: (_model, others) => `To keep it in ${others} documents only`
625
+ },
626
+ replaceStoredValue: {
627
+ keyVerb: "replace",
628
+ conflictKind: "statementRefused",
629
+ verb: "replace a value inside",
630
+ preposition: "in",
631
+ reaches: "holding",
632
+ timeSeries: "so the stored value cannot be replaced in the documents. Keep the enum value's stored value in the contract, then run this command again.",
633
+ sharedChange: "replacing the value would replace it in the documents",
634
+ sharedGoal: (model) => `To replace it in ${model} documents only`
635
+ },
636
+ removeStoredValue: {
637
+ keyVerb: "remove",
638
+ conflictKind: "fieldRemovalRefused",
639
+ verb: "remove a value inside",
640
+ preposition: "from",
641
+ reaches: "holding",
642
+ timeSeries: "so the stored value cannot be removed from the documents that hold it. Keep the enum value in the contract, then run this command again.",
643
+ sharedChange: "removing the value would remove it from the documents",
644
+ sharedGoal: (_model, others) => `To keep it in ${others} documents only`
645
+ },
646
+ fillStoredKey: {
647
+ keyVerb: "fill",
648
+ conflictKind: "fieldFillRefused",
649
+ verb: "fill a key inside",
650
+ preposition: "in",
651
+ reaches: "holding",
652
+ timeSeries: "so the key cannot be filled in the documents. Make the key optional in the contract, then run this command again.",
653
+ sharedChange: "filling the key would fill it in the documents",
654
+ sharedGoal: (model) => `To fill it in ${model} documents only`
207
655
  }
208
656
  };
209
- function quoted(names) {
657
+ function quoted$1(names) {
210
658
  return names.map((name) => JSON.stringify(name)).join(", ");
211
659
  }
660
+ /** What a rewrite does, as a refusal names it: the key it rewrites inside the field, when it names one. */
661
+ function action(rewrite) {
662
+ const words = WORDS[rewrite.kind];
663
+ return rewrite.storedKey !== void 0 && words.keyVerb !== void 0 ? `${words.keyVerb} ${rewrite.storedKey.key} inside` : words.verb;
664
+ }
212
665
  function timeSeriesRefusal(rewrite) {
213
666
  const words = WORDS[rewrite.kind];
214
667
  return {
215
668
  kind: words.conflictKind,
216
- summary: `Cannot ${words.verb} field ${JSON.stringify(rewrite.field)} ${words.preposition} collection ${JSON.stringify(rewrite.collection)}: it is a time-series collection`,
669
+ summary: `Cannot ${action(rewrite)} field ${JSON.stringify(rewrite.field)} ${words.preposition} collection ${JSON.stringify(rewrite.collection)}: it is a time-series collection`,
217
670
  why: `${TIME_SERIES_UPDATES}, ${words.timeSeries}`
218
671
  };
219
672
  }
@@ -225,24 +678,24 @@ function timeSeriesRefusal(rewrite) {
225
678
  * an origin contract, a cast in a collection whose validator describes several root models is
226
679
  * refused, since nothing tells their documents apart.
227
680
  */
228
- function sharedCollectionRefusal(rewrite, contract, validator) {
681
+ function sharedCollectionRefusal$1(rewrite, contract, validator) {
229
682
  const words = WORDS[rewrite.kind];
230
683
  const { collection, field, model } = rewrite;
231
684
  if (model === void 0) {
232
- if (rewrite.kind === "removal" || validatorRootShapesOf(validator).length <= 1) return;
685
+ if (rewrite.kind === "removal" || rewrite.kind === "removeStoredKey" || validatorRootShapesOf(validator).length <= 1) return;
233
686
  return {
234
687
  kind: words.conflictKind,
235
- summary: `Cannot ${words.verb} field ${JSON.stringify(field)} ${words.preposition} collection ${JSON.stringify(collection)}: the collection stores several root models`,
688
+ summary: `Cannot ${action(rewrite)} field ${JSON.stringify(field)} ${words.preposition} collection ${JSON.stringify(collection)}: the collection stores several root models`,
236
689
  why: "No discriminator tells their documents apart, and without the origin contract the plan cannot tell which documents belong to the model that changed. Plan the change with migration plan --from <hash of the migration the database is at>."
237
690
  };
238
691
  }
239
- const others = otherModelsReached(contract.domain, model, collection, rewrite.filter, words.reaches === "holding" ? field : void 0);
692
+ const others = otherModelsReached(contract.domain, model, collection, rewrite.variant, words.reaches === "holding" ? field : void 0).filter((name) => !(rewrite.storedKey?.together.includes(name) ?? false));
240
693
  if (others.length === 0) return void 0;
241
- const named = quoted(others);
694
+ const named = quoted$1(others);
242
695
  const modelName = JSON.stringify(model.model);
243
696
  return {
244
697
  kind: words.conflictKind,
245
- summary: `Cannot ${words.verb} field ${JSON.stringify(field)} of model ${modelName} ${words.preposition} collection ${JSON.stringify(collection)}: model ${named} ${words.reaches === "holding" ? "stores it" : "is stored"} in the same collection`,
698
+ summary: `Cannot ${action(rewrite)} field ${JSON.stringify(field)} of model ${modelName} ${words.preposition} collection ${JSON.stringify(collection)}: model ${named} ${words.reaches === "holding" ? "stores it" : "is stored"} in the same collection`,
246
699
  why: `${sharedCollectionWhy(collection, others, words.sharedChange)} ${words.sharedGoal(modelName, named)}, ${SEPARATE_THE_MODELS}.`
247
700
  };
248
701
  }
@@ -253,27 +706,17 @@ function sharedCollectionRefusal(rewrite, contract, validator) {
253
706
  * plans with these; the runner asks them before it names a statement that would plan one.
254
707
  */
255
708
  function rewriteRefusals(rewrite, contract, collection) {
256
- const shared = sharedCollectionRefusal(rewrite, contract, collection.validator);
709
+ const shared = sharedCollectionRefusal$1(rewrite, contract, collection.validator);
257
710
  return [...collection.timeSeries ? [timeSeriesRefusal(rewrite)] : [], ...shared === void 0 ? [] : [shared]];
258
711
  }
259
- /**
260
- * Mongo migration IR: one concrete `*Call` class per pure factory under
261
- * `migration-factories.ts`, plus a shared `OpFactoryCallNode` abstract
262
- * base. Every call class carries the literal arguments its backing
263
- * factory would receive, computes a human-readable `label` in its
264
- * constructor, and implements two polymorphic hooks:
265
- *
266
- * - `toOp()` — converts the IR node to a runtime
267
- * `MongoMigrationPlanOperation` by delegating to the matching pure
268
- * factory in `migration-factories.ts`.
269
- * - `renderTypeScript()` / `importRequirements()` — inherited from
270
- * `TsExpression`. Used by `renderCallsToTypeScript` to emit the call
271
- * as a TypeScript expression inside the scaffolded `migration.ts`.
272
- *
273
- * The abstract base and all concrete classes are package-private.
274
- * External consumers see only the framework-level `OpFactoryCall`
275
- * interface and the `OpFactoryCall` union.
276
- */
712
+ /** A value a planner-written operation takes from the user: its `migration.ts` writes `placeholder(slot)` where the value goes. */
713
+ var UnfilledValue = class {
714
+ slot;
715
+ constructor(slot) {
716
+ this.slot = slot;
717
+ Object.freeze(this);
718
+ }
719
+ };
277
720
  /**
278
721
  * The single module a scaffolded Mongo `migration.ts` imports from: the
279
722
  * operation factories below, plus the `Migration` base and `MigrationCLI`
@@ -472,6 +915,28 @@ var CastFieldCall = class extends OpFactoryCallNode {
472
915
  }
473
916
  };
474
917
  /**
918
+ * The operation of a call whose value is an unfilled placeholder: a promise that rejects with
919
+ * `MIGRATION.UNFILLED_PLACEHOLDER`, whose meta names the operation it stands for, so that a plan can
920
+ * list it before the user fills the value.
921
+ */
922
+ function unfilledOperation(slot, standsFor) {
923
+ const unfilled = errorUnfilledPlaceholder(slot);
924
+ const operation = Promise.reject(new CliStructuredError(unfilled.code, unfilled.message, {
925
+ ...ifDefined("why", unfilled.why),
926
+ ...ifDefined("fix", unfilled.fix),
927
+ meta: {
928
+ slot,
929
+ operation: {
930
+ id: standsFor.id,
931
+ label: standsFor.label,
932
+ operationClass: standsFor.operationClass
933
+ }
934
+ }
935
+ }));
936
+ operation.catch(() => void 0);
937
+ return operation;
938
+ }
939
+ /**
475
940
  * A data transform the planner scaffolds for a field: its check and its run's filter select the
476
941
  * documents still to change, and its update is a placeholder the user fills in `migration.ts`.
477
942
  * Until then `toOp()` rejects with `MIGRATION.UNFILLED_PLACEHOLDER`, naming the operation it stands
@@ -496,21 +961,11 @@ var DataTransformCall = class extends OpFactoryCallNode {
496
961
  return `${this.name}:update`;
497
962
  }
498
963
  toOp() {
499
- const unfilled = errorUnfilledPlaceholder(this.slot);
500
- const operation = Promise.reject(new CliStructuredError(unfilled.code, unfilled.message, {
501
- ...ifDefined("why", unfilled.why),
502
- ...ifDefined("fix", unfilled.fix),
503
- meta: {
504
- slot: this.slot,
505
- operation: {
506
- id: `data_transform.${this.name}`,
507
- label: this.label,
508
- operationClass: this.operationClass
509
- }
510
- }
511
- }));
512
- operation.catch(() => void 0);
513
- return operation;
964
+ return unfilledOperation(this.slot, {
965
+ id: `data_transform.${this.name}`,
966
+ label: this.label,
967
+ operationClass: this.operationClass
968
+ });
514
969
  }
515
970
  renderTypeScript() {
516
971
  const collection = jsonToTsSource(this.collection);
@@ -556,6 +1011,159 @@ var FillFieldCall = class extends OpFactoryCallNode {
556
1011
  return `fillField(${jsonToTsSource(this.options)})`;
557
1012
  }
558
1013
  };
1014
+ var RenameStoredKeyCall = class extends OpFactoryCallNode {
1015
+ factoryName = "renameStoredKey";
1016
+ operationClass = "widening";
1017
+ options;
1018
+ label;
1019
+ constructor(options) {
1020
+ super();
1021
+ this.options = options;
1022
+ this.label = this.toOp().label;
1023
+ this.freeze();
1024
+ }
1025
+ toOp() {
1026
+ return renameStoredKey(this.options);
1027
+ }
1028
+ renderTypeScript() {
1029
+ return `renameStoredKey(${jsonToTsSource(this.options)})`;
1030
+ }
1031
+ };
1032
+ var RemoveStoredKeyCall = class extends OpFactoryCallNode {
1033
+ factoryName = "removeStoredKey";
1034
+ operationClass = "destructive";
1035
+ options;
1036
+ label;
1037
+ constructor(options) {
1038
+ super();
1039
+ this.options = options;
1040
+ this.label = this.toOp().label;
1041
+ this.freeze();
1042
+ }
1043
+ toOp() {
1044
+ return removeStoredKey(this.options);
1045
+ }
1046
+ renderTypeScript() {
1047
+ return `removeStoredKey(${jsonToTsSource(this.options)})`;
1048
+ }
1049
+ };
1050
+ /**
1051
+ * Sets a key inside a field's value where it is missing or `null`. An unfilled value is a placeholder
1052
+ * the user fills in `migration.ts`; until then `toOp()` rejects with `MIGRATION.UNFILLED_PLACEHOLDER`.
1053
+ */
1054
+ var FillStoredKeyCall = class extends OpFactoryCallNode {
1055
+ factoryName = "fillStoredKey";
1056
+ operationClass = "widening";
1057
+ options;
1058
+ label;
1059
+ constructor(options) {
1060
+ super();
1061
+ this.options = options;
1062
+ this.label = fillStoredKey({
1063
+ ...options,
1064
+ value: null
1065
+ }).label;
1066
+ this.freeze();
1067
+ }
1068
+ toOp() {
1069
+ const { value, ...at } = this.options;
1070
+ if (value instanceof UnfilledValue) return unfilledOperation(value.slot, fillStoredKey({
1071
+ ...at,
1072
+ value: null
1073
+ }));
1074
+ return fillStoredKey({
1075
+ ...at,
1076
+ value
1077
+ });
1078
+ }
1079
+ renderTypeScript() {
1080
+ return `fillStoredKey(${tsObjectSource(Object.entries(this.options).map(([key, value]) => [key, value instanceof UnfilledValue ? `placeholder(${jsonToTsSource(value.slot)})` : jsonToTsSource(value)]))})`;
1081
+ }
1082
+ importRequirements() {
1083
+ return [...super.importRequirements(), ...this.options.value instanceof UnfilledValue ? [{
1084
+ moduleSpecifier: TARGET_MIGRATION_MODULE,
1085
+ symbol: "placeholder"
1086
+ }] : []];
1087
+ }
1088
+ };
1089
+ var ReplaceStoredValueCall = class extends OpFactoryCallNode {
1090
+ factoryName = "replaceStoredValue";
1091
+ operationClass = "widening";
1092
+ options;
1093
+ label;
1094
+ constructor(options) {
1095
+ super();
1096
+ this.options = options;
1097
+ this.label = this.toOp().label;
1098
+ this.freeze();
1099
+ }
1100
+ toOp() {
1101
+ return replaceStoredValue(this.options);
1102
+ }
1103
+ renderTypeScript() {
1104
+ return `replaceStoredValue(${jsonToTsSource(this.options)})`;
1105
+ }
1106
+ };
1107
+ /** A call as an edit names it inside a sentence: on one line, without trailing commas. */
1108
+ function callInSentence(call) {
1109
+ return `${call.factoryName}(${sourceOnOneLine(call.options)})`;
1110
+ }
1111
+ function sourceOnOneLine(value) {
1112
+ if (Array.isArray(value)) return `[${value.map(sourceOnOneLine).join(", ")}]`;
1113
+ if (typeof value === "object" && value !== null) {
1114
+ const entries = Object.entries(value).filter(([, entry]) => entry !== void 0);
1115
+ if (entries.length === 0) return "{}";
1116
+ return `{ ${entries.map(([key, entry]) => `${propertyName(key)}: ${sourceOnOneLine(entry)}`).join(", ")} }`;
1117
+ }
1118
+ return jsonToTsSource(value);
1119
+ }
1120
+ function propertyName(key) {
1121
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) && key !== "__proto__" ? key : tsStringLiteral(key);
1122
+ }
1123
+ var RemoveStoredValueCall = class extends OpFactoryCallNode {
1124
+ factoryName = "removeStoredValue";
1125
+ operationClass = "destructive";
1126
+ options;
1127
+ imports;
1128
+ label;
1129
+ constructor(options, imports = { importsReplacement: false }) {
1130
+ super();
1131
+ this.options = options;
1132
+ this.imports = imports;
1133
+ this.label = this.toOp().label;
1134
+ this.freeze();
1135
+ }
1136
+ toOp() {
1137
+ return removeStoredValue(this.options);
1138
+ }
1139
+ renderTypeScript() {
1140
+ return `removeStoredValue(${jsonToTsSource(this.options)})`;
1141
+ }
1142
+ importRequirements() {
1143
+ return [...super.importRequirements(), ...this.imports.importsReplacement ? [{
1144
+ moduleSpecifier: TARGET_MIGRATION_MODULE,
1145
+ symbol: "replaceStoredValue"
1146
+ }] : []];
1147
+ }
1148
+ };
1149
+ var DeleteDocumentsCall = class extends OpFactoryCallNode {
1150
+ factoryName = "deleteDocuments";
1151
+ operationClass = "destructive";
1152
+ options;
1153
+ label;
1154
+ constructor(options) {
1155
+ super();
1156
+ this.options = options;
1157
+ this.label = this.toOp().label;
1158
+ this.freeze();
1159
+ }
1160
+ toOp() {
1161
+ return deleteDocuments(this.options);
1162
+ }
1163
+ renderTypeScript() {
1164
+ return `deleteDocuments(${jsonToTsSource(this.options)})`;
1165
+ }
1166
+ };
559
1167
  var CollModCall = class extends OpFactoryCallNode {
560
1168
  factoryName = "collMod";
561
1169
  collection;
@@ -619,7 +1227,6 @@ function embeddedModelReason(contract, coordinate) {
619
1227
  const owner = embeddingModelOf(contract.domain, coordinate);
620
1228
  return owner === void 0 ? void 0 : `model "${modelDisplayName(coordinate)}" is embedded in the documents of model "${owner}"`;
621
1229
  }
622
- const EMBEDDED_MODEL_WHY = "Fields of an embedded model are not rewritten in this release. Leave out this statement.";
623
1230
  /** Why a statement on `model` cannot be planned when no collection holds its documents. */
624
1231
  function noCollectionReason(model) {
625
1232
  return `model "${modelDisplayName(model)}" names no collection, and neither does a base or an owner`;
@@ -669,7 +1276,7 @@ function invalidCollectionNameReason(name) {
669
1276
  if (name.includes("\0")) return "it contains a null character";
670
1277
  if (name.startsWith("system.")) return "it starts with \"system.\", which MongoDB reserves";
671
1278
  }
672
- function isEmbedRelation(relation) {
1279
+ function isEmbedRelation$1(relation) {
673
1280
  return !("on" in relation) && !("through" in relation);
674
1281
  }
675
1282
  function storedField(model, field) {
@@ -677,9 +1284,9 @@ function storedField(model, field) {
677
1284
  if (Object.hasOwn(model.fields, field)) return { kind: "field" };
678
1285
  const relation = Object.hasOwn(model.relations, field) ? model.relations[field] : void 0;
679
1286
  if (relation === void 0) return { kind: "absent" };
680
- return isEmbedRelation(relation) ? { kind: "embed" } : { kind: "reference" };
1287
+ return isEmbedRelation$1(relation) ? { kind: "embed" } : { kind: "reference" };
681
1288
  }
682
- function refused(statement, label, reason, why) {
1289
+ function refused$3(statement, label, reason, why) {
683
1290
  return {
684
1291
  kind: "statementRefused",
685
1292
  summary: `${label}: ${reason}`,
@@ -704,14 +1311,8 @@ function planStatements(input) {
704
1311
  const label = `Cannot ${describeMigrationStatement(statement, input.fromContract, input.contract)}`;
705
1312
  const outcome = statement.entity === "namespace" ? {
706
1313
  kind: "refused",
707
- conflict: refused(statement, label, "MongoDB does not rename namespaces in this release", LEAVE_OUT)
708
- } : statement.entity === "enumValue" ? {
709
- kind: "refused",
710
- conflict: refused(statement, label, "MongoDB does not rename enum values in this release", LEAVE_OUT)
711
- } : statement.entity === "valueObjectField" ? {
712
- kind: "refused",
713
- conflict: refused(statement, label, "MongoDB does not rename value object fields in this release", LEAVE_OUT)
714
- } : statement.entity === "model" ? planModelStatement(statement, label, schema, input) : planFieldStatement(statement, label, renamedCollections, schema, input);
1314
+ conflict: refused$3(statement, label, "MongoDB does not rename namespaces in this release", LEAVE_OUT)
1315
+ } : statement.entity === "enumValue" || statement.entity === "valueObjectField" ? { kind: "unchanged" } : statement.entity === "model" ? planModelStatement(statement, label, schema, input) : planFieldStatement(statement, label, renamedCollections, schema, input);
715
1316
  if (outcome.kind === "refused") {
716
1317
  conflicts.push(outcome.conflict);
717
1318
  callOfStatement.push(void 0);
@@ -723,14 +1324,14 @@ function planStatements(input) {
723
1324
  collectionRenames.push(outcome.call);
724
1325
  }
725
1326
  if (outcome.kind === "renameField") {
726
- const same = fieldRenames.find((call) => sameFieldRename(call, outcome.call));
1327
+ const same = fieldRenames.find(({ call }) => sameFieldRename(call, outcome.rename.call));
727
1328
  if (same !== void 0) {
728
- callOfStatement.push(same);
1329
+ callOfStatement.push(same.call);
729
1330
  continue;
730
1331
  }
731
- fieldRenames.push(outcome.call);
1332
+ fieldRenames.push(outcome.rename);
732
1333
  }
733
- callOfStatement.push(outcome.kind === "unchanged" ? void 0 : outcome.call);
1334
+ callOfStatement.push(outcome.kind === "unchanged" ? void 0 : outcome.kind === "renameField" ? outcome.rename.call : outcome.call);
734
1335
  }
735
1336
  return conflicts.length > 0 ? notOk(conflicts) : ok({
736
1337
  schema,
@@ -756,48 +1357,27 @@ function controlPolicyRefusal(statement, label, contract, collection) {
756
1357
  if (policy === "managed") return void 0;
757
1358
  return {
758
1359
  kind: "refused",
759
- conflict: refused(statement, label, `collection "${collection}" has control policy "${policy}"`, `Statements rename only collections, and fields of collections, whose control policy is "managed". Make the change in the database yourself and leave out this statement.`)
1360
+ conflict: refused$3(statement, label, `collection "${collection}" has control policy "${policy}"`, `Statements rename only collections, and fields of collections, whose control policy is "managed". Make the change in the database yourself and leave out this statement.`)
760
1361
  };
761
1362
  }
762
- /**
763
- * The reason and advice when a statement renames a variant whose discriminator value changes: its
764
- * documents keep the old value, which the new validator does not accept.
765
- */
766
- function discriminatorValueChange(statement, input) {
767
- const before = variantFilterOf(input.fromContract.domain, statement.from);
768
- const after = variantFilterOf(input.contract.domain, statement.to);
769
- const base = modelAt(input.contract, statement.to)?.base;
770
- if (before === void 0 || after === void 0 || base === void 0) return void 0;
771
- const [field, oldValue] = Object.entries(before)[0] ?? [];
772
- const newValue = Object.values(after)[0];
773
- if (field === void 0 || oldValue === newValue) return void 0;
774
- const fromName = modelDisplayName(statement.from);
775
- const toName = modelDisplayName(statement.to);
776
- return [`its discriminator value changes from ${JSON.stringify(oldValue)} to ${JSON.stringify(newValue)}`, `The documents of "${fromName}" hold ${field} ${JSON.stringify(oldValue)}, and this release does not rewrite a discriminator value, so they would no longer match "${toName}" and would fail validation. Keep the value: declare "${toName}" with @@base(${base.model}, ${JSON.stringify(oldValue)}). A later release changes a stored value with --convert.`];
777
- }
778
1363
  function planModelStatement(statement, label, schema, input) {
779
1364
  const embedded = embeddedModelReason(input.fromContract, statement.from) ?? embeddedModelReason(input.contract, statement.to);
780
1365
  if (embedded !== void 0) return {
781
1366
  kind: "refused",
782
- conflict: refused(statement, label, embedded, `Embedded models are not renamed in this release. ${LEAVE_OUT}`)
783
- };
784
- const discriminatorChange = discriminatorValueChange(statement, input);
785
- if (discriminatorChange !== void 0) return {
786
- kind: "refused",
787
- conflict: refused(statement, label, ...discriminatorChange)
1367
+ conflict: refused$3(statement, label, embedded, `Embedded models are not renamed in this release. ${LEAVE_OUT}`)
788
1368
  };
789
1369
  const from = collectionOfModel(input.fromContract.domain, statement.from);
790
1370
  const to = collectionOfModel(input.contract.domain, statement.to);
791
1371
  if (from === void 0 || to === void 0) return {
792
1372
  kind: "refused",
793
- conflict: refused(statement, label, noCollectionReason(from === void 0 ? statement.from : statement.to), `A statement renames the documents of a model in its collection. Give the model a collection, then run this command again, or ${LEAVE_OUT.toLowerCase()}`)
1373
+ conflict: refused$3(statement, label, noCollectionReason(from === void 0 ? statement.from : statement.to), `A statement renames the documents of a model in its collection. Give the model a collection, then run this command again, or ${LEAVE_OUT.toLowerCase()}`)
794
1374
  };
795
1375
  if (from === to) return { kind: "unchanged" };
796
1376
  const policyRefusal = controlPolicyRefusal(statement, label, input.contract, to);
797
1377
  if (policyRefusal !== void 0) return policyRefusal;
798
1378
  const refuse = (reason, why) => ({
799
1379
  kind: "refused",
800
- conflict: refused(statement, label, reason, why)
1380
+ conflict: refused$3(statement, label, reason, why)
801
1381
  });
802
1382
  const source = schema.collection(from);
803
1383
  if (source === void 0) return refuse(`the schema being planned from has no collection "${from}"`, `The database has no collection "${from}", or "${from}" is a view, which MongoDB cannot rename. Inspect it with prisma db schema, or ${LEAVE_OUT.toLowerCase()}`);
@@ -813,10 +1393,13 @@ function planModelStatement(statement, label, schema, input) {
813
1393
  function planFieldStatement(statement, label, renamedCollections, schema, input) {
814
1394
  const refuse = (reason, why) => ({
815
1395
  kind: "refused",
816
- conflict: refused(statement, label, reason, why)
1396
+ conflict: refused$3(statement, label, reason, why)
817
1397
  });
818
- const embedded = embeddedModelReason(input.fromContract, statement.from) ?? embeddedModelReason(input.contract, statement.to);
819
- if (embedded !== void 0) return refuse(embedded, EMBEDDED_MODEL_WHY);
1398
+ const embeddedFrom = embeddedModelReason(input.fromContract, statement.from);
1399
+ const embeddedTo = embeddedModelReason(input.contract, statement.to);
1400
+ if (embeddedFrom !== void 0 && embeddedTo !== void 0) return { kind: "unchanged" };
1401
+ const embedded = embeddedFrom ?? embeddedTo;
1402
+ if (embedded !== void 0) return refuse(embedded, `A statement renames a field of an embedded model only while the model stays embedded. ${LEAVE_OUT}`);
820
1403
  const fromField = storedField(modelAt(input.fromContract, statement.from), statement.from.field);
821
1404
  const toField = storedField(modelAt(input.contract, statement.to), statement.to.field);
822
1405
  if (fromField.kind === "embed" || toField.kind === "embed") return refuse("the field is an embedded relation", `Renaming an embedded relation is not supported in this release. ${LEAVE_OUT}`);
@@ -838,17 +1421,24 @@ function planFieldStatement(statement, label, renamedCollections, schema, input)
838
1421
  if (schema.collection(collection)?.options?.timeseries !== void 0) return refuse(`collection "${collection}" is a time-series collection`, `${TIME_SERIES_UPDATES}, so a statement cannot move the values of a field in them. Keep the stored name: store the field as ${JSON.stringify(fromStored)} (with @map(${JSON.stringify(fromStored)}) in PSL), then run this command again without this statement.`);
839
1422
  const policyRefusal = controlPolicyRefusal(statement, label, input.contract, collection);
840
1423
  if (policyRefusal !== void 0) return policyRefusal;
841
- const variant = variantFilterOf(input.fromContract.domain, statement.from);
1424
+ const variant = selectedVariantOf(input.fromContract.domain, statement.from);
842
1425
  const renamedTogether = new Set(input.statements.flatMap((other) => other.entity === "field" && other.from.namespaceId === statement.from.namespaceId && storedFieldNameAt(input.fromContract, other.from) === fromStored && storedFieldNameAt(input.contract, other.to) === toStored ? [other.from.model] : []));
843
1426
  const others = otherModelsReached(input.fromContract.domain, statement.from, originCollection, variant, fromStored).filter((name) => !renamedTogether.has(name));
844
1427
  if (others.length > 0) return refuse(`model ${others.map((name) => JSON.stringify(name)).join(", ")} stores field ${JSON.stringify(fromStored)} in the same collection, ${JSON.stringify(collection)}`, `${sharedCollectionWhy(collection, others, "renaming the field would rename it in the documents")} To change it, ${SEPARATE_THE_MODELS}.`);
1428
+ const selected = variant === void 0 ? rootDocumentsLosing(input.contract, statement.to, fromStored) : {
1429
+ filter: variantFilter(variant),
1430
+ discriminatorFilter: variantDiscriminatorFilter(variant)
1431
+ };
845
1432
  return {
846
1433
  kind: "renameField",
847
- call: new RenameFieldCall(collection, fromStored, toStored, variant ?? rootDocumentsLosing(input.contract, statement.to, fromStored)?.filter)
1434
+ rename: {
1435
+ call: new RenameFieldCall(collection, fromStored, toStored, selected?.filter),
1436
+ discriminatorFilter: selected?.discriminatorFilter
1437
+ }
848
1438
  };
849
1439
  }
850
1440
  function collectionChangeAdvice(statement, from, to) {
851
- if (!sameModel(statement.from, statement.to)) return `Add --rename ${modelDisplayName(statement.from)}:${modelDisplayName(statement.to)} before this statement, so the collection is renamed with its documents.`;
1441
+ if (!sameModel$1(statement.from, statement.to)) return `Add --rename ${modelDisplayName(statement.from)}:${modelDisplayName(statement.to)} before this statement, so the collection is renamed with its documents.`;
852
1442
  return `A statement cannot rename the collection of a model that keeps its name. Rename the collection in a step of its own first: change only @@map and emit the contract. ${renameCollectionByHandSteps(statement.from, from, to)} Then change the field and run this statement again.`;
853
1443
  }
854
1444
  function renameCollectionByHandSteps(model, from, to) {
@@ -867,46 +1457,59 @@ function modelCoordinates(contract) {
867
1457
  model
868
1458
  })));
869
1459
  }
870
- function sameModel(a, b) {
1460
+ function sameModel$1(a, b) {
871
1461
  return a.namespaceId === b.namespaceId && a.model === b.model;
872
1462
  }
873
1463
  /**
874
1464
  * The destination model an origin model became: the one a statement renamed it to, else the one of
875
- * the same name, else the only one stored the same way (same collection, same variant value).
1465
+ * the same name, else the only new model stored the same way (same collection, same variant value)
1466
+ * that no statement names and that no other origin model is stored like, so no two origin models
1467
+ * become one.
876
1468
  */
877
1469
  function destinationModelOf(origin, input) {
878
- const renamed = input.statements.find((statement) => statement.entity === "model" && sameModel(statement.from, origin));
1470
+ const renames = input.statements.filter((statement) => statement.entity === "model");
1471
+ const renamed = renames.find((statement) => sameModel$1(statement.from, origin));
879
1472
  if (renamed !== void 0) return renamed.to;
880
- if (modelAt(input.contract, origin) !== void 0) return origin;
881
- if (embeddingModelOf(input.fromContract.domain, origin) !== void 0) return void 0;
882
- const originCollection = collectionOfModel(input.fromContract.domain, origin);
883
- if (originCollection === void 0) return void 0;
884
- const collection = input.collectionAfterRenames(originCollection);
885
- const filter = JSON.stringify(variantFilterOf(input.fromContract.domain, origin) ?? null);
886
- const stored = modelCoordinates(input.contract).filter((candidate) => embeddingModelOf(input.contract.domain, candidate) === void 0 && collectionOfModel(input.contract.domain, candidate) === collection && JSON.stringify(variantFilterOf(input.contract.domain, candidate) ?? null) === filter);
1473
+ if (!unmatchedByName(origin, renames, input)) return origin;
1474
+ const unmatched = modelCoordinates(input.fromContract).filter((other) => unmatchedByName(other, renames, input));
1475
+ const stored = modelCoordinates(input.contract).filter((candidate) => modelAt(input.fromContract, candidate) === void 0 && !renames.some((statement) => sameModel$1(statement.to, candidate)) && storedAlike(origin, candidate, input) && unmatched.filter((other) => storedAlike(other, candidate, input)).length === 1);
887
1476
  const [only] = stored;
888
1477
  return stored.length === 1 ? only : void 0;
889
1478
  }
1479
+ /** Whether an origin model has no destination by statement or by name. */
1480
+ function unmatchedByName(origin, renames, input) {
1481
+ return modelAt(input.contract, origin) === void 0 && !renames.some((statement) => sameModel$1(statement.from, origin));
1482
+ }
1483
+ /** Whether an origin model and a destination model are stored the same way: neither embedded, the same collection after renames, the same variant value. */
1484
+ function storedAlike(origin, candidate, input) {
1485
+ if (embeddingModelOf(input.fromContract.domain, origin) !== void 0) return false;
1486
+ if (embeddingModelOf(input.contract.domain, candidate) !== void 0) return false;
1487
+ const originCollection = collectionOfModel(input.fromContract.domain, origin);
1488
+ return originCollection !== void 0 && collectionOfModel(input.contract.domain, candidate) === input.collectionAfterRenames(originCollection) && sameVariant(selectedVariantOf(input.contract.domain, candidate), selectedVariantOf(input.fromContract.domain, origin));
1489
+ }
1490
+ function sameVariant(a, b) {
1491
+ return a === void 0 || b === void 0 ? a === b : a.field === b.field && a.value === b.value;
1492
+ }
890
1493
  /** The origin model a destination model was, by the rules of `destinationModelOf`, read the other way. */
891
1494
  function originModelOf(destination, input) {
892
1495
  return modelCoordinates(input.fromContract).find((origin) => {
893
1496
  const matched = destinationModelOf(origin, input);
894
- return matched !== void 0 && sameModel(matched, destination);
1497
+ return matched !== void 0 && sameModel$1(matched, destination);
895
1498
  });
896
1499
  }
897
- function sameField$1(a, b) {
898
- return sameModel(a, b) && a.field === b.field;
1500
+ function sameField$3(a, b) {
1501
+ return sameModel$1(a, b) && a.field === b.field;
899
1502
  }
900
1503
  /** The name a field of the origin has in the destination: its rename statement's new name, else its own. */
901
1504
  function destinationFieldOf(origin, statements) {
902
- return statements.find((statement) => statement.entity === "field" && sameField$1(statement.from, origin))?.to.field ?? origin.field;
1505
+ return statements.find((statement) => statement.entity === "field" && sameField$3(statement.from, origin))?.to.field ?? origin.field;
903
1506
  }
904
1507
  /** The name a field of the destination had in the origin: its rename statement's old name, else its own. */
905
1508
  function originFieldOf(destination, statements) {
906
- return statements.find((statement) => statement.entity === "field" && sameField$1(statement.to, destination))?.from.field ?? destination.field;
1509
+ return statements.find((statement) => statement.entity === "field" && sameField$3(statement.to, destination))?.from.field ?? destination.field;
907
1510
  }
908
1511
  function renamedByStatement(input, field) {
909
- return input.statements.some((statement) => statement.entity === "field" && sameModel(statement.from, field) && statement.from.field === field.field);
1512
+ return input.statements.some((statement) => statement.entity === "field" && sameModel$1(statement.from, field) && statement.from.field === field.field);
910
1513
  }
911
1514
  /** Whether one of `model`'s own fields or embed relations is stored under `storedName`. */
912
1515
  function ownStores(model, storedName) {
@@ -933,19 +1536,71 @@ function rootDocumentsLosing(contract, root, storedName) {
933
1536
  });
934
1537
  return variantModel !== void 0 && ownStores(variantModel, storedName);
935
1538
  }).map(([value]) => value).sort();
936
- if (discriminator === void 0 || keeping.length === 0) return { filter: void 0 };
937
- return { filter: { [ownStoredNameOf(model, discriminator)]: { $nin: keeping } } };
1539
+ if (discriminator === void 0 || keeping.length === 0) return {
1540
+ filter: void 0,
1541
+ discriminatorFilter: void 0
1542
+ };
1543
+ const field = ownStoredNameOf(model, discriminator);
1544
+ return {
1545
+ filter: { [field]: { $nin: keeping } },
1546
+ discriminatorFilter: {
1547
+ kind: "noneOf",
1548
+ field,
1549
+ values: keeping
1550
+ }
1551
+ };
938
1552
  }
939
1553
  /**
940
- * Each field an origin model stores that its destination model no longer stores, in a collection
941
- * the plan keeps: removing it is data loss, carried out by unsetting it from the documents that
942
- * hold it, in a collection whose control policy is `managed`. A variant's field is unset only from that variant's documents, unless its destination
943
- * base now stores it. A base field is unset from the documents of every variant that does not now
944
- * store it itself.
1554
+ * The renames that move a field the documents of the origin model at `origin` store, from the
1555
+ * origin's stored name to the destination's: of its own fields, and of its bases' fields unless its
1556
+ * destination model stores the field itself, so the rename leaves its documents alone.
945
1557
  */
946
- function planFieldRemovals(input) {
947
- return modelCoordinates(input.fromContract).flatMap((origin) => {
948
- if (embeddingModelOf(input.fromContract.domain, origin) !== void 0) return [];
1558
+ function storedNameMoves(input, origin) {
1559
+ const bases = modelAndBasesOf(mongoModelsOf(input.fromContract.domain, origin.namespaceId), origin.model).slice(1);
1560
+ const destination = destinationModelOf(origin, input);
1561
+ const destinationModel = destination === void 0 ? void 0 : modelAt(input.contract, destination);
1562
+ return input.statements.flatMap((statement) => {
1563
+ if (statement.entity !== "field") return [];
1564
+ const from = storedFieldNameAt(input.fromContract, statement.from);
1565
+ const to = storedFieldNameAt(input.contract, statement.to);
1566
+ if (from === void 0 || to === void 0 || from === to) return [];
1567
+ if (sameModel$1(statement.from, origin)) return [{
1568
+ from,
1569
+ to
1570
+ }];
1571
+ const fromModel = modelAt(input.fromContract, statement.from);
1572
+ const ofABase = fromModel !== void 0 && bases.includes(fromModel);
1573
+ const keptByDestination = destinationModel !== void 0 && ownStores(destinationModel, from);
1574
+ return ofABase && !keptByDestination ? [{
1575
+ from,
1576
+ to
1577
+ }] : [];
1578
+ });
1579
+ }
1580
+ /** The stored names of the documents of the origin model at `origin`, without those a rename of one of its own fields moves to another stored name. */
1581
+ function storedFieldsKeptOf(input, origin) {
1582
+ const movedAway = new Set(storedNameMoves(input, origin).map(({ from }) => from));
1583
+ return new Set([...storedFieldsOf(input.fromContract, origin)].filter((name) => !movedAway.has(name)));
1584
+ }
1585
+ /**
1586
+ * The stored names that field rename statements move a field of `origin`'s documents into from
1587
+ * another stored name. The origin's own field under such a name no longer has a place in the
1588
+ * destination's documents, so it is removed.
1589
+ */
1590
+ function storedNamesRenamedInto(input, origin) {
1591
+ return new Set(storedNameMoves(input, origin).map(({ to }) => to));
1592
+ }
1593
+ /**
1594
+ * Each field an origin model stores that its destination model no longer stores, or whose stored
1595
+ * name a rename statement gives to another of its fields, in a collection
1596
+ * the plan keeps: removing it is data loss, carried out by unsetting it from the documents that
1597
+ * hold it, in a collection whose control policy is `managed`. A variant's field is unset only from that variant's documents, by the discriminator value they hold once the plan's replacements ran, unless its destination
1598
+ * base now stores it. A base field is unset from the documents of every variant that does not now
1599
+ * store it itself.
1600
+ */
1601
+ function planFieldRemovals(input) {
1602
+ return modelCoordinates(input.fromContract).flatMap((origin) => {
1603
+ if (embeddingModelOf(input.fromContract.domain, origin) !== void 0) return [];
949
1604
  const originCollection = collectionOfModel(input.fromContract.domain, origin);
950
1605
  if (originCollection === void 0) return [];
951
1606
  const collection = input.collectionAfterRenames(originCollection);
@@ -955,9 +1610,18 @@ function planFieldRemovals(input) {
955
1610
  if (destination === void 0) return [];
956
1611
  const model = modelAt(input.fromContract, origin);
957
1612
  if (model === void 0) return [];
958
- const variant = variantFilterOf(input.fromContract.domain, origin);
1613
+ const variant = selectedVariantOf(input.contract.domain, destination) ?? selectedVariantOf(input.fromContract.domain, origin);
1614
+ const ofVariant = variant === void 0 ? void 0 : {
1615
+ filter: variantFilter(variant),
1616
+ discriminatorFilter: variantDiscriminatorFilter(variant)
1617
+ };
1618
+ const takenByRename = storedNamesRenamedInto(input, origin);
959
1619
  const losing = (storedName) => {
960
- if (variant !== void 0) return storedFieldsOf(input.contract, destination).has(storedName) ? void 0 : { filter: variant };
1620
+ if (takenByRename.has(storedName)) return ofVariant ?? {
1621
+ filter: void 0,
1622
+ discriminatorFilter: void 0
1623
+ };
1624
+ if (ofVariant !== void 0) return storedFieldsOf(input.contract, destination).has(storedName) ? void 0 : ofVariant;
961
1625
  return rootDocumentsLosing(input.contract, destination, storedName);
962
1626
  };
963
1627
  return Object.entries(ownStoredNamesOf(model)).filter(([field, storedName]) => unrewritableFieldNameReason(storedName) === void 0 && !renamedByStatement(input, {
@@ -971,7 +1635,10 @@ function planFieldRemovals(input) {
971
1635
  kind: "field",
972
1636
  ...origin,
973
1637
  field
974
- }
1638
+ },
1639
+ destination,
1640
+ variant,
1641
+ discriminatorFilter: lost.discriminatorFilter
975
1642
  }];
976
1643
  });
977
1644
  });
@@ -991,7 +1658,10 @@ function planValidatorFieldRemovals(input) {
991
1658
  subject: {
992
1659
  kind: "storage",
993
1660
  name: `${originCollection.name}.${field}`
994
- }
1661
+ },
1662
+ destination: void 0,
1663
+ variant: void 0,
1664
+ discriminatorFilter: void 0
995
1665
  }));
996
1666
  });
997
1667
  }
@@ -1006,8 +1676,9 @@ function refuse(statement, reason, why, contracts) {
1006
1676
  }
1007
1677
  };
1008
1678
  }
1009
- function withFilter(filter, condition) {
1010
- return filter === void 0 ? condition : { $and: [filter, condition] };
1679
+ const NESTED_CONVERT_WHY = "A --convert statement converts a field of a model or a variant. Converting a field inside a value object or an embedded model is not a statement on any database in this release: rewrite the values yourself, in a dataTransform operation of the migration.ts that migration plan writes or in mongosh, and leave out this statement.";
1680
+ function withVariant(variant, condition) {
1681
+ return variant === void 0 ? condition : { $and: [variantFilter(variant), condition] };
1011
1682
  }
1012
1683
  /** Why the plan cannot rewrite a field of the destination contract, if it cannot. */
1013
1684
  function unrewritableReason(contract, field) {
@@ -1027,7 +1698,7 @@ function unrewritableReason(contract, field) {
1027
1698
  */
1028
1699
  function convertTransform(statement, change, contracts) {
1029
1700
  const embedded = embeddedModelReason(contracts.contract, statement.field);
1030
- if (embedded !== void 0) return refuse(statement, embedded, EMBEDDED_MODEL_WHY, contracts);
1701
+ if (embedded !== void 0) return refuse(statement, embedded, NESTED_CONVERT_WHY, contracts);
1031
1702
  const unrewritable = unrewritableReason(contracts.contract, statement.field);
1032
1703
  if (unrewritable !== void 0) return refuse(statement, unrewritable, "Leave out this statement.", contracts);
1033
1704
  if (change === void 0) return refuse(statement, "its type does not change", "Leave out this statement.", contracts);
@@ -1037,13 +1708,14 @@ function convertTransform(statement, change, contracts) {
1037
1708
  });
1038
1709
  return {
1039
1710
  kind: "transform",
1040
- call: new DataTransformCall(fieldStatementText(contracts.contract, statement.field), change.collection, withFilter(change.filter, notOfType)),
1711
+ call: new DataTransformCall(fieldStatementText(contracts.contract, statement.field), change.collection, withVariant(change.variant, notOfType)),
1041
1712
  rewrite: {
1042
1713
  kind: "conversion",
1043
1714
  collection: change.collection,
1044
1715
  field: change.field,
1045
- filter: change.filter,
1046
- model: change.destination
1716
+ variant: change.variant,
1717
+ model: change.destination,
1718
+ storedKey: void 0
1047
1719
  }
1048
1720
  };
1049
1721
  }
@@ -1072,8 +1744,6 @@ function backfillTransform(statement, input) {
1072
1744
  const definition = model?.fields[statement.field.field];
1073
1745
  if (model === void 0 || definition === void 0) return refuse(statement, "the destination has no such field", "Leave out this statement.", contracts);
1074
1746
  if (definition.nullable) return refuse(statement, "it is optional", "Only a required field needs a value in every document. Leave out this statement.", contracts);
1075
- const embedded = embeddedModelReason(input.contract, statement.field);
1076
- if (embedded !== void 0) return refuse(statement, embedded, EMBEDDED_MODEL_WHY, contracts);
1077
1747
  const unrewritable = unrewritableReason(input.contract, statement.field);
1078
1748
  if (unrewritable !== void 0) return refuse(statement, unrewritable, "Leave out this statement.", contracts);
1079
1749
  const collection = collectionOfModel(input.contract.domain, statement.field);
@@ -1081,16 +1751,17 @@ function backfillTransform(statement, input) {
1081
1751
  if (input.schema.collection(collection) === void 0) return refuse(statement, `collection "${collection}" is new, so it has no documents to fill`, "Leave out this statement.", contracts);
1082
1752
  const storedName = ownStoredNameOf(model, statement.field.field);
1083
1753
  if (requiredInOrigin(input, collection, statement.field, storedName)) return refuse(statement, "it was already required", "Every document already has a value for it. Leave out this statement.", contracts);
1084
- const variant = variantFilterOf(input.contract.domain, statement.field);
1754
+ const variant = selectedVariantOf(input.contract.domain, statement.field);
1085
1755
  return {
1086
1756
  kind: "transform",
1087
- call: new DataTransformCall(fieldStatementText(input.contract, statement.field), collection, withFilter(variant, documentsMissingOrNull(storedName))),
1757
+ call: new DataTransformCall(fieldStatementText(input.contract, statement.field), collection, withVariant(variant, documentsMissingOrNull(storedName))),
1088
1758
  rewrite: {
1089
1759
  kind: "backfill",
1090
1760
  collection,
1091
1761
  field: storedName,
1092
- filter: variant,
1093
- model: statement.field
1762
+ variant,
1763
+ model: statement.field,
1764
+ storedKey: void 0
1094
1765
  }
1095
1766
  };
1096
1767
  }
@@ -1137,23 +1808,17 @@ function untouchedChange(name, before, after) {
1137
1808
  if (objectBefore !== void 0 && objectAfter === void 0 && after.type.kind === "scalar") return `Field "${name}" changes from value object "${objectBefore}" to a scalar`;
1138
1809
  }
1139
1810
  /**
1140
- * What changes for each field of an embedded model, whose documents the plan does not rewrite: a
1141
- * field removed, a field whose type changes, and a required field that is new or was optional.
1811
+ * What changes for each field of an embedded model kept by both contracts that the plan does not rewrite: a field whose type changes. A removed or renamed field is a statement, and a required field that is new or was optional is filled, which the plan rewrites.
1142
1812
  */
1143
- function embeddedModelChanges(fieldName, model, before, after) {
1144
- const named = (field) => `Field "${fieldName(field)}" of embedded model "${model}"`;
1145
- const kept = Object.entries(after.fields).flatMap(([field, definition]) => {
1146
- const previous = Object.hasOwn(before.fields, field) ? before.fields[field] : void 0;
1147
- if (previous === void 0) return definition.nullable ? [] : [`${named(field)} is new and required`];
1148
- if (typeKey(previous) !== typeKey(definition)) return [`${named(field)} changes type`];
1149
- return previous.nullable && !definition.nullable ? [`${named(field)} becomes required`] : [];
1813
+ function embeddedModelChanges$1(fieldName, originField, model, before, after) {
1814
+ return Object.entries(after.fields).flatMap(([field, definition]) => {
1815
+ const origin = originField(field);
1816
+ const previous = Object.hasOwn(before.fields, origin) ? before.fields[origin] : void 0;
1817
+ return previous !== void 0 && typeKey(previous) !== typeKey(definition) ? [`Field "${fieldName(field)}" of embedded model "${model}" changes type`] : [];
1150
1818
  });
1151
- const removed = Object.keys(before.fields).filter((field) => !Object.hasOwn(after.fields, field)).map((field) => `${named(field)} is removed`);
1152
- return [...kept, ...removed];
1153
1819
  }
1154
1820
  /**
1155
- * The changes the plan does not rewrite the documents for, and that are not statements: a change to
1156
- * a field of an embedded model, a field of a value object whose type changes, a field that moves to
1821
+ * The changes the plan does not rewrite the documents for, and that are not statements: a type change of a field of an embedded model, a field of a value object whose type changes, a field that moves to
1157
1822
  * another value object or between a scalar and a value object, and a field that gains an enum or
1158
1823
  * changes enum. Each is a warning that names the field; the validator change's document check then
1159
1824
  * fails the apply until the documents fit.
@@ -1169,7 +1834,11 @@ function documentsNotRewrittenWarnings(input) {
1169
1834
  ...model,
1170
1835
  field
1171
1836
  });
1172
- return embeddedModelChanges(fieldName, model.model, before, after).map(warning);
1837
+ const originField = (field) => originFieldOf({
1838
+ ...model,
1839
+ field
1840
+ }, input.statements);
1841
+ return embeddedModelChanges$1(fieldName, originField, model.model, before, after).map(warning);
1173
1842
  }
1174
1843
  return Object.entries(after.fields).flatMap(([field, definition]) => {
1175
1844
  const originField = originFieldOf({
@@ -1198,10 +1867,966 @@ function documentsNotRewrittenWarnings(input) {
1198
1867
  });
1199
1868
  return [...modelFields, ...valueObjectFields];
1200
1869
  }
1870
+ function isMany(many) {
1871
+ return many !== void 0 && many !== false;
1872
+ }
1873
+ function fieldMember(storedName, field) {
1874
+ const { type } = field;
1875
+ if (type.kind === "union") {
1876
+ const types = type.members.flatMap((member) => member.kind === "valueObject" ? [valueObjectKey(member)] : []);
1877
+ return {
1878
+ storedName,
1879
+ many: isMany(field.many),
1880
+ holds: void 0,
1881
+ unlisted: {
1882
+ kind: "union",
1883
+ types
1884
+ }
1885
+ };
1886
+ }
1887
+ if (type.kind !== "valueObject") return {
1888
+ storedName,
1889
+ many: false,
1890
+ holds: void 0,
1891
+ unlisted: void 0
1892
+ };
1893
+ if (field.dict === true) return {
1894
+ storedName,
1895
+ many: false,
1896
+ holds: void 0,
1897
+ unlisted: {
1898
+ kind: "dictionary",
1899
+ types: [valueObjectKey(type)]
1900
+ }
1901
+ };
1902
+ return {
1903
+ storedName,
1904
+ many: isMany(field.many),
1905
+ holds: valueObjectKey(type),
1906
+ unlisted: void 0
1907
+ };
1908
+ }
1909
+ function membersOf(fields, storedNames) {
1910
+ return Object.entries(fields).flatMap(([name, field]) => {
1911
+ const storedName = storedNames[name];
1912
+ return storedName === void 0 ? [] : [fieldMember(storedName, field)];
1913
+ });
1914
+ }
1915
+ function modelMembers(model) {
1916
+ const embedStoredNames = ownEmbedStoredNamesOf(model);
1917
+ return [...membersOf(model.fields, ownStoredNamesOf(model)), ...Object.entries(model.relations).flatMap(([name, relation]) => {
1918
+ const storedName = embedStoredNames[name];
1919
+ return isEmbedRelation(relation) && storedName !== void 0 ? [{
1920
+ storedName,
1921
+ many: relation.cardinality === "1:N",
1922
+ holds: modelKey(relation.to.model),
1923
+ unlisted: void 0
1924
+ }] : [];
1925
+ })];
1926
+ }
1927
+ /** The walk over one namespace's models and the value objects their documents hold. */
1928
+ var PlaceWalk = class {
1929
+ #nodes = /* @__PURE__ */ new Map();
1930
+ #modelsWithVariants = /* @__PURE__ */ new Set();
1931
+ #groups;
1932
+ #target;
1933
+ constructor(contract, models, target) {
1934
+ this.#target = target;
1935
+ this.#groups = recursiveGroupsOf(contract.storage, models);
1936
+ for (const { coordinate, valueObject } of valueObjectsWithCoordinates(contract.storage)) this.#nodes.set(valueObjectKey(coordinate), {
1937
+ name: coordinate.entityName,
1938
+ members: membersOf(valueObject.fields, valueObjectStoredNamesOf(valueObject))
1939
+ });
1940
+ for (const [name, model] of Object.entries(models)) {
1941
+ this.#nodes.set(modelKey(name), {
1942
+ name,
1943
+ members: modelMembers(model)
1944
+ });
1945
+ if (Object.keys(model.variants ?? {}).length > 0) this.#modelsWithVariants.add(modelKey(name));
1946
+ }
1947
+ }
1948
+ /** Whether an object of type `key` is, or holds at any depth, an object of the target type. */
1949
+ #reaches(key) {
1950
+ const seen = /* @__PURE__ */ new Set();
1951
+ const pending = [key];
1952
+ for (let next = pending.pop(); next !== void 0; next = pending.pop()) {
1953
+ if (next === this.#target) return true;
1954
+ if (seen.has(next)) continue;
1955
+ seen.add(next);
1956
+ for (const member of this.#nodes.get(next)?.members ?? []) {
1957
+ if (member.holds !== void 0) pending.push(member.holds);
1958
+ pending.push(...member.unlisted?.types ?? []);
1959
+ }
1960
+ }
1961
+ return false;
1962
+ }
1963
+ /** The paths from an object with `members`, reached by `steps`, to each object of the target type below it. */
1964
+ #pathsBelow(members, steps) {
1965
+ const paths = [];
1966
+ for (const member of members) {
1967
+ const found = this.#pathsOfMember(member, steps);
1968
+ if (!found.ok) return found;
1969
+ paths.push(...found.value);
1970
+ }
1971
+ return ok(paths);
1972
+ }
1973
+ #pathsOfMember(member, steps) {
1974
+ if (member.unlisted !== void 0) return member.unlisted.types.some((type) => this.#reaches(type)) ? notOk({ kind: member.unlisted.kind }) : ok([]);
1975
+ const { holds } = member;
1976
+ if (holds === void 0) return ok([]);
1977
+ if (this.#groups.has(holds)) return this.#reaches(holds) ? notOk({
1978
+ kind: "containsItself",
1979
+ type: this.#nodes.get(holds)?.name ?? holds
1980
+ }) : ok([]);
1981
+ if (this.#modelsWithVariants.has(holds)) throw new InternalError(`Embedded model "${this.#nodes.get(holds)?.name ?? holds}" has variants, which contract validation refuses`);
1982
+ const memberSteps = [
1983
+ ...steps,
1984
+ {
1985
+ kind: "member",
1986
+ name: member.storedName
1987
+ },
1988
+ ...member.many ? [{ kind: "wildcard" }] : []
1989
+ ];
1990
+ const own = holds === this.#target ? [memberSteps] : [];
1991
+ const nested = this.#pathsBelow(this.#nodes.get(holds)?.members ?? [], memberSteps);
1992
+ return nested.ok ? ok([...own, ...nested.value]) : nested;
1993
+ }
1994
+ /** The places the documents of a root model or variant hold objects of the target type in its own fields and embed relations. */
1995
+ placesOf(model, root) {
1996
+ const places = [];
1997
+ for (const member of modelMembers(model)) {
1998
+ const found = this.#pathsOfMember(member, []);
1999
+ if (!found.ok) return notOk({
2000
+ ...root,
2001
+ field: member.storedName,
2002
+ reach: found.failure
2003
+ });
2004
+ places.push(...found.value.map((steps) => ({
2005
+ ...root,
2006
+ field: member.storedName,
2007
+ steps: steps.slice(1)
2008
+ })));
2009
+ }
2010
+ return ok(places);
2011
+ }
2012
+ };
2013
+ function targetKey(type) {
2014
+ return type.kind === "valueObject" ? valueObjectKey(type.valueObject) : modelKey(type.model.model);
2015
+ }
2016
+ /**
2017
+ * Finds every place a contract's documents hold objects of a value object or an embedded model: for each root field of a root model or variant, the steps from the field's value to each object, through value object fields and embed relations (a list step for a list), at any depth. Steps use stored names. A root field that holds the type through a value object or embedded model that contains itself, a union or a dictionary has no list of paths, and is the failure.
2018
+ */
2019
+ function storedPlacesOf(contract) {
2020
+ return (type) => {
2021
+ const target = targetKey(type);
2022
+ const places = [];
2023
+ for (const namespaceId of Object.keys(contract.domain.namespaces)) {
2024
+ const models = mongoModelsOf(contract.domain, namespaceId);
2025
+ const walk = new PlaceWalk(contract, models, target);
2026
+ for (const [name, model] of Object.entries(models)) {
2027
+ const coordinate = {
2028
+ namespaceId: asNamespaceId(namespaceId),
2029
+ model: name
2030
+ };
2031
+ if (embeddingModelOf(contract.domain, coordinate) !== void 0) continue;
2032
+ const collection = collectionOfModel(contract.domain, coordinate);
2033
+ if (collection === void 0) continue;
2034
+ const found = walk.placesOf(model, {
2035
+ collection,
2036
+ model: coordinate,
2037
+ variant: selectedVariantOf(contract.domain, coordinate)
2038
+ });
2039
+ if (!found.ok) return found;
2040
+ places.push(...found.value);
2041
+ }
2042
+ }
2043
+ return ok(places);
2044
+ };
2045
+ }
2046
+ function valueObjectChanges(fromContract, contract, statements) {
2047
+ return valueObjectsWithCoordinates(fromContract.storage).flatMap(({ coordinate, valueObject }) => {
2048
+ const destination = valueObjectAt(contract.storage, coordinate);
2049
+ if (destination === void 0) return [];
2050
+ return Object.keys(valueObject.fields).flatMap((field) => {
2051
+ const subject = {
2052
+ kind: "valueObjectField",
2053
+ namespaceId: asNamespaceId(coordinate.namespaceId),
2054
+ valueObject: coordinate.entityName,
2055
+ field
2056
+ };
2057
+ const type = {
2058
+ kind: "valueObject",
2059
+ valueObject: coordinate
2060
+ };
2061
+ const statement = statements.find((each) => each.entity === "valueObjectField" && each.from.namespaceId === subject.namespaceId && each.from.valueObject === subject.valueObject && each.from.field === field);
2062
+ if (statement !== void 0) return [{
2063
+ type,
2064
+ subject,
2065
+ from: field,
2066
+ to: statement.to.field,
2067
+ statement
2068
+ }];
2069
+ const takenByRename = statements.some((each) => each.entity === "valueObjectField" && each.from.namespaceId === subject.namespaceId && each.from.valueObject === subject.valueObject && each.from.field !== field && each.to.field === field);
2070
+ return Object.hasOwn(destination.fields, field) && !takenByRename ? [] : [{
2071
+ type,
2072
+ subject,
2073
+ from: field,
2074
+ to: void 0,
2075
+ statement: void 0
2076
+ }];
2077
+ });
2078
+ });
2079
+ }
2080
+ function sameModelAs(a, b) {
2081
+ return a.namespaceId === b.namespaceId && a.model === b.model;
2082
+ }
2083
+ function sameField$2(a, b) {
2084
+ return sameModelAs(a, b) && a.field === b.field;
2085
+ }
2086
+ function embeddedModelChanges(fromContract, contract, statements) {
2087
+ return modelCoordinates(fromContract).flatMap((coordinate) => {
2088
+ if (embeddingModelOf(fromContract.domain, coordinate) === void 0) return [];
2089
+ if (embeddingModelOf(contract.domain, coordinate) === void 0) return [];
2090
+ const origin = modelAt(fromContract, coordinate);
2091
+ const destination = modelAt(contract, coordinate);
2092
+ if (origin === void 0 || destination === void 0) return [];
2093
+ const kept = /* @__PURE__ */ new Set([...Object.values(ownStoredNamesOf(destination)), ...Object.values(ownEmbedStoredNamesOf(destination))]);
2094
+ const type = {
2095
+ kind: "embeddedModel",
2096
+ model: coordinate
2097
+ };
2098
+ const takenByRename = new Set(statements.flatMap((each) => {
2099
+ if (each.entity !== "field" || !sameModelAs(each.from, coordinate)) return [];
2100
+ const from = Object.hasOwn(origin.fields, each.from.field) ? ownStoredNameOf(origin, each.from.field) : void 0;
2101
+ const to = Object.hasOwn(destination.fields, each.to.field) ? ownStoredNameOf(destination, each.to.field) : void 0;
2102
+ return to === void 0 || to === from ? [] : [to];
2103
+ }));
2104
+ return Object.entries(ownStoredNamesOf(origin)).flatMap(([field, from]) => {
2105
+ const subject = {
2106
+ kind: "field",
2107
+ ...coordinate,
2108
+ field
2109
+ };
2110
+ const statement = statements.find((each) => each.entity === "field" && sameField$2(each.from, subject));
2111
+ if (statement !== void 0) {
2112
+ const to = Object.hasOwn(destination.fields, statement.to.field) ? ownStoredNameOf(destination, statement.to.field) : void 0;
2113
+ return to === void 0 || to === from ? [] : [{
2114
+ type,
2115
+ subject,
2116
+ from,
2117
+ to,
2118
+ statement
2119
+ }];
2120
+ }
2121
+ return kept.has(from) && !takenByRename.has(from) ? [] : [{
2122
+ type,
2123
+ subject,
2124
+ from,
2125
+ to: void 0,
2126
+ statement: void 0
2127
+ }];
2128
+ });
2129
+ });
2130
+ }
2131
+ /** The field a key change is about, as a refusal names it. */
2132
+ function fieldText$2(change, contracts) {
2133
+ return change.subject.kind === "valueObjectField" ? `value object field "${valueObjectFieldStatementText(contracts.fromContract, change.subject)}"` : `field "${fieldStatementText(contracts.fromContract, change.subject)}"`;
2134
+ }
2135
+ function refused$2(change, contracts, reason, why) {
2136
+ if (change.statement === void 0) return {
2137
+ kind: "fieldRemovalRefused",
2138
+ summary: `Cannot remove ${fieldText$2(change, contracts)}: ${reason}`,
2139
+ why: why("keep the field in the contract")
2140
+ };
2141
+ return {
2142
+ kind: "statementRefused",
2143
+ summary: `Cannot ${describeMigrationStatement(change.statement, contracts.fromContract, contracts.contract)}: ${reason}`,
2144
+ why: why("leave out this statement"),
2145
+ refusedStatement: change.statement
2146
+ };
2147
+ }
2148
+ function placeText$1(place) {
2149
+ return `field "${place.field}" of collection "${place.collection}"`;
2150
+ }
2151
+ function unlistedRefusal$2(change, place, contracts) {
2152
+ const byHand = (advice) => `Rewrite ${placeText$1(place)} yourself, in a dataTransform operation of the migration.ts that migration plan writes or in mongosh, or ${advice}.`;
2153
+ if (place.reach.kind === "containsItself") return refused$2(change, contracts, `${placeText$1(place)} holds it inside "${place.reach.type}", which contains itself, so the documents can hold it at any depth`, (advice) => `The plan rewrites a key at every path from a field to its objects, and a type that contains itself has no last path. ${byHand(advice)}`);
2154
+ return refused$2(change, contracts, `${placeText$1(place)} holds a ${place.reach.kind} of value objects`, (advice) => `The plan rewrites a key only in a field that holds a value object, a list of them, or an embedded model. ${byHand(advice)}`);
2155
+ }
2156
+ function unrewritableKeyRefusal(change, keys, contracts) {
2157
+ for (const key of keys) {
2158
+ const reason = unrewritableKeyReason(key);
2159
+ if (reason === void 0) continue;
2160
+ return refused$2(change, contracts, `the stored name "${key}" cannot be rewritten`, (advice) => `${reason}, so the plan cannot rewrite the key. Rewrite it in the database yourself, or ${advice}.`);
2161
+ }
2162
+ if (change.to === void 0) return void 0;
2163
+ const reason = unrewritableKeyReason(change.to);
2164
+ return reason === void 0 ? void 0 : refused$2(change, contracts, `the stored name "${change.to}" cannot be written`, (advice) => `${reason}. Give the field a stored name without "." that does not start with "$", or ${advice}.`);
2165
+ }
2166
+ function rewriteKey(place, path) {
2167
+ return JSON.stringify([
2168
+ place.collection,
2169
+ place.field,
2170
+ jsonPathText(path),
2171
+ place.variant ?? null
2172
+ ]);
2173
+ }
2174
+ function callOf$1(planned) {
2175
+ const { change, place, path } = planned;
2176
+ const at = {
2177
+ collection: place.collection,
2178
+ field: place.field,
2179
+ path: jsonPathText(path)
2180
+ };
2181
+ const filter = place.variant === void 0 ? {} : { filter: variantFilter(place.variant) };
2182
+ return change.to === void 0 ? new RemoveStoredKeyCall({
2183
+ ...at,
2184
+ ...filter
2185
+ }) : new RenameStoredKeyCall({
2186
+ ...at,
2187
+ to: change.to,
2188
+ ...filter
2189
+ });
2190
+ }
2191
+ function plannedRewriteOf$1(planned, contracts) {
2192
+ return {
2193
+ kind: planned.change.to === void 0 ? "removeStoredKey" : "renameStoredKey",
2194
+ collection: planned.place.collection,
2195
+ field: planned.place.field,
2196
+ variant: planned.place.variant,
2197
+ model: planned.place.model,
2198
+ storedKey: {
2199
+ key: fieldText$2(planned.change, contracts),
2200
+ together: planned.models
2201
+ }
2202
+ };
2203
+ }
2204
+ function policyRefusal(change, place, policy, contracts) {
2205
+ return refused$2(change, contracts, `collection "${place.collection}" has control policy "${policy}"`, () => "Statements rename only collections, and fields of collections, whose control policy is \"managed\". Make the change in the database yourself and leave out this statement.");
2206
+ }
2207
+ function rewriteCollectionOf(schema, name) {
2208
+ const collection = schema.collection(name);
2209
+ return {
2210
+ timeSeries: collection?.options?.timeseries !== void 0,
2211
+ validator: collection?.validator?.jsonSchema
2212
+ };
2213
+ }
2214
+ /**
2215
+ * The key rewrites of the fields of value objects and embedded models a plan makes. A value object or embedded model of the origin is compared, field by field, with the one of the same name in the destination: a rename statement renames a field's key, and a field whose stored name the destination no longer has, which no statement renames, is removed, which the plan asks about. Each rewrite acts at every place the destination's documents hold the type, in a collection the plan keeps, with the destination's names on the path; renames run outer keys first. A rename in a collection whose control policy is not `managed` is refused, and a removal there is left out, as top-level removals are. With no origin contract, the removals are read from the two validators.
2216
+ */
2217
+ function planStoredKeyRewrites(input) {
2218
+ const { fromContract, contract } = input;
2219
+ if (fromContract === null) {
2220
+ const removals = validatorKeyRemovals(input);
2221
+ const refusals = removals.flatMap(({ call, key }) => rewriteRefusals({
2222
+ kind: "removeStoredKey",
2223
+ collection: call.options.collection,
2224
+ field: call.options.field,
2225
+ variant: void 0,
2226
+ model: void 0,
2227
+ storedKey: {
2228
+ key: `key ${JSON.stringify(key)}`,
2229
+ together: []
2230
+ }
2231
+ }, contract, rewriteCollectionOf(input.schema, call.options.collection)));
2232
+ return refusals.length > 0 ? notOk(dedupe(refusals)) : ok({
2233
+ renames: [],
2234
+ removals: removals.map(({ key: _, ...removal }) => removal)
2235
+ });
2236
+ }
2237
+ const contracts = {
2238
+ fromContract,
2239
+ contract
2240
+ };
2241
+ const changes = [...valueObjectChanges(fromContract, contract, input.statements), ...embeddedModelChanges(fromContract, contract, input.statements)];
2242
+ const placesOf = storedPlacesOf(contract);
2243
+ const conflicts = [];
2244
+ const planned = /* @__PURE__ */ new Map();
2245
+ for (const change of changes) {
2246
+ const places = placesOf(change.type);
2247
+ if (!places.ok) {
2248
+ conflicts.push(unlistedRefusal$2(change, places.failure, contracts));
2249
+ continue;
2250
+ }
2251
+ for (const place of places.value) {
2252
+ if (input.schema.collection(place.collection) === void 0) continue;
2253
+ const policy = collectionControlPolicy(contract, place.collection);
2254
+ if (policy !== "managed" && change.statement === void 0) continue;
2255
+ const refusal = policy !== "managed" ? policyRefusal(change, place, policy, contracts) : unrewritableKeyRefusal(change, [
2256
+ place.field,
2257
+ ...place.steps.flatMap((step) => step.kind === "member" ? [step.name] : []),
2258
+ change.from
2259
+ ], contracts);
2260
+ if (refusal !== void 0) {
2261
+ conflicts.push(refusal);
2262
+ continue;
2263
+ }
2264
+ const path = [...place.steps, {
2265
+ kind: "member",
2266
+ name: change.from
2267
+ }];
2268
+ const key = JSON.stringify([rewriteKey(place, path), change.to ?? null]);
2269
+ const known = planned.get(key);
2270
+ if (known !== void 0) {
2271
+ known.models.push(place.model.model);
2272
+ continue;
2273
+ }
2274
+ planned.set(key, {
2275
+ change,
2276
+ place,
2277
+ path,
2278
+ models: [place.model.model]
2279
+ });
2280
+ }
2281
+ }
2282
+ for (const each of planned.values()) conflicts.push(...rewriteRefusals(plannedRewriteOf$1(each, contracts), contract, rewriteCollectionOf(input.schema, each.place.collection)));
2283
+ if (conflicts.length > 0) return notOk(dedupe(conflicts));
2284
+ const rewrites = [...planned.values()].map((each, order) => ({
2285
+ each,
2286
+ order
2287
+ })).sort((a, b) => a.each.path.length - b.each.path.length || a.order - b.order).map(({ each }) => ({
2288
+ call: callOf$1(each),
2289
+ subject: each.change.subject,
2290
+ statement: each.change.statement,
2291
+ variant: each.place.variant
2292
+ }));
2293
+ return ok({
2294
+ renames: rewrites.flatMap(({ call, ...rewrite }) => call instanceof RenameStoredKeyCall ? [{
2295
+ ...rewrite,
2296
+ call
2297
+ }] : []),
2298
+ removals: rewrites.flatMap(({ call, ...rewrite }) => call instanceof RemoveStoredKeyCall ? [{
2299
+ ...rewrite,
2300
+ call
2301
+ }] : [])
2302
+ });
2303
+ }
2304
+ function dedupe(conflicts) {
2305
+ const seen = /* @__PURE__ */ new Set();
2306
+ return conflicts.filter((conflict) => {
2307
+ const key = JSON.stringify([
2308
+ conflict.kind,
2309
+ conflict.summary,
2310
+ conflict.why
2311
+ ]);
2312
+ if (seen.has(key)) return false;
2313
+ seen.add(key);
2314
+ return true;
2315
+ });
2316
+ }
2317
+ function isRecord$3(value) {
2318
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2319
+ }
2320
+ /** The keys a validator's property schema declares for each object below its field, by the path to those objects. */
2321
+ function declaredKeys(schema, steps, found) {
2322
+ if (!isRecord$3(schema)) return;
2323
+ const { properties, items } = schema;
2324
+ if (isRecord$3(properties)) {
2325
+ const text = jsonPathText(steps);
2326
+ const entry = found.get(text) ?? {
2327
+ steps,
2328
+ keys: /* @__PURE__ */ new Set()
2329
+ };
2330
+ found.set(text, entry);
2331
+ for (const [key, property] of Object.entries(properties)) {
2332
+ entry.keys.add(key);
2333
+ declaredKeys(property, [...steps, {
2334
+ kind: "member",
2335
+ name: key
2336
+ }], found);
2337
+ }
2338
+ }
2339
+ if (items !== void 0) declaredKeys(items, [...steps, { kind: "wildcard" }], found);
2340
+ for (const branch of [schema["oneOf"], schema["anyOf"]].flat()) declaredKeys(branch, steps, found);
2341
+ }
2342
+ /** The keys each root field's objects are declared with, over every kind of document a validator describes. */
2343
+ function declaredKeysOf(jsonSchema) {
2344
+ const byField = /* @__PURE__ */ new Map();
2345
+ for (const shape of validatorDocumentShapesOf(jsonSchema)) for (const [field, schema] of Object.entries(shape.properties)) {
2346
+ const found = byField.get(field) ?? /* @__PURE__ */ new Map();
2347
+ byField.set(field, found);
2348
+ declaredKeys(schema, [], found);
2349
+ }
2350
+ return byField;
2351
+ }
2352
+ /**
2353
+ * Without an origin contract, a key inside a root field is known removed when the origin validator declares it on the objects at a path and the destination validator declares those objects without it. It is named by its storage name, the collection, field and keys joined by dots.
2354
+ */
2355
+ function validatorKeyRemovals(input) {
2356
+ return input.schema.collections.flatMap((origin) => {
2357
+ const collection = origin.name;
2358
+ if (collectionControlPolicy(input.contract, collection) !== "managed") return [];
2359
+ const after = input.destination.collection(collection)?.validator?.jsonSchema;
2360
+ if (after === void 0 || origin.validator === void 0) return [];
2361
+ const kept = declaredKeysOf(after);
2362
+ return [...declaredKeysOf(origin.validator.jsonSchema)].flatMap(([field, objects]) => {
2363
+ const keptObjects = kept.get(field);
2364
+ if (keptObjects === void 0 || unrewritableKeyReason(field) !== void 0) return [];
2365
+ return [...objects].flatMap(([text, { steps, keys }]) => {
2366
+ const keptKeys = keptObjects.get(text)?.keys;
2367
+ if (keptKeys === void 0) return [];
2368
+ const members = steps.flatMap((step) => step.kind === "member" ? [step.name] : []);
2369
+ if (members.some((key) => unrewritableKeyReason(key) !== void 0)) return [];
2370
+ return [...keys].filter((key) => !keptKeys.has(key) && unrewritableKeyReason(key) === void 0).sort().map((key) => ({
2371
+ call: new RemoveStoredKeyCall({
2372
+ collection,
2373
+ field,
2374
+ path: jsonPathText([...steps, {
2375
+ kind: "member",
2376
+ name: key
2377
+ }])
2378
+ }),
2379
+ subject: {
2380
+ kind: "storage",
2381
+ name: [
2382
+ collection,
2383
+ field,
2384
+ ...members,
2385
+ key
2386
+ ].join(".")
2387
+ },
2388
+ statement: void 0,
2389
+ variant: void 0,
2390
+ key
2391
+ }));
2392
+ });
2393
+ });
2394
+ });
2395
+ }
2396
+ function sameValue(left, right) {
2397
+ return JSON.stringify(left) === JSON.stringify(right);
2398
+ }
2399
+ function isStoredValue(value) {
2400
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
2401
+ }
2402
+ /** Whether the field takes the enum's values: a model field names the enum, and a value object field its storage value set, which has the enum's name. */
2403
+ function typedBy(field, namespaceId, enumName) {
2404
+ const ref = field.valueSet;
2405
+ return ref !== void 0 && ref.spaceId === void 0 && ref.namespaceId === namespaceId && ref.entityName === enumName;
2406
+ }
2407
+ function isRequired(field) {
2408
+ return !field.nullable && (field.many === void 0 || field.many === false);
2409
+ }
2410
+ /**
2411
+ * Every place the destination's documents hold values of the enum: each root field of a root model
2412
+ * or variant it types, and each key of a value object or an embedded model it types, at every path
2413
+ * the documents hold that type.
2414
+ */
2415
+ function placesOfEnum(contract, namespaceId, enumName) {
2416
+ const places = [];
2417
+ const nestedTypes = [];
2418
+ for (const coordinate of modelCoordinates(contract)) {
2419
+ const model = mongoModelsOf(contract.domain, coordinate.namespaceId)[coordinate.model];
2420
+ if (model === void 0) continue;
2421
+ const storedNames = ownStoredNamesOf(model);
2422
+ const typed = Object.entries(model.fields).flatMap(([name, field]) => {
2423
+ const storedName = storedNames[name];
2424
+ return storedName !== void 0 && typedBy(field, namespaceId, enumName) ? [[storedName, field]] : [];
2425
+ });
2426
+ if (typed.length === 0) continue;
2427
+ if (embeddingModelOf(contract.domain, coordinate) !== void 0) {
2428
+ nestedTypes.push({
2429
+ type: {
2430
+ kind: "embeddedModel",
2431
+ model: coordinate
2432
+ },
2433
+ fields: typed
2434
+ });
2435
+ continue;
2436
+ }
2437
+ const collection = collectionOfModel(contract.domain, coordinate);
2438
+ if (collection === void 0) continue;
2439
+ const variant = selectedVariantOf(contract.domain, coordinate);
2440
+ for (const [storedName, field] of typed) places.push({
2441
+ place: {
2442
+ collection,
2443
+ field: storedName,
2444
+ model: coordinate,
2445
+ variant
2446
+ },
2447
+ path: void 0,
2448
+ keys: [storedName],
2449
+ required: isRequired(field)
2450
+ });
2451
+ }
2452
+ for (const { coordinate, valueObject } of valueObjectsWithCoordinates(contract.storage)) {
2453
+ const storedNames = valueObjectStoredNamesOf(valueObject);
2454
+ const typed = Object.entries(valueObject.fields).flatMap(([name, field]) => {
2455
+ const storedName = storedNames[name];
2456
+ return storedName !== void 0 && typedBy(field, namespaceId, enumName) ? [[storedName, field]] : [];
2457
+ });
2458
+ if (typed.length > 0) nestedTypes.push({
2459
+ type: {
2460
+ kind: "valueObject",
2461
+ valueObject: coordinate
2462
+ },
2463
+ fields: typed
2464
+ });
2465
+ }
2466
+ const placesOf = storedPlacesOf(contract);
2467
+ for (const { type, fields } of nestedTypes) {
2468
+ const found = placesOf(type);
2469
+ if (!found.ok) return notOk(found.failure);
2470
+ for (const { steps, ...place } of found.value) for (const [storedName, field] of fields) {
2471
+ const path = [...steps, {
2472
+ kind: "member",
2473
+ name: storedName
2474
+ }];
2475
+ places.push({
2476
+ place,
2477
+ path: jsonPathText(path),
2478
+ keys: [place.field, ...path.flatMap((step) => step.kind === "member" ? [step.name] : [])],
2479
+ required: isRequired(field)
2480
+ });
2481
+ }
2482
+ }
2483
+ return ok(places);
2484
+ }
2485
+ /**
2486
+ * What changes for each member of the origin enum, as SQL compares enums: a rename statement gives
2487
+ * a member the stored value of the member it names; a member the destination has under the same
2488
+ * name takes that member's stored value; a member the destination lacks is kept when the
2489
+ * destination still stores its value, and otherwise its value is removed.
2490
+ */
2491
+ function valueChanges(enums, enumValueOf, statementOf) {
2492
+ return enums.originEnum.members.flatMap((member) => {
2493
+ const enumValue = enumValueOf(member.name);
2494
+ const statement = statementOf(member.name);
2495
+ const named = statement?.to.member ?? member.name;
2496
+ const target = enums.destinationEnum.members.find(({ name }) => name === named);
2497
+ if (target !== void 0) return sameValue(target.value, member.value) ? [] : [{
2498
+ enumValue,
2499
+ from: member.value,
2500
+ to: target.value,
2501
+ statement
2502
+ }];
2503
+ if (statement !== void 0) return [];
2504
+ if (enums.destinationEnum.members.some(({ value }) => sameValue(value, member.value))) return [];
2505
+ return [{
2506
+ enumValue,
2507
+ from: member.value,
2508
+ to: void 0,
2509
+ statement: void 0
2510
+ }];
2511
+ });
2512
+ }
2513
+ /**
2514
+ * The changes of an enum whose new value another change reads, or whose old value another change
2515
+ * writes: swaps and chains. A removal among them cannot keep its data with a replacement in the
2516
+ * same migration, which the plan would refuse.
2517
+ */
2518
+ function overlappingChanges(changes) {
2519
+ const replacing = changes.filter((change) => change.to !== void 0);
2520
+ return replacing.filter((change) => replacing.some((other) => other !== change && (other.to !== void 0 && sameValue(other.to, change.from) || change.to !== void 0 && sameValue(change.to, other.from))));
2521
+ }
2522
+ function isBooleanEnum(enums) {
2523
+ return enums.originEnum.members.some(({ value }) => typeof value === "boolean");
2524
+ }
2525
+ const NO_THIRD_BOOLEAN = "A boolean enum has no third value to move a stored value to, so the change cannot be split into two steps.";
2526
+ const SWAP_IN_ONE_UPDATE = "swap the stored values yourself in one update of the database, such as an updateMany whose pipeline sets each field that holds the enum to its negation with $not";
2527
+ function twoMigrationsAdvice(enums, changes) {
2528
+ if (isBooleanEnum(enums)) return `${NO_THIRD_BOOLEAN} To keep that data, plan the change with migration plan, remove the removeStoredValue calls of this enum from the migration.ts it writes, and ${SWAP_IN_ONE_UPDATE}, before you apply it.`;
2529
+ const names = changes.map(({ enumValue }) => `"${enumValueStatementText(enums.fromContract, enumValue)}"`);
2530
+ return `To keep that data: ${inTwoSteps(`move the stored values of ${names.length === 1 ? `enum value ${names.join("")}` : `enum values ${names.slice(0, -1).join(", ")} and ${names.at(-1)}`} to values the enum does not use`, "move them to their final values")} MongoDB applies the operations one at a time, so replacements whose old values other replacements write cannot safely run again after a later operation stops.`;
2531
+ }
2532
+ /** How to make a change of the enum's stored values that one migration cannot make safely. */
2533
+ function enumTwoMigrationsAdvice(enums) {
2534
+ return isBooleanEnum(enums) ? `${NO_THIRD_BOOLEAN} Leave out these statements, plan the change with migration plan, remove the removeStoredValue calls of this enum from the migration.ts it writes, and ${SWAP_IN_ONE_UPDATE}, before you apply it.` : inTwoSteps("move every value they swap or chain to a stored value the enum does not use", "move them to their final values");
2535
+ }
2536
+ /** The expression that writes a stored value with the BSON type the enum's codec stores. */
2537
+ function valueExpression(value, bsonTypes) {
2538
+ if (typeof value === "string") return { $literal: value };
2539
+ if (typeof value === "boolean") return value;
2540
+ if (bsonTypes.includes("double")) return { $toDouble: value };
2541
+ if (bsonTypes.includes("long")) return { $toLong: value };
2542
+ if (bsonTypes.includes("decimal")) return { $toDecimal: String(value) };
2543
+ return value;
2544
+ }
2545
+ function enumValueText(contract, value) {
2546
+ return `enum value "${enumValueStatementText(contract, value)}"`;
2547
+ }
2548
+ function refused$1(enums, change, reason, why) {
2549
+ const { statement } = change;
2550
+ if (statement === void 0) return {
2551
+ kind: "fieldRemovalRefused",
2552
+ summary: `Cannot remove ${enumValueText(enums.fromContract, change.enumValue)}: ${reason}`,
2553
+ why
2554
+ };
2555
+ return {
2556
+ kind: "statementRefused",
2557
+ summary: `Cannot rename ${enumValueText(enums.fromContract, statement.from)} to "${enumValueStatementText(enums.contract, statement.to)}": ${reason}`,
2558
+ why,
2559
+ refusedStatement: statement
2560
+ };
2561
+ }
2562
+ function unsupportedValueRefusal(enums, change, stored) {
2563
+ return refused$1(enums, change, `its stored value ${JSON.stringify(stored)} is not a string, a number or a boolean`, `The plan matches stored enum values by equality, and MongoDB's equality on an object depends on the order of its keys and on a list matches lists that contain it. Rewrite the documents that hold ${enumValueText(enums.fromContract, change.enumValue)} yourself, in a dataTransform operation of the migration.ts that migration plan writes or in mongosh, then run the command again.`);
2564
+ }
2565
+ function stillStoredRefusal(enums, statement, stored, holder) {
2566
+ const holderText = enumValueStatementText(enums.contract, {
2567
+ ...statement.to,
2568
+ member: holder
2569
+ });
2570
+ return refused$1(enums, {
2571
+ enumValue: statement.from,
2572
+ statement
2573
+ }, `the destination enum still stores ${JSON.stringify(stored)}, as enum value "${holderText}"`, `A rename changes the stored value in every document that holds it, so the documents that hold ${JSON.stringify(stored)} for "${holderText}" would change too. Leave out this statement.`);
2574
+ }
2575
+ /**
2576
+ * For a member that keeps its name and takes the stored value of an origin member the destination no longer has: the plan cannot tell whether that member is gone, so it says how to name the member it became. The member it suggests is the destination's only new member that no rename names, as the question picks it.
2577
+ */
2578
+ function takenValueAdvice(enums, enumValue, to, holder, renamedTo) {
2579
+ const { originEnum, destinationEnum } = enums;
2580
+ if (holder === void 0 || destinationEnum.members.some(({ name }) => name === holder)) return;
2581
+ const added = destinationEnum.members.filter(({ name }) => !renamedTo.includes(name) && !originEnum.members.some((member) => member.name === name));
2582
+ const [only] = added;
2583
+ if (only === void 0) return void 0;
2584
+ const holderText = enumValueStatementText(enums.fromContract, {
2585
+ ...enumValue,
2586
+ member: holder
2587
+ });
2588
+ const lead = `Its new stored value ${JSON.stringify(to)} was the stored value of enum value "${holderText}", which the enum no longer has.`;
2589
+ const ask = "and the next question says how to keep the data of both.";
2590
+ const memberText = (member) => enumValueStatementText(enums.contract, {
2591
+ ...enumValue,
2592
+ member
2593
+ });
2594
+ const rename = (member) => statementFlag("rename", `${holderText}:${memberText(member)}`);
2595
+ if (added.length > 1) return `${lead} If "${holderText}" is now another member, add ${rename("<new member>")}, ${ask}`;
2596
+ return `${lead} If "${holderText}" is now "${memberText(only.name)}", add ${rename(only.name)}, ${ask}`;
2597
+ }
2598
+ function mergingRefusal(enums, statement, from, to, holder) {
2599
+ const holderText = enumValueStatementText(enums.fromContract, {
2600
+ ...statement.from,
2601
+ member: holder
2602
+ });
2603
+ return refused$1(enums, {
2604
+ enumValue: statement.from,
2605
+ statement
2606
+ }, `the origin enum already stores ${JSON.stringify(to)}, as enum value "${holderText}"`, `A rename changes ${JSON.stringify(from)} to ${JSON.stringify(to)} in every document that holds it, which merges its data with the data of "${holderText}". Leave out this statement. To merge them on purpose, plan the change with migration plan and write replaceStoredValue(...) in the migration.ts it writes.`);
2607
+ }
2608
+ function unlistedRefusal$1(enums, change, unlisted) {
2609
+ const where = `field "${unlisted.field}" of collection "${unlisted.collection}"`;
2610
+ return unlisted.reach.kind === "containsItself" ? refused$1(enums, change, `${where} holds it inside "${unlisted.reach.type}", which contains itself, so the documents can hold it at any depth`, `The plan rewrites a stored value at every path from a field to it, and a type that contains itself has no last path. Rewrite ${where} yourself, in a dataTransform operation of the migration.ts that migration plan writes or in mongosh.`) : refused$1(enums, change, `${where} holds a ${unlisted.reach.kind} of value objects`, `The plan rewrites a stored value only in a field that holds it, a value object, a list of them, or an embedded model. Rewrite ${where} yourself, in a dataTransform operation of the migration.ts that migration plan writes or in mongosh.`);
2611
+ }
2612
+ /**
2613
+ * The rewrites of stored enum values a plan makes, as SQL plans them. Enums are compared by the
2614
+ * stored value of each member (see `valueChanges`). A changed value is rewritten at every place the
2615
+ * destination's documents hold the enum: a rename statement replaces it, and any other change
2616
+ * removes it, which the plan asks about, with the replacements that would keep its data. Rename
2617
+ * statements that merge two values, or leave a value another member stores, are refused; the
2618
+ * planner refuses swaps and chains for the whole plan (see `overlapRefusal`). A rename in a
2619
+ * collection whose control policy is not `managed` is refused, and a removal there is left out. With
2620
+ * no origin contract, nothing is known to have changed.
2621
+ */
2622
+ function planEnumValueRewrites(input) {
2623
+ const { fromContract, contract } = input;
2624
+ if (fromContract === null) return ok({
2625
+ replacements: [],
2626
+ removals: []
2627
+ });
2628
+ const valueRenames = input.statements.filter((statement) => statement.entity === "enumValue");
2629
+ const replacements = [];
2630
+ const removals = [];
2631
+ const conflicts = [];
2632
+ for (const [namespaceId, namespace] of Object.entries(fromContract.domain?.namespaces ?? {})) for (const [enumName, originEnum] of Object.entries(namespace.enum ?? {})) {
2633
+ const destinationEnum = contract.domain?.namespaces[namespaceId]?.enum?.[enumName];
2634
+ if (destinationEnum === void 0) continue;
2635
+ const found = planEnum({
2636
+ fromContract,
2637
+ contract,
2638
+ originEnum,
2639
+ destinationEnum
2640
+ }, namespaceId, enumName, valueRenames, input);
2641
+ if (!found.ok) {
2642
+ conflicts.push(...found.failure);
2643
+ continue;
2644
+ }
2645
+ replacements.push(...found.value.replacements);
2646
+ removals.push(...found.value.removals);
2647
+ }
2648
+ return conflicts.length > 0 ? notOk(conflicts) : ok({
2649
+ replacements,
2650
+ removals
2651
+ });
2652
+ }
2653
+ function planEnum(enums, namespaceId, enumName, valueRenames, input) {
2654
+ const { originEnum, destinationEnum, contract } = enums;
2655
+ const raw = valueChanges(enums, (member) => ({
2656
+ namespaceId: asNamespaceId(namespaceId),
2657
+ enum: enumName,
2658
+ member
2659
+ }), (member) => valueRenames.find((rename) => rename.from.namespaceId === namespaceId && rename.from.enum === enumName && rename.from.member === member));
2660
+ const checked = [];
2661
+ for (const change of raw) {
2662
+ if (!isStoredValue(change.from)) return notOk([unsupportedValueRefusal(enums, change, change.from)]);
2663
+ if (change.to !== void 0 && !isStoredValue(change.to)) return notOk([unsupportedValueRefusal(enums, change, change.to)]);
2664
+ checked.push({
2665
+ ...change,
2666
+ from: change.from,
2667
+ to: change.to
2668
+ });
2669
+ }
2670
+ const changedFrom = (value) => checked.some(({ from }) => sameValue(from, value));
2671
+ const changedTo = (value) => checked.some(({ to }) => to !== void 0 && sameValue(to, value));
2672
+ const merging = (to) => originEnum.members.find(({ value }) => sameValue(value, to) && !changedFrom(value));
2673
+ for (const { statement, from, to } of checked) {
2674
+ if (statement === void 0 || to === void 0) continue;
2675
+ const holder = destinationEnum.members.find(({ value }) => sameValue(value, from));
2676
+ if (holder !== void 0 && !changedTo(from)) return notOk([stillStoredRefusal(enums, statement, from, holder.name)]);
2677
+ const merged = merging(to);
2678
+ if (merged !== void 0) return notOk([mergingRefusal(enums, statement, from, to, merged.name)]);
2679
+ }
2680
+ if (checked.length === 0) return ok({
2681
+ replacements: [],
2682
+ removals: []
2683
+ });
2684
+ const overlapping = overlappingChanges(checked);
2685
+ const advice = enumTwoMigrationsAdvice(enums);
2686
+ const places = placesOfEnum(contract, namespaceId, enumName);
2687
+ if (!places.ok) {
2688
+ const [first] = checked;
2689
+ return first === void 0 ? ok({
2690
+ replacements: [],
2691
+ removals: []
2692
+ }) : notOk([unlistedRefusal$1(enums, checked.find((change) => change.statement !== void 0) ?? first, places.failure)]);
2693
+ }
2694
+ const live = places.value.filter(({ place }) => input.schema.collection(place.collection) !== void 0);
2695
+ const conflicts = placeRefusals(enums, checked, live, input);
2696
+ if (conflicts.length > 0) return notOk(conflicts);
2697
+ const bsonTypes = bsonTypesOfCodec(destinationEnum.codecId, input.lookups) ?? [];
2698
+ const replacements = [];
2699
+ const removals = [];
2700
+ for (const change of checked) {
2701
+ const keepsDataBy = change.statement !== void 0 || change.to === void 0 || merging(change.to) !== void 0 ? void 0 : overlapping.includes(change) ? "advice" : "edit";
2702
+ const removalAdvice = keepsDataBy === "advice" ? twoMigrationsAdvice(enums, overlapping) : change.statement === void 0 && change.to !== void 0 ? takenValueAdvice(enums, change.enumValue, change.to, merging(change.to)?.name, valueRenames.flatMap((statement) => statement.to.enum === enumName && statement.to.namespaceId === namespaceId ? [statement.to.member] : [])) : void 0;
2703
+ for (const enumPlace of live) {
2704
+ if (collectionControlPolicy(contract, enumPlace.place.collection) !== "managed") continue;
2705
+ const at = {
2706
+ collection: enumPlace.place.collection,
2707
+ field: enumPlace.place.field,
2708
+ ...enumPlace.path === void 0 ? {} : { path: enumPlace.path },
2709
+ ...enumPlace.place.variant === void 0 ? {} : { filter: variantFilter(enumPlace.place.variant) }
2710
+ };
2711
+ const replacementCall = (to) => new ReplaceStoredValueCall({
2712
+ ...at,
2713
+ from: change.from,
2714
+ to: valueExpression(to, bsonTypes)
2715
+ });
2716
+ if (change.statement !== void 0 && change.to !== void 0) {
2717
+ replacements.push({
2718
+ call: replacementCall(change.to),
2719
+ enumValue: change.enumValue,
2720
+ statement: change.statement,
2721
+ keepDataEdit: void 0,
2722
+ keepDataAdvice: void 0,
2723
+ twoMigrationsAdvice: advice,
2724
+ writtenValue: change.to,
2725
+ variant: enumPlace.place.variant
2726
+ });
2727
+ continue;
2728
+ }
2729
+ const call = new RemoveStoredValueCall({
2730
+ ...at,
2731
+ value: change.from,
2732
+ ...enumPlace.required ? { required: true } : {},
2733
+ enumValue: enumValueCoordinateJson(change.enumValue)
2734
+ }, { importsReplacement: keepsDataBy === "edit" || enumPlace.required });
2735
+ removals.push({
2736
+ call,
2737
+ enumValue: change.enumValue,
2738
+ statement: void 0,
2739
+ keepDataEdit: keepsDataBy === "edit" && change.to !== void 0 ? {
2740
+ remove: callInSentence(call),
2741
+ replaceWith: callInSentence(replacementCall(change.to))
2742
+ } : void 0,
2743
+ keepDataAdvice: removalAdvice,
2744
+ twoMigrationsAdvice: advice,
2745
+ variant: enumPlace.place.variant
2746
+ });
2747
+ }
2748
+ }
2749
+ return ok({
2750
+ replacements,
2751
+ removals
2752
+ });
2753
+ }
2754
+ /** The refusals of the places a change rewrites: keys MongoDB cannot rewrite, unmanaged collections for a rename, time-series and shared collections. */
2755
+ function placeRefusals(enums, changes, places, input) {
2756
+ const conflicts = [];
2757
+ const seen = /* @__PURE__ */ new Set();
2758
+ const push = (conflict) => {
2759
+ const key = JSON.stringify([conflict.kind, conflict.summary]);
2760
+ if (seen.has(key)) return;
2761
+ seen.add(key);
2762
+ conflicts.push(conflict);
2763
+ };
2764
+ for (const change of changes) {
2765
+ const kind = change.statement === void 0 ? "removeStoredValue" : "replaceStoredValue";
2766
+ for (const { place, keys } of places) {
2767
+ const policy = collectionControlPolicy(input.contract, place.collection);
2768
+ if (policy !== "managed") {
2769
+ if (change.statement !== void 0) push(refused$1(enums, change, `collection "${place.collection}" has control policy "${policy}"`, "Statements rename only collections, and fields of collections, whose control policy is \"managed\". Make the change in the database yourself and leave out this statement."));
2770
+ continue;
2771
+ }
2772
+ const unrewritable = keys.find((key) => unrewritableKeyReason(key) !== void 0);
2773
+ if (unrewritable !== void 0) {
2774
+ push(refused$1(enums, change, `the stored name "${unrewritable}" cannot be rewritten`, `${unrewritableKeyReason(unrewritable)}, so the plan cannot rewrite the value there. Rewrite it in the database yourself, or ${change.statement === void 0 ? "keep the enum value in the contract" : "leave out this statement"}.`));
2775
+ continue;
2776
+ }
2777
+ for (const conflict of rewriteRefusals({
2778
+ kind,
2779
+ collection: place.collection,
2780
+ field: place.field,
2781
+ variant: place.variant,
2782
+ model: place.model,
2783
+ storedKey: {
2784
+ key: enumValueText(enums.fromContract, change.enumValue),
2785
+ together: []
2786
+ }
2787
+ }, input.contract, rewriteCollectionOf(input.schema, place.collection))) push(change.statement === void 0 ? conflict : {
2788
+ ...conflict,
2789
+ refusedStatement: change.statement
2790
+ });
2791
+ }
2792
+ }
2793
+ return conflicts;
2794
+ }
2795
+ /**
2796
+ * The rename that gives a destination base's discriminator the values of its origin's, when the
2797
+ * origin's discriminator field is gone from the destination and no rename moves it elsewhere.
2798
+ */
2799
+ function discriminatorRenameOf(origin, destination, contracts) {
2800
+ const from = origin === void 0 ? void 0 : modelAt(contracts.fromContract, origin);
2801
+ const to = modelAt(contracts.contract, destination);
2802
+ const oldField = from?.discriminator?.field;
2803
+ const newField = to?.discriminator?.field;
2804
+ if (origin === void 0 || to === void 0 || oldField === void 0 || newField === void 0) return void 0;
2805
+ const movedElsewhere = contracts.statements.some((statement) => statement.kind === "rename" && statement.entity === "field" && statement.from.namespaceId === origin.namespaceId && statement.from.model === origin.model && statement.from.field === oldField);
2806
+ if (oldField === newField || Object.hasOwn(to.fields, oldField) || movedElsewhere) return void 0;
2807
+ const text = fieldStatementText(contracts.contract, {
2808
+ ...destination,
2809
+ field: oldField
2810
+ });
2811
+ const baseRenamed = origin.namespaceId !== destination.namespaceId || origin.model !== destination.model;
2812
+ const baseRenameStated = contracts.statements.some((statement) => statement.kind === "rename" && statement.entity === "model" && statement.from.namespaceId === origin.namespaceId && statement.from.model === origin.model);
2813
+ const fieldRename = statementFlag("rename", `${text}:${fieldStatementText(contracts.contract, {
2814
+ ...destination,
2815
+ field: newField
2816
+ })}`);
2817
+ return {
2818
+ from: fieldStatementText(contracts.fromContract, {
2819
+ ...origin,
2820
+ field: oldField
2821
+ }),
2822
+ flags: baseRenamed && !baseRenameStated ? `${statementFlag("rename", `${modelStatementText(contracts.fromContract, origin)}:${modelStatementText(contracts.contract, destination)}`)} ${fieldRename}` : fieldRename
2823
+ };
2824
+ }
1201
2825
  const EMPTY_LIST = {
1202
2826
  expression: [],
1203
2827
  text: "[]"
1204
2828
  };
2829
+ /** The temporary value of a field's type: an empty list for any list, and for a scalar the one its codec declares. A value object, an enum or a dictionary has none. */
1205
2830
  function temporaryValueOf(field, lookups) {
1206
2831
  if (field.many !== void 0 && field.many !== false) return EMPTY_LIST;
1207
2832
  if (field.type.kind !== "scalar" || field.valueSet !== void 0 || field.dict === true) return;
@@ -1210,7 +2835,7 @@ function temporaryValueOf(field, lookups) {
1210
2835
  function backfilled(statements, field) {
1211
2836
  return statements.some((statement) => statement.kind === "backfill" && statement.entity === "field" && statement.field.namespaceId === field.namespaceId && statement.field.model === field.model && statement.field.field === field.field);
1212
2837
  }
1213
- /** Whether the origin stored the field: in the origin model, or, without an origin contract, in the origin validator. */
2838
+ /** Whether the origin stored the field: in the origin model, unless a rename moves the origin's field of that name away, or, without an origin contract, in the origin validator. */
1214
2839
  function inOrigin(input, field, collection, storedName) {
1215
2840
  if (input.match === void 0) {
1216
2841
  const validator = input.schema.collection(collection)?.validator;
@@ -1218,22 +2843,54 @@ function inOrigin(input, field, collection, storedName) {
1218
2843
  }
1219
2844
  const origin = originModelOf(field, input.match);
1220
2845
  const model = origin === void 0 ? void 0 : modelAt(input.match.fromContract, origin);
1221
- return origin === void 0 || model === void 0 || Object.hasOwn(model.fields, originFieldOf(field, input.match.statements)) || storedFieldsOf(input.match.fromContract, origin).has(storedName);
2846
+ if (origin === void 0 || model === void 0) return true;
2847
+ const originField = originFieldOf(field, input.match.statements);
2848
+ return !(destinationFieldOf({
2849
+ ...origin,
2850
+ field: originField
2851
+ }, input.match.statements) !== field.field) && (Object.hasOwn(model.fields, originField) || storedFieldsKeptOf(input.match, origin).has(storedName));
1222
2852
  }
1223
2853
  /** Whether a unique index covers the field, so that one value in every document would break it. */
1224
- function underUniqueIndex(destination, collection, storedName) {
2854
+ function underUniqueIndex$1(destination, collection, storedName) {
1225
2855
  return (destination.collection(collection)?.indexes ?? []).some((index) => index.unique && index.keys.some((key) => key.field === storedName));
1226
2856
  }
1227
2857
  /**
2858
+ * The refusal of a new required discriminator whose values the documents hold in the origin's
2859
+ * discriminator, which the plan removes: a temporary value names no variant, and only a rename of
2860
+ * the old discriminator keeps each document's variant. `undefined` when the destination has no such
2861
+ * rename to offer, such as for a base's first discriminator; the validator change then checks the
2862
+ * documents.
2863
+ */
2864
+ function newDiscriminatorRefusal(input, model, field, collection) {
2865
+ const rename = input.match === void 0 ? void 0 : discriminatorRenameOf(originModelOf(model, input.match), model, {
2866
+ fromContract: input.match.fromContract,
2867
+ contract: input.contract,
2868
+ statements: input.statements
2869
+ });
2870
+ if (rename === void 0) return void 0;
2871
+ return {
2872
+ kind: "unsupportedOperation",
2873
+ summary: `Cannot plan the migration: new discriminator field "${fieldStatementText(input.contract, {
2874
+ ...model,
2875
+ field
2876
+ })}" has no value in the documents of collection ${collection}`,
2877
+ why: `A document's discriminator value says which variant it is, and no temporary value names a variant, so the plan does not fill the field. The documents hold their variant in "${rename.from}", which the plan removes: add ${rename.flags} to keep it, and run the command again.`
2878
+ };
2879
+ }
2880
+ /**
1228
2881
  * Each required field of the destination that is new on a collection the plan keeps, with the
1229
2882
  * temporary value its type declares, unless a `--backfill` statement names it. A field whose type
1230
- * declares none, or that a unique index covers, is left to the validator change's document check.
2883
+ * declares none, or that a unique index covers, is left to the validator change's document check,
2884
+ * and so is a new discriminator, since a temporary value would leave every document matched by no
2885
+ * variant; when the documents hold their variants in an old discriminator the plan removes, the
2886
+ * plan is refused instead, offering its rename.
1231
2887
  */
1232
2888
  function planFieldFills(input) {
1233
- return modelCoordinates(input.contract).flatMap((model) => {
2889
+ const refusals = [];
2890
+ const fills = modelCoordinates(input.contract).flatMap((model) => {
1234
2891
  const definition = modelAt(input.contract, model);
1235
2892
  const collection = collectionOfModel(input.contract.domain, model);
1236
- if (definition === void 0 || collection === void 0 || embeddedModelReason(input.contract, model) !== void 0) return [];
2893
+ if (definition === void 0 || collection === void 0 || embeddingModelOf(input.contract.domain, model) !== void 0) return [];
1237
2894
  if (input.schema.collection(collection) === void 0) return [];
1238
2895
  if (collectionControlPolicy(input.contract, collection) !== "managed") return [];
1239
2896
  return Object.entries(definition.fields).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).flatMap(([field, contractField]) => {
@@ -1245,26 +2902,322 @@ function planFieldFills(input) {
1245
2902
  if (contractField.nullable || unrewritableFieldNameReason(storedName) !== void 0) return [];
1246
2903
  if (backfilled(input.statements, coordinate)) return [];
1247
2904
  if (inOrigin(input, coordinate, collection, storedName)) return [];
1248
- if (underUniqueIndex(input.destination, collection, storedName)) return [];
2905
+ if (definition.discriminator?.field === field) {
2906
+ const refusal = newDiscriminatorRefusal(input, model, field, collection);
2907
+ if (refusal !== void 0) refusals.push(refusal);
2908
+ return [];
2909
+ }
2910
+ if (underUniqueIndex$1(input.destination, collection, storedName)) return [];
1249
2911
  const value = temporaryValueOf(contractField, input.lookups);
1250
2912
  if (value === void 0) return [];
1251
- const filter = variantFilterOf(input.contract.domain, model);
2913
+ const variant = selectedVariantOf(input.contract.domain, model);
1252
2914
  return [{
1253
2915
  call: new FillFieldCall({
1254
2916
  collection,
1255
2917
  field: storedName,
1256
2918
  value: value.expression,
1257
- ...filter === void 0 ? {} : { filter }
2919
+ ...variant === void 0 ? {} : { filter: variantFilter(variant) }
1258
2920
  }),
1259
2921
  subject: {
1260
2922
  kind: "field",
1261
2923
  ...coordinate
1262
2924
  },
1263
- text: value.text
2925
+ text: value.text,
2926
+ variant
2927
+ }];
2928
+ });
2929
+ });
2930
+ return refusals.length > 0 ? notOk(refusals) : ok(fills);
2931
+ }
2932
+ function byName([a], [b]) {
2933
+ return a < b ? -1 : a > b ? 1 : 0;
2934
+ }
2935
+ function sameValueObjectField(a, b) {
2936
+ return a.namespaceId === b.namespaceId && a.valueObject === b.valueObject && a.field === b.field;
2937
+ }
2938
+ function sameField$1(a, b) {
2939
+ return a.namespaceId === b.namespaceId && a.model === b.model && a.field === b.field;
2940
+ }
2941
+ /**
2942
+ * The name the origin gives a destination field: the field a rename statement renames to it, else
2943
+ * the field of the same name, unless a rename statement moves that field away, which makes the
2944
+ * destination's field a new one.
2945
+ */
2946
+ function originNameOf(name, renamedTo, movedAway) {
2947
+ if (renamedTo !== void 0) return renamedTo.from.field;
2948
+ return movedAway ? void 0 : name;
2949
+ }
2950
+ /** Whether a field is new to the destination, or was optional in the origin. */
2951
+ function newlyRequired(field, origin) {
2952
+ return !field.nullable && (origin === void 0 || origin.nullable);
2953
+ }
2954
+ function valueObjectFields(fromContract, contract, statements) {
2955
+ return valueObjectsWithCoordinates(contract.storage).flatMap(({ coordinate, valueObject }) => {
2956
+ const origin = valueObjectAt(fromContract.storage, coordinate);
2957
+ if (origin === void 0) return [];
2958
+ return Object.entries(valueObject.fields).sort(byName).flatMap(([name, field]) => {
2959
+ const subject = {
2960
+ kind: "valueObjectField",
2961
+ namespaceId: asNamespaceId(coordinate.namespaceId),
2962
+ valueObject: coordinate.entityName,
2963
+ field: name
2964
+ };
2965
+ const renames = statements.filter((statement) => statement.kind === "rename" && statement.entity === "valueObjectField");
2966
+ const originName = originNameOf(name, renames.find((rename) => sameValueObjectField(rename.to, subject)), renames.some((rename) => sameValueObjectField(rename.from, subject)));
2967
+ const originField = originName !== void 0 && Object.hasOwn(origin.fields, originName) ? origin.fields[originName] : void 0;
2968
+ if (!newlyRequired(field, originField)) return [];
2969
+ return [{
2970
+ type: {
2971
+ kind: "valueObject",
2972
+ valueObject: coordinate
2973
+ },
2974
+ subject,
2975
+ key: name,
2976
+ field,
2977
+ isNew: originField === void 0,
2978
+ statement: statements.find((statement) => statement.kind === "backfill" && statement.entity === "valueObjectField" && sameValueObjectField(statement.field, subject))
1264
2979
  }];
1265
2980
  });
1266
2981
  });
1267
2982
  }
2983
+ function embeddedModelFields(fromContract, contract, statements) {
2984
+ return modelCoordinates(contract).flatMap((coordinate) => {
2985
+ if (embeddingModelOf(contract.domain, coordinate) === void 0) return [];
2986
+ if (embeddingModelOf(fromContract.domain, coordinate) === void 0) return [];
2987
+ const origin = modelAt(fromContract, coordinate);
2988
+ const destination = modelAt(contract, coordinate);
2989
+ if (origin === void 0 || destination === void 0) return [];
2990
+ return Object.entries(destination.fields).sort(byName).flatMap(([name, field]) => {
2991
+ const subject = {
2992
+ kind: "field",
2993
+ ...coordinate,
2994
+ field: name
2995
+ };
2996
+ const renames = statements.filter((statement) => statement.kind === "rename" && statement.entity === "field");
2997
+ const originName = originNameOf(name, renames.find((rename) => sameField$1(rename.to, subject)), renames.some((rename) => sameField$1(rename.from, subject)));
2998
+ const originField = originName !== void 0 && Object.hasOwn(origin.fields, originName) ? origin.fields[originName] : void 0;
2999
+ if (!newlyRequired(field, originField)) return [];
3000
+ return [{
3001
+ type: {
3002
+ kind: "embeddedModel",
3003
+ model: coordinate
3004
+ },
3005
+ subject,
3006
+ key: ownStoredNameOf(destination, name),
3007
+ field,
3008
+ isNew: originField === void 0,
3009
+ statement: statements.find((statement) => statement.kind === "backfill" && statement.entity === "field" && sameField$1(statement.field, subject))
3010
+ }];
3011
+ });
3012
+ });
3013
+ }
3014
+ /** The backfill statements of fields inside a field's value: of a value object field, or of a field of an embedded model. */
3015
+ function nestedBackfills(statements, contract) {
3016
+ return statements.filter((statement) => statement.kind === "backfill" && (statement.entity === "valueObjectField" || embeddingModelOf(contract.domain, statement.field) !== void 0));
3017
+ }
3018
+ /** The field a fill is about, as a refusal names it. */
3019
+ function fieldText$1(required, contract) {
3020
+ return required.subject.kind === "valueObjectField" ? `value object field "${valueObjectFieldStatementText(contract, required.subject)}"` : `field "${fieldStatementText(contract, required.subject)}"`;
3021
+ }
3022
+ /** The field as a statement names it, such as `Address.zip`. */
3023
+ function statementText(required, contract) {
3024
+ return required.subject.kind === "valueObjectField" ? valueObjectFieldStatementText(contract, required.subject) : fieldStatementText(contract, required.subject);
3025
+ }
3026
+ function refused(required, contracts, reason, why) {
3027
+ if (required.statement === void 0) return {
3028
+ kind: "fieldFillRefused",
3029
+ summary: `Cannot fill ${fieldText$1(required, contracts.contract)}: ${reason}`,
3030
+ why: why("make the field optional in the contract")
3031
+ };
3032
+ return {
3033
+ kind: "statementRefused",
3034
+ summary: `Cannot ${describeMigrationStatement(required.statement, contracts.fromContract, contracts.contract)}: ${reason}`,
3035
+ why: why("leave out this statement"),
3036
+ refusedStatement: required.statement
3037
+ };
3038
+ }
3039
+ function placeText(place) {
3040
+ return `field "${place.field}" of collection "${place.collection}"`;
3041
+ }
3042
+ function unlistedRefusal(required, place, contracts) {
3043
+ const byHand = (advice) => `Fill the key in ${placeText(place)} yourself, in a dataTransform operation of the migration.ts that migration plan writes or in mongosh, or ${advice}.`;
3044
+ if (place.reach.kind === "containsItself") return refused(required, contracts, `${placeText(place)} holds it inside "${place.reach.type}", which contains itself, so the documents can hold it at any depth`, (advice) => `The plan fills a key at every path from a field to its objects, and a type that contains itself has no last path. ${byHand(advice)}`);
3045
+ return refused(required, contracts, `${placeText(place)} holds a ${place.reach.kind} of value objects`, (advice) => `The plan fills a key only in a field that holds a value object, a list of them, or an embedded model. ${byHand(advice)}`);
3046
+ }
3047
+ /** The names on the path from a document to the key: the root field, each member the path steps through, and the key. */
3048
+ function namesOnPath(place, key) {
3049
+ return [
3050
+ place.field,
3051
+ ...place.steps.flatMap((step) => step.kind === "member" ? [step.name] : []),
3052
+ key
3053
+ ];
3054
+ }
3055
+ /** Whether a unique index covers the key at a place, so that one value in every object would break it. */
3056
+ function underUniqueIndex(destination, place, key) {
3057
+ const dotted = namesOnPath(place, key).join(".");
3058
+ return (destination.collection(place.collection)?.indexes ?? []).some((index) => index.unique && index.keys.some((indexKey) => indexKey.field === dotted));
3059
+ }
3060
+ /**
3061
+ * The places a required field is filled at: every place the destination's documents hold its value object or embedded model, in a collection the plan keeps, as the destination names them. A temporary value is not written in a collection whose control policy is not `managed`, nor where a stored name on the path cannot be rewritten; a backfill there is refused.
3062
+ */
3063
+ function fillPlaces(required, places, input, contracts) {
3064
+ const planned = /* @__PURE__ */ new Map();
3065
+ const conflicts = [];
3066
+ for (const place of places) {
3067
+ if (input.schema.collection(place.collection) === void 0) continue;
3068
+ const policy = collectionControlPolicy(input.contract, place.collection);
3069
+ if (policy !== "managed") {
3070
+ if (required.statement !== void 0) conflicts.push(refused(required, contracts, `collection "${place.collection}" has control policy "${policy}"`, () => "Statements fill only fields of collections whose control policy is \"managed\". Fill the field in the database yourself and leave out this statement."));
3071
+ continue;
3072
+ }
3073
+ const unrewritable = namesOnPath(place, required.key).find((name) => unrewritableKeyReason(name) !== void 0);
3074
+ if (unrewritable !== void 0) {
3075
+ if (required.statement !== void 0) conflicts.push(refused(required, contracts, `the stored name "${unrewritable}" cannot be rewritten`, (advice) => `${unrewritableKeyReason(unrewritable)}, so the plan cannot fill the key. Fill it in the database yourself, or ${advice}.`));
3076
+ continue;
3077
+ }
3078
+ const path = [...place.steps, {
3079
+ kind: "member",
3080
+ name: required.key
3081
+ }];
3082
+ const key = JSON.stringify([
3083
+ place.collection,
3084
+ place.field,
3085
+ jsonPathText(path),
3086
+ place.variant ?? null
3087
+ ]);
3088
+ const known = planned.get(key);
3089
+ if (known !== void 0) {
3090
+ known.models.push(place.model.model);
3091
+ continue;
3092
+ }
3093
+ planned.set(key, {
3094
+ required,
3095
+ place,
3096
+ path,
3097
+ models: [place.model.model]
3098
+ });
3099
+ }
3100
+ return conflicts.length > 0 ? notOk(conflicts) : ok([...planned.values()]);
3101
+ }
3102
+ function plannedRewriteOf(fill, contract) {
3103
+ return {
3104
+ kind: "fillStoredKey",
3105
+ collection: fill.place.collection,
3106
+ field: fill.place.field,
3107
+ variant: fill.place.variant,
3108
+ model: fill.place.model,
3109
+ storedKey: {
3110
+ key: fieldText$1(fill.required, contract),
3111
+ together: fill.models
3112
+ }
3113
+ };
3114
+ }
3115
+ /**
3116
+ * The fills of keys inside fields' values a plan makes. A field of a value object or an embedded model that both contracts have is filled when the destination makes it required and the origin lacks it or makes it optional. A backfill statement fills it with a value the user writes in `migration.ts`. Without one, a new field whose type has a temporary value gets it, unless a unique index covers it at one of its places; any other, and a field that was optional, is left to the validator change's document check, as at the top level. Each fill acts at every place the destination's documents hold the type, nested or in lists, as the destination names them, and has the refusals of every rewrite. With no origin contract, nothing is known to have changed.
3117
+ */
3118
+ function planStoredKeyFills(input) {
3119
+ const { fromContract, contract } = input;
3120
+ const required = fromContract === null ? [] : [...valueObjectFields(fromContract, contract, input.statements), ...embeddedModelFields(fromContract, contract, input.statements)];
3121
+ const conflicts = nestedBackfills(input.statements, contract).filter((statement) => !required.some((each) => each.statement === statement)).map((statement) => ({
3122
+ kind: "statementRefused",
3123
+ summary: `Cannot ${describeMigrationStatement(statement, fromContract ?? contract, contract)}: no existing object is known to lack it, since the origin contract has no such field that is optional, nor this value object or embedded model without it`,
3124
+ why: "A --backfill statement fills a required field that a value object or an embedded model gains, or that was optional. Leave out this statement.",
3125
+ refusedStatement: statement
3126
+ }));
3127
+ if (fromContract === null) return conflicts.length > 0 ? notOk(conflicts) : ok([]);
3128
+ const contracts = {
3129
+ fromContract,
3130
+ contract
3131
+ };
3132
+ const placesOf = storedPlacesOf(contract);
3133
+ const fills = [];
3134
+ for (const each of required) {
3135
+ const temporary = each.statement === void 0 && each.isNew ? temporaryValueOf(each.field, input.lookups) : void 0;
3136
+ if (each.statement === void 0 && temporary === void 0) continue;
3137
+ const places = placesOf(each.type);
3138
+ if (!places.ok) {
3139
+ conflicts.push(unlistedRefusal(each, places.failure, contracts));
3140
+ continue;
3141
+ }
3142
+ if (temporary !== void 0 && places.value.some((place) => underUniqueIndex(input.destination, place, each.key))) continue;
3143
+ const planned = fillPlaces(each, places.value, input, contracts);
3144
+ if (!planned.ok) {
3145
+ conflicts.push(...planned.failure);
3146
+ continue;
3147
+ }
3148
+ for (const fill of planned.value) {
3149
+ conflicts.push(...rewriteRefusals(plannedRewriteOf(fill, contract), contract, rewriteCollectionOf(input.schema, fill.place.collection)));
3150
+ fills.push({
3151
+ call: new FillStoredKeyCall({
3152
+ collection: fill.place.collection,
3153
+ field: fill.place.field,
3154
+ path: jsonPathText(fill.path),
3155
+ value: temporary?.expression ?? new UnfilledValue(`${statementText(each, contract)}:value`),
3156
+ ...fill.place.variant === void 0 ? {} : { filter: variantFilter(fill.place.variant) }
3157
+ }),
3158
+ subject: each.subject,
3159
+ text: temporary?.text,
3160
+ statement: each.statement,
3161
+ variant: fill.place.variant
3162
+ });
3163
+ }
3164
+ }
3165
+ return conflicts.length > 0 ? notOk(conflicts) : ok(fills);
3166
+ }
3167
+ function storedTypesOf(contract) {
3168
+ const valueObjects = valueObjectsWithCoordinates(contract.storage).map(({ coordinate, valueObject }) => ({
3169
+ type: {
3170
+ kind: "valueObject",
3171
+ valueObject: coordinate
3172
+ },
3173
+ storedNames: valueObjectStoredNamesOf(valueObject),
3174
+ subjectOf: (field) => ({
3175
+ kind: "valueObjectField",
3176
+ namespaceId: asNamespaceId(coordinate.namespaceId),
3177
+ valueObject: coordinate.entityName,
3178
+ field
3179
+ })
3180
+ }));
3181
+ const embedded = modelCoordinates(contract).flatMap((coordinate) => {
3182
+ const model = modelAt(contract, coordinate);
3183
+ return model === void 0 || embeddingModelOf(contract.domain, coordinate) === void 0 ? [] : [{
3184
+ type: {
3185
+ kind: "embeddedModel",
3186
+ model: coordinate
3187
+ },
3188
+ storedNames: ownStoredNamesOf(model),
3189
+ subjectOf: (field) => ({
3190
+ kind: "field",
3191
+ ...coordinate,
3192
+ field
3193
+ })
3194
+ }];
3195
+ });
3196
+ return [...valueObjects, ...embedded];
3197
+ }
3198
+ /**
3199
+ * The field of a value object or an embedded model that the destination's documents store at `path` inside root field `field` of `collection`, where `path` is the JSONPath from the field's value to the key; `undefined` when no listed place holds one there.
3200
+ */
3201
+ function storedKeyFieldAt(contract, at) {
3202
+ const placesOf = storedPlacesOf(contract);
3203
+ for (const { type, storedNames, subjectOf } of storedTypesOf(contract)) {
3204
+ const places = placesOf(type);
3205
+ if (!places.ok) continue;
3206
+ for (const [name, storedName] of Object.entries(storedNames)) {
3207
+ const place = places.value.find((each) => each.collection === at.collection && each.field === at.field && jsonPathText([...each.steps, {
3208
+ kind: "member",
3209
+ name: storedName
3210
+ }]) === at.path);
3211
+ if (place === void 0) continue;
3212
+ const subject = subjectOf(name);
3213
+ return {
3214
+ subject,
3215
+ text: subject.kind === "valueObjectField" ? valueObjectFieldStatementText(contract, subject) : fieldStatementText(contract, subject),
3216
+ place
3217
+ };
3218
+ }
3219
+ }
3220
+ }
1268
3221
  function withoutNull(types) {
1269
3222
  return types.filter((type) => type !== "null");
1270
3223
  }
@@ -1276,7 +3229,7 @@ function fieldShapeOf(field, lookups) {
1276
3229
  list: field.many !== void 0 && field.many !== false
1277
3230
  };
1278
3231
  }
1279
- function isRecord$1(value) {
3232
+ function isRecord$2(value) {
1280
3233
  return typeof value === "object" && value !== null && !Array.isArray(value);
1281
3234
  }
1282
3235
  function bsonTypesOf(schema) {
@@ -1291,10 +3244,10 @@ function isObjectSchema(schema) {
1291
3244
  }
1292
3245
  /** The shape a validator's property schema gives a field; `undefined` for an object, whose fields are not compared here. */
1293
3246
  function propertyShapeOf(schema) {
1294
- if (!isRecord$1(schema) || isObjectSchema(schema)) return void 0;
3247
+ if (!isRecord$2(schema) || isObjectSchema(schema)) return void 0;
1295
3248
  const types = bsonTypesOf(schema);
1296
3249
  const items = schema["items"];
1297
- if (types.length === 1 && types[0] === "array" && isRecord$1(items)) return isObjectSchema(items) ? void 0 : {
3250
+ if (types.length === 1 && types[0] === "array" && isRecord$2(items)) return isObjectSchema(items) ? void 0 : {
1298
3251
  types: bsonTypesOf(items),
1299
3252
  list: true
1300
3253
  };
@@ -1333,7 +3286,7 @@ function castFieldCallOf(change) {
1333
3286
  collection: change.collection,
1334
3287
  field: change.field,
1335
3288
  ...change.cast,
1336
- ...change.filter === void 0 ? {} : { filter: change.filter }
3289
+ ...change.variant === void 0 ? {} : { filter: variantFilter(change.variant) }
1337
3290
  });
1338
3291
  }
1339
3292
  /**
@@ -1375,7 +3328,7 @@ function planFieldTypeChanges(input, lookups) {
1375
3328
  field: storedName,
1376
3329
  from,
1377
3330
  to,
1378
- filter: variantFilterOf(input.contract.domain, destination)
3331
+ variant: selectedVariantOf(input.contract.domain, destination)
1379
3332
  })];
1380
3333
  });
1381
3334
  });
@@ -1413,11 +3366,11 @@ function planValidatorTypeChanges(input) {
1413
3366
  };
1414
3367
  const planned = /* @__PURE__ */ new Map();
1415
3368
  for (const { shape, field, from, to } of changes) {
1416
- const filter = shape.discriminator === void 0 || sameEverywhere(field, shape.rootIndex) || shape.discriminator === void 0 ? void 0 : { [shape.discriminator.field]: shape.discriminator.value };
3369
+ const variant = shape.discriminator === void 0 || sameEverywhere(field, shape.rootIndex) ? void 0 : shape.discriminator;
1417
3370
  const key = JSON.stringify([
1418
3371
  shape.rootIndex,
1419
3372
  field,
1420
- filter ?? null
3373
+ variant ?? null
1421
3374
  ]);
1422
3375
  if (planned.has(key)) continue;
1423
3376
  planned.set(key, typeChange({
@@ -1430,7 +3383,7 @@ function planValidatorTypeChanges(input) {
1430
3383
  field,
1431
3384
  from,
1432
3385
  to,
1433
- filter
3386
+ variant
1434
3387
  }));
1435
3388
  }
1436
3389
  return [...planned.values()].sort((a, b) => a.field < b.field ? -1 : a.field > b.field ? 1 : 0);
@@ -1469,17 +3422,220 @@ function noCastRefusals(changes, contract) {
1469
3422
  function sameField(a, b) {
1470
3423
  return a.namespaceId === b.namespaceId && a.model === b.model && a.field === b.field;
1471
3424
  }
1472
- /** The type change a convert statement names, if it names one. */
1473
- function typeChangeConverted(statement, changes) {
1474
- return changes.find((change) => change.destination !== void 0 && sameField(change.destination, statement.field));
3425
+ /** The type change a convert statement names, if it names one. */
3426
+ function typeChangeConverted(statement, changes) {
3427
+ return changes.find((change) => change.destination !== void 0 && sameField(change.destination, statement.field));
3428
+ }
3429
+ /** The codecs and data types the composed components register, an extension's among them. */
3430
+ function mongoTypeLookupsOf(frameworkComponents) {
3431
+ return {
3432
+ codecLookup: extractCodecLookup(frameworkComponents),
3433
+ dataTypeLookup: assembleDataTypes(frameworkComponents).lookup
3434
+ };
3435
+ }
3436
+ function sameModel(a, b) {
3437
+ return a.namespaceId === b.namespaceId && a.model === b.model;
3438
+ }
3439
+ function statementOf(origin, destination, statements) {
3440
+ return statements.find((statement) => statement.entity === "model" && (statement.kind === "rename" && sameModel(statement.from, origin) || statement.kind === "convert" && sameModel(statement.model, destination)));
3441
+ }
3442
+ /** The model of the destination a model of the origin is, as the statements and field changes find it: a variant renamed without a statement that keeps its collection and value is the same documents. */
3443
+ function destinationOf(origin, fromContract, input) {
3444
+ return destinationModelOf(origin, {
3445
+ statements: input.statements.filter((statement) => statement.kind === "rename"),
3446
+ fromContract,
3447
+ contract: input.contract,
3448
+ collectionAfterRenames: input.collectionAfterRenames
3449
+ });
3450
+ }
3451
+ function baseOf(contract, model) {
3452
+ const base = modelAt(contract, model)?.base;
3453
+ return base === void 0 ? void 0 : {
3454
+ namespaceId: asNamespaceId(base.namespace),
3455
+ model: base.model
3456
+ };
1475
3457
  }
1476
- /** The codecs and data types the composed components register, an extension's among them. */
1477
- function mongoTypeLookupsOf(frameworkComponents) {
3458
+ /** The destination's base of a variant of the origin: the base of the model it is, else the model its origin base is. */
3459
+ function destinationBaseOf(origin, destination, fromContract, input) {
3460
+ const originBase = baseOf(fromContract, origin);
3461
+ return (destination === void 0 ? void 0 : baseOf(input.contract, destination)) ?? (originBase === void 0 ? void 0 : destinationOf(originBase, fromContract, input));
3462
+ }
3463
+ /** The stored name of the discriminator of the destination's base, when it still has one, as SQL finds the column. */
3464
+ function destinationDiscriminator(origin, destination, fromContract, input) {
3465
+ const base = destinationBaseOf(origin, destination, fromContract, input);
3466
+ const model = base === void 0 ? void 0 : modelAt(input.contract, base);
3467
+ const field = model?.discriminator?.field;
3468
+ return model === void 0 || field === void 0 ? void 0 : ownStoredNameOf(model, field);
3469
+ }
3470
+ /** The enum value replacement that rewrites `from` to `to` in the discriminator field itself. */
3471
+ function enumReplacementOf(input, at, from, to) {
3472
+ return input.enumReplacements.find(({ options }) => options.collection === at.collection && options.field === at.field && options.path === void 0 && options.filter === void 0 && options.from === from && canonicalize(options.to) === canonicalize({ $literal: to }));
3473
+ }
3474
+ /** What changes for the documents of one variant of the origin, if anything does. */
3475
+ function changeOf(origin, fromContract, input) {
3476
+ if (embeddingModelOf(fromContract.domain, origin) !== void 0) return void 0;
3477
+ const before = selectedVariantOf(fromContract.domain, origin);
3478
+ const originCollection = collectionOfModel(fromContract.domain, origin);
3479
+ if (before === void 0 || originCollection === void 0) return void 0;
3480
+ const collection = input.collectionAfterRenames(originCollection);
3481
+ if (!input.keptCollections.has(collection)) return void 0;
3482
+ const destination = destinationOf(origin, fromContract, input);
3483
+ const after = destination === void 0 ? void 0 : selectedVariantOf(input.contract.domain, destination);
3484
+ if (destination !== void 0 && after === void 0) return void 0;
3485
+ const field = destinationDiscriminator(origin, destination, fromContract, input);
3486
+ if (field === void 0) return void 0;
3487
+ const from = String(before.value);
3488
+ const to = after === void 0 ? void 0 : String(after.value);
3489
+ if (to === from) return void 0;
3490
+ const statement = destination === void 0 ? void 0 : statementOf(origin, destination, input.statements);
3491
+ const byEnum = to === void 0 ? void 0 : enumReplacementOf(input, {
3492
+ collection,
3493
+ field
3494
+ }, from, to);
1478
3495
  return {
1479
- codecLookup: extractCodecLookup(frameworkComponents),
1480
- dataTypeLookup: assembleDataTypes(frameworkComponents).lookup
3496
+ model: origin,
3497
+ destination,
3498
+ collection,
3499
+ field,
3500
+ originField: before.field,
3501
+ from,
3502
+ to: statement === void 0 && byEnum === void 0 ? void 0 : to,
3503
+ statement,
3504
+ byEnum
3505
+ };
3506
+ }
3507
+ function quoted(value) {
3508
+ return JSON.stringify(value);
3509
+ }
3510
+ function variantText(model) {
3511
+ return `variant ${quoted(modelDisplayName(model))}`;
3512
+ }
3513
+ function changeLabel(change, input, fromContract) {
3514
+ return change.statement === void 0 ? `Cannot delete the documents of ${variantText(change.model)}` : `Cannot ${describeMigrationStatement(change.statement, fromContract, input.contract)}`;
3515
+ }
3516
+ function refusedChange(change, label, reason, why) {
3517
+ return change.statement === void 0 ? {
3518
+ kind: "unsupportedOperation",
3519
+ summary: `${label}: ${reason}`,
3520
+ why
3521
+ } : {
3522
+ kind: "statementRefused",
3523
+ summary: `${label}: ${reason}`,
3524
+ why,
3525
+ refusedStatement: change.statement
1481
3526
  };
1482
3527
  }
3528
+ /** How to change discriminator values that one migration cannot change safely. */
3529
+ const VARIANT_TWO_STEPS = inTwoSteps("delete the variants, or move their values to discriminator values no variant uses", "change the values to their final ones");
3530
+ /** Why a change cannot be planned in its collection: a time-series collection, or, for a statement, one whose control policy is not `managed`. */
3531
+ function collectionRefusal(change, input, fromContract) {
3532
+ const label = changeLabel(change, input, fromContract);
3533
+ const policy = collectionControlPolicy(input.contract, change.collection);
3534
+ if (policy !== "managed") return refusedChange(change, label, `collection "${change.collection}" has control policy "${policy}"`, "Statements rename only collections, and fields of collections, whose control policy is \"managed\". Make the change in the database yourself and leave out this statement.");
3535
+ if (input.schema.collection(change.collection)?.options?.timeseries === void 0) return sharedCollectionRefusal(change, label, fromContract);
3536
+ return refusedChange(change, label, `collection "${change.collection}" is a time-series collection`, `${TIME_SERIES_UPDATES}, so the plan does not rewrite or delete the documents of a variant there. Keep the variant and its discriminator value in the contract, then run this command again.`);
3537
+ }
3538
+ /**
3539
+ * The refusal of a change whose documents another root model stored in the same collection can
3540
+ * share: a model with the same discriminator field and value, whose documents the delete or
3541
+ * replacement would reach too.
3542
+ */
3543
+ function sharedCollectionRefusal(change, label, fromContract) {
3544
+ const originCollection = collectionOfModel(fromContract.domain, change.model);
3545
+ if (originCollection === void 0) return void 0;
3546
+ const others = otherModelsReached(fromContract.domain, change.model, originCollection, {
3547
+ field: change.originField,
3548
+ value: change.from
3549
+ }, void 0);
3550
+ if (others.length === 0) return void 0;
3551
+ const named = others.map(quoted).join(", ");
3552
+ const effect = change.to === void 0 ? "deleting the documents with that value would delete the documents" : "replacing the value would replace it in the documents";
3553
+ return refusedChange(change, label, `model ${named} ${others.length === 1 ? "is" : "are"} stored in collection ${quoted(change.collection)} and its documents can hold ${change.originField} ${quoted(change.from)}`, `${sharedCollectionWhy(change.collection, others, effect)} To change only the documents of ${variantText(change.model)}, ${SEPARATE_THE_MODELS}.`);
3554
+ }
3555
+ /**
3556
+ * The refusal of a change whose documents hold their discriminator value in a field the plan does
3557
+ * not rename to the destination's discriminator: the rewrite would select its documents by a field
3558
+ * that holds no variant's value.
3559
+ */
3560
+ function unrenamedDiscriminatorRefusal(change, input, fromContract) {
3561
+ const renamed = input.fieldRenames.some((rename) => rename.collection === change.collection && rename.from === change.originField && rename.to === change.field);
3562
+ if (change.field === change.originField || renamed) return void 0;
3563
+ const destinationBase = destinationBaseOf(change.model, change.destination, fromContract, input);
3564
+ const rename = destinationBase === void 0 ? void 0 : discriminatorRenameOf(baseOf(fromContract, change.model), destinationBase, {
3565
+ fromContract,
3566
+ contract: input.contract,
3567
+ statements: input.statements
3568
+ });
3569
+ const field = quoted(change.field);
3570
+ const discriminator = destinationBase === void 0 ? void 0 : modelAt(input.contract, destinationBase)?.discriminator?.field;
3571
+ const backfill = destinationBase === void 0 || discriminator === void 0 ? "--backfill" : `--backfill ${fieldStatementText(input.contract, {
3572
+ ...destinationBase,
3573
+ field: discriminator
3574
+ })}`;
3575
+ const advice = rename === void 0 ? inTwoSteps(`make ${field} the discriminator with the variants' current values, giving every document its value there (under migration plan, with ${backfill})`, "change the variants") : `Add ${rename.flags} and run the command again.`;
3576
+ const unrenamed = `hold their discriminator value in field ${quoted(change.originField)}, which the plan does not rename to ${field}`;
3577
+ return refusedChange(change, change.statement === void 0 ? "Cannot plan the migration" : changeLabel(change, input, fromContract), change.statement === void 0 ? `the documents of ${variantText(change.model)} ${unrenamed}` : `its documents ${unrenamed}`, `The plan rewrites the documents of a variant by their value in field ${field}, which only a rename of field ${quoted(change.originField)} gives them. ${advice}`);
3578
+ }
3579
+ function callOf(change) {
3580
+ if (change.byEnum !== void 0) return change.byEnum;
3581
+ return change.to === void 0 ? new DeleteDocumentsCall({
3582
+ collection: change.collection,
3583
+ filter: variantFilter({
3584
+ field: change.field,
3585
+ value: change.from
3586
+ })
3587
+ }) : new ReplaceStoredValueCall({
3588
+ collection: change.collection,
3589
+ field: change.field,
3590
+ from: change.from,
3591
+ to: { $literal: change.to }
3592
+ });
3593
+ }
3594
+ /**
3595
+ * The document rewrites a plan makes for the variants of the origin whose discriminator value the
3596
+ * destination changes or drops, as SQL plans their rows. A rename or convert statement replaces a
3597
+ * changed value in the base's collection; a changed value with no statement, and a variant the
3598
+ * destination no longer has, delete the documents that hold the old value, which the plan asks about.
3599
+ * Deletes run first, so later rewrites read fewer documents; a delete and a replacement of the same
3600
+ * value are refused for the whole plan (see `overlapRefusal`), so the order does not decide what the
3601
+ * documents end as. A rewrite that would reach the documents of another root model in the same
3602
+ * collection is refused. In a collection whose control policy is not `managed`, a statement is refused and a
3603
+ * delete left out. The discriminator is read from the destination's base, so a base that renames it
3604
+ * is followed, and a base that no longer has one keeps every document. A replacement of an enum value
3605
+ * that already moves a variant's value is that variant's replacement. With no origin contract,
3606
+ * nothing is known to have changed.
3607
+ */
3608
+ function planVariantDocumentRewrites(input) {
3609
+ const { fromContract } = input;
3610
+ if (fromContract === null) return ok({
3611
+ deletes: [],
3612
+ replacements: []
3613
+ });
3614
+ const changes = modelCoordinates(fromContract).flatMap((origin) => {
3615
+ const change = changeOf(origin, fromContract, input);
3616
+ if (change === void 0) return [];
3617
+ return change.to === void 0 && collectionControlPolicy(input.contract, change.collection) !== "managed" ? [] : [change];
3618
+ });
3619
+ const refusals = changes.flatMap((change) => {
3620
+ const refusal = collectionRefusal(change, input, fromContract) ?? unrenamedDiscriminatorRefusal(change, input, fromContract);
3621
+ return refusal === void 0 ? [] : [refusal];
3622
+ });
3623
+ if (refusals.length > 0) return notOk(refusals);
3624
+ const rewrites = changes.map((change) => ({
3625
+ ...change,
3626
+ call: callOf(change)
3627
+ }));
3628
+ return ok({
3629
+ deletes: rewrites.flatMap(({ call, ...rewrite }) => call instanceof DeleteDocumentsCall ? [{
3630
+ ...rewrite,
3631
+ call
3632
+ }] : []),
3633
+ replacements: rewrites.flatMap(({ call, ...rewrite }) => call instanceof ReplaceStoredValueCall ? [{
3634
+ ...rewrite,
3635
+ call
3636
+ }] : [])
3637
+ });
3638
+ }
1483
3639
  function renderOps(calls) {
1484
3640
  return calls.map((call) => call.toOp());
1485
3641
  }
@@ -1652,15 +3808,45 @@ function propertiesOf(schema) {
1652
3808
  for (const shape of validatorDocumentShapesOf(schema)) for (const [field, fieldSchema] of Object.entries(shape.properties)) schemas.set(field, (schemas.get(field) ?? /* @__PURE__ */ new Set()).add(canonicalize(fieldSchema)));
1653
3809
  return Object.fromEntries([...schemas].map(([field, distinct]) => [field, [...distinct].sort().join("|")]));
1654
3810
  }
3811
+ function propertyChanges(origin, dest) {
3812
+ return {
3813
+ changed: Object.keys(dest).filter((field) => Object.hasOwn(origin, field) && origin[field] !== dest[field]),
3814
+ added: Object.keys(dest).filter((field) => !Object.hasOwn(origin, field)),
3815
+ removed: Object.keys(origin).filter((field) => !Object.hasOwn(dest, field))
3816
+ };
3817
+ }
3818
+ /** The property changes of each kind of document both validators describe that the collection-wide changes do not name, each in the words of the documents it applies to. */
3819
+ function shapeChanges(origin, dest, named) {
3820
+ const canonical = (properties) => Object.fromEntries(Object.entries(properties).map(([field, schema]) => [field, canonicalize(schema)]));
3821
+ const shapesBefore = validatorDocumentShapesOf(origin.jsonSchema);
3822
+ const changes = validatorDocumentShapesOf(dest.jsonSchema).flatMap((shape) => {
3823
+ const before = shapesBefore.find(({ key }) => key === shape.key);
3824
+ if (before === void 0 || shape.discriminator === void 0) return [];
3825
+ const { field, value } = shape.discriminator;
3826
+ const where = ` in documents whose ${field} is ${JSON.stringify(value)}`;
3827
+ const found = propertyChanges(canonical(before.properties), canonical(shape.properties));
3828
+ const unnamed = (kind) => found[kind].filter((each) => !named[kind].includes(each)).map((each) => `${each}${where}`);
3829
+ return [{
3830
+ changed: unnamed("changed"),
3831
+ added: unnamed("added"),
3832
+ removed: unnamed("removed")
3833
+ }];
3834
+ });
3835
+ return {
3836
+ changed: changes.flatMap(({ changed }) => changed),
3837
+ added: changes.flatMap(({ added }) => added),
3838
+ removed: changes.flatMap(({ removed }) => removed)
3839
+ };
3840
+ }
1655
3841
  /**
1656
- * A label naming the properties whose schema changed, so a dry run shows what a validator update does: `Update validator on items (changed: meta; added: note)`.
3842
+ * A label naming the properties whose schema changed, so a dry run shows what a validator update does: `Update validator on items (changed: meta; added: note)`. A change of a variant's properties that the collection's properties as a whole do not show names the variant's documents.
1657
3843
  */
1658
3844
  function validatorUpdateLabel(collName, origin, dest) {
1659
- const originProps = propertiesOf(origin.jsonSchema);
1660
- const destProps = propertiesOf(dest.jsonSchema);
1661
- const changed = Object.keys(destProps).filter((field) => Object.hasOwn(originProps, field) && originProps[field] !== destProps[field]);
1662
- const added = Object.keys(destProps).filter((field) => !Object.hasOwn(originProps, field));
1663
- const removed = Object.keys(originProps).filter((field) => !Object.hasOwn(destProps, field));
3845
+ const whole = propertyChanges(propertiesOf(origin.jsonSchema), propertiesOf(dest.jsonSchema));
3846
+ const ofVariants = shapeChanges(origin, dest, whole);
3847
+ const changed = [...whole.changed, ...ofVariants.changed];
3848
+ const added = [...whole.added, ...ofVariants.added];
3849
+ const removed = [...whole.removed, ...ofVariants.removed];
1664
3850
  const parts = [
1665
3851
  ...changed.length > 0 ? [`changed: ${changed.join(", ")}`] : [],
1666
3852
  ...added.length > 0 ? [`added: ${added.join(", ")}`] : [],
@@ -1720,15 +3906,6 @@ function modelStoredIn(fromContract, collection) {
1720
3906
  }));
1721
3907
  return (storing.find(({ isRoot }) => isRoot) ?? storing[0])?.coordinate;
1722
3908
  }
1723
- /** A convert or backfill statement this release cannot carry out on MongoDB. */
1724
- function unsupportedStatementConflict(statement, fromContract, contract) {
1725
- return {
1726
- kind: "statementRefused",
1727
- summary: `MongoDB does not apply ${statement.kind} statements in this release, so nothing was planned: ${describeMigrationStatement(statement, fromContract, contract)}`,
1728
- why: `MongoDB cannot carry out ${statement.kind} statements in this release. Leave out the statement.`,
1729
- refusedStatement: statement
1730
- };
1731
- }
1732
3909
  /**
1733
3910
  * Each collection drop of the plan, with the model whose documents it loses. When that model keeps
1734
3911
  * its name and the plan brings its new collection into existence, the drop says how to rename the
@@ -1930,11 +4107,6 @@ var MongoMigrationPlanner = class {
1930
4107
  const contract = blindCast(options.contract);
1931
4108
  const originIR = blindCast(options.schema);
1932
4109
  const fromContract = options.fromContract;
1933
- const unsupported = options.statements.find((statement) => statement.kind !== "rename" && !((statement.kind === "convert" || statement.kind === "backfill") && statement.entity === "field"));
1934
- if (unsupported !== void 0) return {
1935
- kind: "failure",
1936
- conflicts: [unsupportedStatementConflict(unsupported, fromContract ?? contract, contract)]
1937
- };
1938
4110
  const renames = options.statements.filter((statement) => statement.kind === "rename");
1939
4111
  const statementPlan = planStatements({
1940
4112
  statements: renames,
@@ -1948,7 +4120,67 @@ var MongoMigrationPlanner = class {
1948
4120
  };
1949
4121
  const { schema, collectionRenames, fieldRenames, callOfStatement } = statementPlan.value;
1950
4122
  const destinationIR = contractToMongoSchemaIR(contract);
4123
+ const storedKeys = planStoredKeyRewrites({
4124
+ statements: renames,
4125
+ fromContract,
4126
+ contract,
4127
+ schema,
4128
+ destination: destinationIR
4129
+ });
4130
+ if (!storedKeys.ok) return {
4131
+ kind: "failure",
4132
+ conflicts: [...storedKeys.failure]
4133
+ };
4134
+ const storedKeyRewrites = [...storedKeys.value.renames, ...storedKeys.value.removals];
4135
+ const enumValues = planEnumValueRewrites({
4136
+ statements: renames,
4137
+ fromContract,
4138
+ contract,
4139
+ schema,
4140
+ lookups: mongoTypeLookupsOf(options.frameworkComponents)
4141
+ });
4142
+ if (!enumValues.ok) return {
4143
+ kind: "failure",
4144
+ conflicts: [...enumValues.failure]
4145
+ };
4146
+ const { replacements: valueReplacements, removals: valueRemovals } = enumValues.value;
1951
4147
  const renamedTo = new Map(collectionRenames.map((call) => [call.from, call.to]));
4148
+ const match = fromContract === null ? void 0 : {
4149
+ statements: renames,
4150
+ fromContract,
4151
+ contract,
4152
+ collectionAfterRenames: (collection) => renamedTo.get(collection) ?? collection
4153
+ };
4154
+ const lookups = mongoTypeLookupsOf(options.frameworkComponents);
4155
+ const plannedFills = planFieldFills({
4156
+ match,
4157
+ contract,
4158
+ lookups,
4159
+ schema,
4160
+ destination: destinationIR,
4161
+ statements: options.statements
4162
+ });
4163
+ if (!plannedFills.ok) return {
4164
+ kind: "failure",
4165
+ conflicts: [...plannedFills.failure]
4166
+ };
4167
+ const fills = plannedFills.value;
4168
+ const variants = planVariantDocumentRewrites({
4169
+ statements: options.statements,
4170
+ fromContract,
4171
+ contract,
4172
+ schema,
4173
+ keptCollections: new Set(schema.collectionNames.filter((name) => destinationIR.collection(name) !== void 0)),
4174
+ collectionAfterRenames: (collection) => renamedTo.get(collection) ?? collection,
4175
+ enumReplacements: valueReplacements.flatMap(({ call }) => call instanceof ReplaceStoredValueCall ? [call] : []),
4176
+ fieldRenames: fieldRenames.map(({ call }) => call)
4177
+ });
4178
+ if (!variants.ok) return {
4179
+ kind: "failure",
4180
+ conflicts: [...variants.failure]
4181
+ };
4182
+ const { deletes: variantDeletes, replacements: variantReplacements } = variants.value;
4183
+ const variantCalls = new Set(variantReplacements.map(({ call }) => call));
1952
4184
  const removals = fromContract === null ? planValidatorFieldRemovals({
1953
4185
  origin: schema,
1954
4186
  destination: destinationIR,
@@ -1960,13 +4192,6 @@ var MongoMigrationPlanner = class {
1960
4192
  collectionAfterRenames: (collection) => renamedTo.get(collection) ?? collection,
1961
4193
  keptCollections: new Set(schema.collectionNames.filter((name) => destinationIR.collection(name) !== void 0))
1962
4194
  });
1963
- const match = fromContract === null ? void 0 : {
1964
- statements: renames,
1965
- fromContract,
1966
- contract,
1967
- collectionAfterRenames: (collection) => renamedTo.get(collection) ?? collection
1968
- };
1969
- const lookups = mongoTypeLookupsOf(options.frameworkComponents);
1970
4195
  const typeChanges = fromContract === null ? planValidatorTypeChanges({
1971
4196
  origin: schema,
1972
4197
  destination: destinationIR,
@@ -1992,7 +4217,7 @@ var MongoMigrationPlanner = class {
1992
4217
  outcome: convertTransform(statement, change, contracts)
1993
4218
  }];
1994
4219
  }
1995
- if (statement.kind === "backfill" && statement.entity === "field") return [{
4220
+ if (statement.kind === "backfill" && statement.entity === "field" && embeddingModelOf(contract.domain, statement.field) === void 0) return [{
1996
4221
  statement,
1997
4222
  outcome: backfillTransform(statement, {
1998
4223
  contract,
@@ -2011,36 +4236,43 @@ var MongoMigrationPlanner = class {
2011
4236
  call
2012
4237
  }];
2013
4238
  });
2014
- const fills = planFieldFills({
2015
- match,
4239
+ const storedKeyFills = planStoredKeyFills({
4240
+ statements: options.statements,
4241
+ fromContract,
2016
4242
  contract,
2017
4243
  lookups,
2018
4244
  schema,
2019
- destination: destinationIR,
2020
- statements: options.statements
4245
+ destination: destinationIR
2021
4246
  });
4247
+ if (!storedKeyFills.ok) return {
4248
+ kind: "failure",
4249
+ conflicts: [...storedKeyFills.failure]
4250
+ };
2022
4251
  const rewrites = [
2023
- ...removals.map(({ call, subject }) => ({
4252
+ ...removals.map(({ call, destination, variant }) => ({
2024
4253
  kind: "removal",
2025
4254
  collection: call.collection,
2026
4255
  field: call.field,
2027
- filter: call.filter,
2028
- model: subject.kind === "field" ? subject : void 0
4256
+ variant,
4257
+ model: destination,
4258
+ storedKey: void 0
2029
4259
  })),
2030
4260
  ...casts.map((change) => ({
2031
4261
  kind: "cast",
2032
4262
  collection: change.collection,
2033
4263
  field: change.field,
2034
- filter: change.filter,
2035
- model: change.destination
4264
+ variant: change.variant,
4265
+ model: change.destination,
4266
+ storedKey: void 0
2036
4267
  })),
2037
4268
  ...transforms.flatMap(({ outcome }) => outcome.kind === "transform" ? [outcome.rewrite] : []),
2038
- ...fills.map(({ call, subject }) => ({
4269
+ ...fills.map(({ call, subject, variant }) => ({
2039
4270
  kind: "fill",
2040
4271
  collection: call.options.collection,
2041
4272
  field: call.options.field,
2042
- filter: call.options.filter,
2043
- model: subject.kind === "field" ? subject : void 0
4273
+ variant,
4274
+ model: subject.kind === "field" ? subject : void 0,
4275
+ storedKey: void 0
2044
4276
  }))
2045
4277
  ];
2046
4278
  const refusals = [
@@ -2056,61 +4288,226 @@ var MongoMigrationPlanner = class {
2056
4288
  kind: "failure",
2057
4289
  conflicts: refusals
2058
4290
  };
2059
- const castCalls = casts.map(({ call }) => call);
2060
- const callOf = new Map([...renames.map((statement, index) => [statement, callOfStatement[index]]), ...transforms.map(({ statement, outcome }) => outcome.kind === "transform" ? [statement, outcome.call] : [statement, void 0])]);
2061
- const transformCalls = (kind) => transforms.flatMap(({ statement, outcome }) => statement.kind === kind && outcome.kind === "transform" ? [outcome.call] : []);
4291
+ const callsOf = new Map([
4292
+ ...renames.map((statement, index) => {
4293
+ const call = callOfStatement[index];
4294
+ const keyCalls = [
4295
+ ...storedKeyRewrites,
4296
+ ...variantReplacements,
4297
+ ...valueReplacements
4298
+ ].flatMap((rewrite) => rewrite.statement === statement ? [rewrite.call] : []);
4299
+ return [statement, call === void 0 ? keyCalls : [call, ...keyCalls]];
4300
+ }),
4301
+ ...transforms.map(({ statement, outcome }) => outcome.kind === "transform" ? [statement, [outcome.call]] : [statement, []]),
4302
+ ...variantReplacements.flatMap((rewrite) => rewrite.statement?.kind === "convert" ? [[rewrite.statement, [rewrite.call]]] : []),
4303
+ ...options.statements.flatMap((statement) => {
4304
+ const calls = storedKeyFills.value.flatMap((fill) => fill.statement === statement ? [fill.call] : []);
4305
+ return calls.length === 0 ? [] : [[statement, calls]];
4306
+ })
4307
+ ]);
4308
+ const twoMigrationsAdvice = new Map([...[...valueReplacements, ...valueRemovals].map((rewrite) => [rewrite.call, rewrite.twoMigrationsAdvice]), ...[...variantDeletes, ...variantReplacements].map((rewrite) => [rewrite.call, VARIANT_TWO_STEPS])]);
4309
+ const changeOfCall = new Map([
4310
+ ...removals.map(({ call, subject }) => [call, {
4311
+ kind: "removed",
4312
+ subject
4313
+ }]),
4314
+ ...variantDeletes.map(({ call, model }) => [call, {
4315
+ kind: "removed",
4316
+ subject: {
4317
+ kind: "model",
4318
+ ...model
4319
+ }
4320
+ }]),
4321
+ ...storedKeys.value.removals.map(({ call, subject }) => [call, {
4322
+ kind: "removed",
4323
+ subject
4324
+ }]),
4325
+ ...valueRemovals.map(({ call, enumValue }) => [call, {
4326
+ kind: "removed",
4327
+ subject: {
4328
+ kind: "enumValue",
4329
+ ...enumValue
4330
+ }
4331
+ }]),
4332
+ ...fills.map(({ call, subject }) => [call, {
4333
+ kind: "newRequired",
4334
+ subject
4335
+ }]),
4336
+ ...storedKeyFills.value.flatMap(({ call, subject, text }) => text === void 0 ? [] : [[call, {
4337
+ kind: "newRequired",
4338
+ subject
4339
+ }]])
4340
+ ]);
4341
+ const rewriteOf = (facts) => {
4342
+ const statements = options.statements.filter((statement) => (callsOf.get(statement) ?? []).includes(facts.call));
4343
+ return {
4344
+ ...facts,
4345
+ statements,
4346
+ twoMigrationsAdvice: twoMigrationsAdvice.get(facts.call),
4347
+ unanswered: statements.length === 0 ? changeOfCall.get(facts.call) : void 0
4348
+ };
4349
+ };
4350
+ const writesDiscriminator = ({ statement }) => {
4351
+ const { namespaceId, model, field } = statement.field;
4352
+ return mongoModelsOf(contract.domain, namespaceId)[model]?.discriminator?.field === field;
4353
+ };
4354
+ const transformRewrites = (kind, selected = () => true) => transforms.filter(selected).flatMap(({ statement, outcome }) => statement.kind === kind && outcome.kind === "transform" ? [rewriteOf({
4355
+ kind: "dataTransform",
4356
+ call: outcome.call,
4357
+ transformedField: outcome.rewrite.field,
4358
+ discriminatorFilter: variantDiscriminatorFilter(outcome.rewrite.variant)
4359
+ })] : []);
4360
+ const fieldRenamesSelecting = (kind) => fieldRenames.flatMap(({ call, discriminatorFilter }) => discriminatorFilter?.kind === kind ? [rewriteOf({
4361
+ kind: "renameField",
4362
+ call,
4363
+ discriminatorFilter
4364
+ })] : []);
4365
+ const orderedRewrites = [
4366
+ ...fieldRenamesSelecting("variant"),
4367
+ ...fieldRenamesSelecting(void 0),
4368
+ ...variantDeletes.map(({ call, field, from }) => rewriteOf({
4369
+ kind: "deleteDocuments",
4370
+ call,
4371
+ deletedVariant: {
4372
+ field,
4373
+ value: from
4374
+ }
4375
+ })),
4376
+ ...variantReplacements.map(({ call, to }) => rewriteOf({
4377
+ kind: "replaceStoredValue",
4378
+ call,
4379
+ writtenValue: valueReplacements.find((replacement) => replacement.call === call)?.writtenValue ?? to,
4380
+ discriminatorFilter: void 0
4381
+ })),
4382
+ ...fieldRenamesSelecting("noneOf"),
4383
+ ...storedKeys.value.renames.map(({ call, variant }) => rewriteOf({
4384
+ kind: "renameStoredKey",
4385
+ call,
4386
+ discriminatorFilter: variantDiscriminatorFilter(variant)
4387
+ })),
4388
+ ...valueReplacements.flatMap(({ call, writtenValue, variant }) => variantCalls.has(call) ? [] : [rewriteOf({
4389
+ kind: "replaceStoredValue",
4390
+ call,
4391
+ writtenValue,
4392
+ discriminatorFilter: variantDiscriminatorFilter(variant)
4393
+ })]),
4394
+ ...transformRewrites("backfill", writesDiscriminator),
4395
+ ...casts.map(({ call, variant }) => rewriteOf({
4396
+ kind: "castField",
4397
+ call,
4398
+ discriminatorFilter: variantDiscriminatorFilter(variant)
4399
+ })),
4400
+ ...transformRewrites("convert"),
4401
+ ...fills.map(({ call, variant }) => rewriteOf({
4402
+ kind: "fillField",
4403
+ call,
4404
+ discriminatorFilter: variantDiscriminatorFilter(variant)
4405
+ })),
4406
+ ...storedKeyFills.value.map(({ call, variant }) => rewriteOf({
4407
+ kind: "fillStoredKey",
4408
+ call,
4409
+ discriminatorFilter: variantDiscriminatorFilter(variant)
4410
+ })),
4411
+ ...transformRewrites("backfill", (transform) => !writesDiscriminator(transform)),
4412
+ ...removals.map(({ call, discriminatorFilter }) => rewriteOf({
4413
+ kind: "unsetField",
4414
+ call,
4415
+ discriminatorFilter
4416
+ })),
4417
+ ...storedKeys.value.removals.map(({ call, variant }) => rewriteOf({
4418
+ kind: "removeStoredKey",
4419
+ call,
4420
+ discriminatorFilter: variantDiscriminatorFilter(variant)
4421
+ })),
4422
+ ...valueRemovals.map(({ call, variant }) => rewriteOf({
4423
+ kind: "removeStoredValue",
4424
+ call,
4425
+ discriminatorFilter: variantDiscriminatorFilter(variant)
4426
+ }))
4427
+ ];
4428
+ const overlap = overlapRefusal(orderedRewrites, {
4429
+ ...contracts,
4430
+ statements: options.statements
4431
+ });
4432
+ if (overlap !== void 0) return {
4433
+ kind: "failure",
4434
+ conflicts: [overlap]
4435
+ };
2062
4436
  const result = this.planCalls({
2063
4437
  ...options,
2064
4438
  statementCalls: {
2065
4439
  schema,
2066
4440
  collectionRenames,
2067
- rewrites: [
2068
- ...fieldRenames,
2069
- ...castCalls,
2070
- ...transformCalls("convert"),
2071
- ...fills.map((fill) => fill.call),
2072
- ...transformCalls("backfill"),
2073
- ...removals.map((removal) => removal.call)
2074
- ]
4441
+ rewrites: orderedRewrites.map(({ call }) => call)
2075
4442
  }
2076
4443
  });
2077
4444
  if (result.kind === "failure") return result;
2078
4445
  const { calls, warnings } = result;
2079
4446
  const allWarnings = [...warnings, ...match === void 0 ? [] : documentsNotRewrittenWarnings(match)];
2080
- const fieldLoss = [...removals.map((removal) => ({
2081
- operationIndex: calls.indexOf(removal.call),
2082
- subject: removal.subject,
2083
- loss: "drop",
2084
- label: removal.call.label
2085
- })), ...casts.flatMap(({ call, subject }) => call.operationClass !== "destructive" ? [] : [{
2086
- operationIndex: calls.indexOf(call),
2087
- subject,
2088
- loss: "typeChange",
2089
- label: call.label
2090
- }])];
4447
+ const fieldLoss = [
4448
+ ...variantDeletes.map((rewrite) => ({
4449
+ operationIndex: calls.indexOf(rewrite.call),
4450
+ subject: {
4451
+ kind: "model",
4452
+ ...rewrite.model
4453
+ },
4454
+ loss: "drop",
4455
+ label: rewrite.call.label
4456
+ })),
4457
+ ...removals.map((removal) => ({
4458
+ operationIndex: calls.indexOf(removal.call),
4459
+ subject: removal.subject,
4460
+ loss: "drop",
4461
+ label: removal.call.label
4462
+ })),
4463
+ ...storedKeys.value.removals.map((removal) => ({
4464
+ operationIndex: calls.indexOf(removal.call),
4465
+ subject: removal.subject,
4466
+ loss: "drop",
4467
+ label: removal.call.label
4468
+ })),
4469
+ ...valueRemovals.map((removal) => ({
4470
+ operationIndex: calls.indexOf(removal.call),
4471
+ subject: {
4472
+ kind: "enumValue",
4473
+ ...removal.enumValue
4474
+ },
4475
+ loss: "drop",
4476
+ label: removal.call.label,
4477
+ ...ifDefined("keepDataEdit", removal.keepDataEdit),
4478
+ ...ifDefined("keepDataAdvice", removal.keepDataAdvice)
4479
+ })),
4480
+ ...casts.flatMap(({ call, subject }) => call.operationClass !== "destructive" ? [] : [{
4481
+ operationIndex: calls.indexOf(call),
4482
+ subject,
4483
+ loss: "typeChange",
4484
+ label: call.label
4485
+ }])
4486
+ ];
2091
4487
  return {
2092
4488
  kind: "success",
2093
4489
  plan: new PlannerProducedMongoMigration(calls, {
2094
4490
  from: options.origin?.storageHash ?? null,
2095
4491
  to: contract.storage.storageHash
2096
4492
  }, options.snapshotsImportPath),
2097
- appliedStatements: options.statements.map((statement) => {
2098
- const call = callOf.get(statement);
2099
- return {
2100
- statement,
2101
- operationIndexes: call === void 0 ? [] : [calls.indexOf(call)]
2102
- };
2103
- }),
4493
+ appliedStatements: options.statements.map((statement) => ({
4494
+ statement,
4495
+ operationIndexes: (callsOf.get(statement) ?? []).map((call) => calls.indexOf(call))
4496
+ })),
2104
4497
  dataLoss: [...collectionDrops(calls, fromContract, contract, {
2105
4498
  origin: schema,
2106
4499
  destination: destinationIR
2107
4500
  }), ...fieldLoss].sort((a, b) => a.operationIndex - b.operationIndex),
2108
4501
  accessChanges: [],
2109
- temporaryValues: fills.map((fill) => ({
4502
+ temporaryValues: [...fills.map((fill) => ({
2110
4503
  operationIndex: calls.indexOf(fill.call),
2111
4504
  subject: fill.subject,
2112
4505
  value: fill.text
2113
- })),
4506
+ })), ...storedKeyFills.value.flatMap(({ call, subject, text }) => text === void 0 ? [] : [{
4507
+ operationIndex: calls.indexOf(call),
4508
+ subject,
4509
+ value: text
4510
+ }])],
2114
4511
  ...allWarnings.length > 0 ? { warnings: allWarnings } : {}
2115
4512
  };
2116
4513
  }
@@ -2170,14 +4567,14 @@ function planMutableOptionsDiffCall(collName, origin, dest) {
2170
4567
  operationClass: "widening"
2171
4568
  });
2172
4569
  }
2173
- function isRecord(value) {
4570
+ function isRecord$1(value) {
2174
4571
  return value !== null && typeof value === "object";
2175
4572
  }
2176
4573
  function getNestedField(doc, path) {
2177
4574
  const parts = path.split(".");
2178
4575
  let current = doc;
2179
4576
  for (const part of parts) {
2180
- if (!isRecord(current)) return;
4577
+ if (!isRecord$1(current)) return;
2181
4578
  const record = current;
2182
4579
  if (!Object.hasOwn(record, part)) return;
2183
4580
  current = record[part];
@@ -2441,6 +4838,35 @@ const OperationMetaJson = type({ "rewrite?": type({
2441
4838
  kind: "\"fill\"",
2442
4839
  collection: "string",
2443
4840
  field: "string"
4841
+ }).or({
4842
+ kind: "\"renameStoredKey\"",
4843
+ collection: "string",
4844
+ field: "string",
4845
+ path: "string",
4846
+ to: "string"
4847
+ }).or({
4848
+ kind: "\"removeStoredKey\" | \"fillStoredKey\"",
4849
+ collection: "string",
4850
+ field: "string",
4851
+ path: "string"
4852
+ }).or({
4853
+ kind: "\"replaceStoredValue\"",
4854
+ collection: "string",
4855
+ field: "string",
4856
+ "path?": "string"
4857
+ }).or({
4858
+ kind: "\"removeStoredValue\"",
4859
+ collection: "string",
4860
+ field: "string",
4861
+ "path?": "string",
4862
+ value: "string | number | boolean",
4863
+ "required?": "boolean",
4864
+ "filter?": "Record<string, unknown>",
4865
+ "enumValue?": {
4866
+ "namespaceId?": "string",
4867
+ enum: "string",
4868
+ member: "string"
4869
+ }
2444
4870
  }) });
2445
4871
  const DdlOperationJson = type({
2446
4872
  id: "string",
@@ -2829,6 +5255,12 @@ function failedCheckOf(operationId, phase, check) {
2829
5255
  function runnerFailure(code, summary, options) {
2830
5256
  return notOk(migrationRunnerFailure(code, summary, options));
2831
5257
  }
5258
+ /** The mongosh query that finds the documents a DML operation's first precheck matches. */
5259
+ function precheckFind(op, collection) {
5260
+ const source = op.precheck[0]?.source.command;
5261
+ const match = source instanceof RawAggregateCommand ? Reflect.get(source.pipeline[0] ?? {}, "$match") : void 0;
5262
+ return `db.getCollection(${JSON.stringify(collection)}).find(${JSON.stringify(match ?? {})})`;
5263
+ }
2832
5264
  /**
2833
5265
  * The failure when a field rename's before-check finds documents that already hold the new name,
2834
5266
  * which `$rename` would overwrite: what to look for and fix, and that the plan resumes.
@@ -2839,12 +5271,28 @@ function renameOverwriteRefusal(op) {
2839
5271
  const renames = Reflect.get(rewrite.command.update, "$rename");
2840
5272
  const to = typeof renames === "object" && renames !== null ? Reflect.get(renames, rewrite.field) : void 0;
2841
5273
  if (typeof to !== "string") return void 0;
2842
- const [check] = op.precheck;
2843
- const source = check?.source.command;
2844
- const match = source instanceof RawAggregateCommand ? Reflect.get(source.pipeline[0] ?? {}, "$match") : void 0;
2845
- const find = `db.getCollection(${JSON.stringify(rewrite.collection)}).find(${JSON.stringify(match ?? {})})`;
5274
+ const find = precheckFind(op, rewrite.collection);
2846
5275
  return runnerFailure("MIGRATION.PRECHECK_FAILED", `Operation ${op.id} stopped: some documents in collection ${rewrite.collection} already hold ${to} as well as ${rewrite.field}`, {
2847
- why: `Renaming ${rewrite.field} to ${to} would overwrite their ${to}. Find them with ${find} in mongosh, and move or remove one of the two values in each. Nothing was renamed. Once the documents are fixed, run the command again; operations that already ran are skipped.`,
5276
+ why: `Renaming ${rewrite.field} to ${to} would overwrite their ${to}. Find them with ${find} in mongosh, and move or remove one of the two values in each. Once the documents are fixed, run the command again; operations that already ran are skipped.`,
5277
+ meta: {
5278
+ operationId: op.id,
5279
+ name: op.name
5280
+ }
5281
+ });
5282
+ }
5283
+ /**
5284
+ * The failure when a key rename's before-check finds objects that already hold the new key, which
5285
+ * the rename would overwrite: how to find them, and that the plan resumes.
5286
+ */
5287
+ function storedKeyRenameRefusal(op) {
5288
+ const rewrite = op.meta?.rewrite;
5289
+ if (rewrite?.kind !== "renameStoredKey") return void 0;
5290
+ const keyPath = storedKeyPath(rewrite.field, rewrite.path);
5291
+ if (!keyPath.ok) return void 0;
5292
+ const from = JSON.stringify(keyPath.value.key);
5293
+ const to = JSON.stringify(rewrite.to);
5294
+ return runnerFailure("MIGRATION.PRECHECK_FAILED", `Operation ${op.id} stopped: some objects at path ${JSON.stringify(keyPath.value.objectsText)} in field ${rewrite.field} of collection ${rewrite.collection} already hold key ${to} as well as key ${from}`, {
5295
+ why: `Renaming key ${from} to ${to} would overwrite their ${to}. Find them with ${precheckFind(op, rewrite.collection)} in mongosh, and remove one of the two keys from each of those objects. Once the documents are fixed, run the command again; operations that already ran are skipped.`,
2848
5296
  meta: {
2849
5297
  operationId: op.id,
2850
5298
  name: op.name
@@ -2859,7 +5307,7 @@ function renameTargetExistsRefusal(op, failed) {
2859
5307
  if (!(rename instanceof RenameCollectionCommand) || failed.expect !== "notExists") return;
2860
5308
  const { collection: from, to } = rename;
2861
5309
  return runnerFailure("MIGRATION.PRECHECK_FAILED", `Operation ${op.id} stopped: collection ${to} already exists, so collection ${from} cannot be renamed to it`, {
2862
- why: `Nothing was renamed. If ${to} holds nothing you need, drop it, then run the command again; operations that already ran are skipped. If an earlier operation of the same migration created ${to}, as a createIndex on ${to} does, make renameCollection(${JSON.stringify(from)}, ${JSON.stringify(to)}) the first operation of its migration.ts, run node on that migration.ts, and then drop ${to} and run the command again.`,
5310
+ why: `If ${to} holds nothing you need, drop it, then run the command again; operations that already ran are skipped. If an earlier operation of the same migration created ${to}, as a createIndex on ${to} does, make renameCollection(${JSON.stringify(from)}, ${JSON.stringify(to)}) the first operation of its migration.ts, run node on that migration.ts, and then drop ${to} and run the command again.`,
2863
5311
  meta: { operationId: op.id }
2864
5312
  });
2865
5313
  }
@@ -2929,17 +5377,120 @@ function joinedList(items) {
2929
5377
  }
2930
5378
  /**
2931
5379
  * The `--backfill` statements that let the user choose values for the fields a change makes
2932
- * required: one for each field the planner would accept the statement for, as its rewrite refusals
2933
- * and stored name rules say.
5380
+ * required, one for each field the planner would accept the statement for, as its rewrite refusals
5381
+ * and stored name rules say; and each such field as `rowsWithoutValue`, from which the caller gives
5382
+ * the steps that plan the change with them. A field only new variants require gets its value in
5383
+ * the update of the discriminator's backfill, or later by its own.
5384
+ */
5385
+ function backfillAdvice(contract, lacking, touched, rewriteCollection, position) {
5386
+ const lackingFields = [...lacking.documents.keys()].filter((field) => !position.renamed.has(field));
5387
+ const accepted = backfillStatements(contract, lacking.collection, lackingFields.filter((field) => !touched.ofNewVariants.has(field)), touched.keys, rewriteCollection);
5388
+ if (accepted.length === 0) return {
5389
+ sentences: [],
5390
+ rowsWithoutValue: []
5391
+ };
5392
+ const statements = [...new Set(accepted.map(({ statement }) => `--backfill ${statement}`))];
5393
+ const subjects = new Map(accepted.map(({ stored, subject }) => [migrationSubjectKey(subject), {
5394
+ subject,
5395
+ documents: lacking.documents.get(stored) ?? 0
5396
+ }]));
5397
+ return {
5398
+ sentences: [`To give ${quotedList(accepted.map(({ stored }) => stored))} values you choose, plan the change with ${statements.join(" ")}, as the steps to take describe.`, ...newVariantFieldsAdvice(contract, lacking.collection, lackingFields.flatMap((field) => {
5399
+ const discriminator = touched.ofNewVariants.get(field);
5400
+ return discriminator === void 0 ? [] : [{
5401
+ field,
5402
+ discriminator
5403
+ }];
5404
+ }), accepted)],
5405
+ rowsWithoutValue: [...subjects.values()].map(({ subject, documents }) => ({
5406
+ field: subject,
5407
+ reason: "newField",
5408
+ rows: documents,
5409
+ operationIndex: position.operationIndex
5410
+ }))
5411
+ };
5412
+ }
5413
+ /**
5414
+ * How fields that only new variants require get values: the update of the discriminator's
5415
+ * `--backfill` sets them where it gives a variant's value, or they stay optional until a later
5416
+ * change, when their variant has documents, makes them required with their own `--backfill`.
5417
+ */
5418
+ function newVariantFieldsAdvice(contract, collection, fields, accepted) {
5419
+ const [first] = fields;
5420
+ const discriminatorBackfill = first === void 0 ? void 0 : accepted.find(({ stored }) => stored === first.discriminator.field);
5421
+ if (first === void 0 || discriminatorBackfill === void 0) return [];
5422
+ const where = [...new Set(fields.map(({ discriminator }) => discriminator.value))].map((value) => {
5423
+ return `${quotedList(fields.filter(({ discriminator }) => discriminator.value === value).map(({ field }) => field))} in the documents it gives value ${JSON.stringify(value)}`;
5424
+ });
5425
+ const example = fields.filter(({ discriminator }) => discriminator.value === first.discriminator.value).map(({ field }) => `${field}: <value>`);
5426
+ const later = fields.flatMap(({ field }) => {
5427
+ const subject = fieldStoredAt(contract, collection, field);
5428
+ return subject.kind === "field" ? [`--backfill ${fieldStatementText(contract, subject)}`] : [];
5429
+ });
5430
+ const them = fields.length === 1 ? "it" : "them";
5431
+ return [`The update you write for --backfill ${discriminatorBackfill.statement} can also set ${joinedList(where)}, such as { $set: { ${first.discriminator.field}: ${JSON.stringify(first.discriminator.value)}, ${example.join(", ")} } }.`, `Or keep ${quotedList(fields.map(({ field }) => field))} optional until a later change, which can make ${them} required${later.length === 0 ? "" : ` with ${later.join(" ")}`}.`];
5432
+ }
5433
+ /**
5434
+ * The fields and nested keys, by their dotted stored paths such as `home.line1`, to which a rename
5435
+ * of the plan gives its new name in `collection`. Such a field was required under its old name, so
5436
+ * a document without it lacked the old one too: it needs fixing, not a `--backfill`.
2934
5437
  */
2935
- function backfillAdvice(contract, collection, requires, rewriteCollection) {
2936
- const accepted = backfillStatements(contract, collection, requires, rewriteCollection);
2937
- if (accepted.length === 0) return [];
2938
- return [`To give ${quotedList(accepted.map(({ stored }) => stored))} values you choose, plan the change with migration plan ${accepted.map(({ statement }) => `--backfill ${statement}`).join(" ")}.`];
5438
+ function renamedInto(operations, collection) {
5439
+ return new Set(operations.flatMap((operation) => {
5440
+ if (!isMongoDmlOperation(operation)) return [];
5441
+ const rewrite = operation.meta?.rewrite;
5442
+ if (rewrite?.kind === "renameStoredKey") {
5443
+ if (rewrite.collection !== collection) return [];
5444
+ const parsed = storedKeyPath(rewrite.field, rewrite.path);
5445
+ if (!parsed.ok) return [];
5446
+ return [[
5447
+ rewrite.field,
5448
+ ...keysOf(parsed.value).slice(0, -1),
5449
+ rewrite.to
5450
+ ].join(".")];
5451
+ }
5452
+ const renamed = fieldRewriteOf(operation);
5453
+ if (renamed === void 0 || renamed.collection !== collection) return [];
5454
+ const names = Reflect.get(renamed.command.update, "$rename");
5455
+ return isRecord(names) ? Object.values(names).filter((name) => typeof name === "string") : [];
5456
+ }));
5457
+ }
5458
+ /** The `--backfill` statement of a nested key, by the field of a value object or an embedded model stored there, and that field, when the planner would accept it. */
5459
+ function nestedBackfillStatement(contract, collection, key, rewriteCollection) {
5460
+ const parsed = storedKeyPath(key.field, key.path);
5461
+ if (!parsed.ok) return void 0;
5462
+ if ([key.field, ...parsed.value.path.steps.flatMap((step) => step.kind === "member" ? [step.name] : [])].some((name) => unrewritableKeyReason(name) !== void 0)) return void 0;
5463
+ const found = storedKeyFieldAt(contract, {
5464
+ collection,
5465
+ ...key
5466
+ });
5467
+ if (found === void 0) return void 0;
5468
+ return rewriteRefusals({
5469
+ kind: "fillStoredKey",
5470
+ collection,
5471
+ field: key.field,
5472
+ variant: found.place.variant,
5473
+ model: found.place.model,
5474
+ storedKey: {
5475
+ key: found.text,
5476
+ together: []
5477
+ }
5478
+ }, contract, rewriteCollection).length === 0 ? {
5479
+ statement: found.text,
5480
+ subject: found.subject
5481
+ } : void 0;
2939
5482
  }
2940
5483
  /** The `--backfill` statement of each of `fields` the planner would accept it for, with the field's stored name. */
2941
- function backfillStatements(contract, collection, fields, rewriteCollection) {
5484
+ function backfillStatements(contract, collection, fields, keys, rewriteCollection) {
2942
5485
  return fields.flatMap((stored) => {
5486
+ const key = keys.get(stored);
5487
+ if (key !== void 0) {
5488
+ const found = nestedBackfillStatement(contract, collection, key, rewriteCollection);
5489
+ return found === void 0 ? [] : [{
5490
+ stored,
5491
+ ...found
5492
+ }];
5493
+ }
2943
5494
  if (unrewritableFieldNameReason(stored) !== void 0) return [];
2944
5495
  const field = fieldStoredAt(contract, collection, stored);
2945
5496
  if (field.kind !== "field") return [];
@@ -2951,11 +5502,13 @@ function backfillStatements(contract, collection, fields, rewriteCollection) {
2951
5502
  kind: "backfill",
2952
5503
  collection,
2953
5504
  field: stored,
2954
- filter: variantFilterOf(contract.domain, model),
2955
- model
5505
+ variant: selectedVariantOf(contract.domain, model),
5506
+ model,
5507
+ storedKey: void 0
2956
5508
  }, contract, rewriteCollection).length === 0 ? [{
2957
5509
  stored,
2958
- statement: fieldStatementText(contract, field)
5510
+ statement: fieldStatementText(contract, field),
5511
+ subject: field
2959
5512
  }] : [];
2960
5513
  });
2961
5514
  }
@@ -2986,50 +5539,145 @@ function mongoshValue(value) {
2986
5539
  }
2987
5540
  return JSON.stringify(value) ?? String(value);
2988
5541
  }
2989
- function documentShapes(jsonSchema) {
2990
- return validatorDocumentShapesOf(jsonSchema).map((shape) => ({
2991
- key: shape.key,
2992
- required: new Set(shape.required),
2993
- properties: new Map(Object.entries(shape.properties).map(([field, schema]) => [field, canonicalize(schema)]))
2994
- }));
2995
- }
2996
5542
  const ANY_VALUE = canonicalize({});
2997
- const NO_SHAPE = {
2998
- key: "",
2999
- required: /* @__PURE__ */ new Set(),
3000
- properties: /* @__PURE__ */ new Map()
3001
- };
5543
+ function isRecord(value) {
5544
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5545
+ }
5546
+ /** The object schemas a property schema accepts: itself, or the branches of its `oneOf` or `anyOf`. */
5547
+ function objectSchemasOf(schema) {
5548
+ if (!isRecord(schema)) return [];
5549
+ if (isRecord(schema["properties"])) return [schema];
5550
+ return [schema["oneOf"], schema["anyOf"]].flatMap((branches) => Array.isArray(branches) ? branches : []).flatMap(objectSchemasOf);
5551
+ }
5552
+ /** The element schema a property schema gives a list, itself or through its `oneOf` or `anyOf` branches. */
5553
+ function itemsOf(schema) {
5554
+ if (!isRecord(schema)) return void 0;
5555
+ if (schema["items"] !== void 0) return schema["items"];
5556
+ return [schema["oneOf"], schema["anyOf"]].flatMap((branches) => Array.isArray(branches) ? branches : []).map(itemsOf).find((items) => items !== void 0);
5557
+ }
5558
+ function requiredOf(schema) {
5559
+ const required = schema["required"];
5560
+ return new Set(Array.isArray(required) ? required.filter((key) => typeof key === "string") : []);
5561
+ }
5562
+ /** The keys below a property that the new schema requires and the old one, which also describes the objects there, did not. A union of object schemas is not compared. */
5563
+ function nestedRequiredKeys(before, after, steps) {
5564
+ const [objectBefore, ...otherBefore] = objectSchemasOf(before);
5565
+ const [objectAfter, ...otherAfter] = objectSchemasOf(after);
5566
+ const inObjects = objectBefore === void 0 || objectAfter === void 0 || otherBefore.length > 0 || otherAfter.length > 0 ? [] : [...[...requiredOf(objectAfter)].filter((key) => !requiredOf(objectBefore).has(key)).map((key) => [...steps, {
5567
+ kind: "member",
5568
+ name: key
5569
+ }]), ...Object.entries(isRecord(objectAfter["properties"]) ? objectAfter["properties"] : {}).filter(([key]) => isRecord(objectBefore["properties"]) && Object.hasOwn(objectBefore["properties"], key)).flatMap(([key, schema]) => nestedRequiredKeys(Reflect.get(objectBefore["properties"] ?? {}, key), schema, [...steps, {
5570
+ kind: "member",
5571
+ name: key
5572
+ }]))];
5573
+ const itemsBefore = itemsOf(before);
5574
+ const itemsAfter = itemsOf(after);
5575
+ return [...inObjects, ...itemsBefore === void 0 || itemsAfter === void 0 ? [] : nestedRequiredKeys(itemsBefore, itemsAfter, [...steps, { kind: "wildcard" }])];
5576
+ }
3002
5577
  /**
3003
5578
  * The fields a validator change makes required, and those whose accepted values it changes, each
3004
- * kind of document compared with the same kind under the validator the collection has. A kind the
3005
- * collection's validator does not describe is not compared; with no validator, every field it
3006
- * requires is new.
5579
+ * kind of document compared with the same kind under the validator the collection has. Inside a field both validators describe, a
5580
+ * key of its objects or list elements that the change makes required is named by its dotted path. A kind the collection's validator does not describe is not
5581
+ * compared; with no validator, every field it requires is new.
3007
5582
  */
3008
5583
  function fieldsTouched(before, after) {
3009
- const shapesBefore = documentShapes(before);
5584
+ const shapesBefore = validatorDocumentShapesOf(before);
3010
5585
  const requires = /* @__PURE__ */ new Set();
3011
5586
  const retypes = /* @__PURE__ */ new Set();
3012
- for (const shape of documentShapes(after)) {
3013
- const previous = shapesBefore.length === 0 ? NO_SHAPE : shapesBefore.find(({ key }) => key === shape.key);
3014
- if (previous === void 0) continue;
3015
- for (const field of shape.required) if (field !== "_id" && !previous.required.has(field)) requires.add(field);
3016
- for (const [field, schema] of shape.properties) {
3017
- const schemaBefore = previous.properties.get(field);
3018
- if (schemaBefore !== void 0 && schemaBefore !== schema && schema !== ANY_VALUE) retypes.add(field);
5587
+ const keys = /* @__PURE__ */ new Map();
5588
+ const ofOldKinds = /* @__PURE__ */ new Set();
5589
+ const ofNewVariants = /* @__PURE__ */ new Map();
5590
+ const objectLists = /* @__PURE__ */ new Set();
5591
+ const rootsAfter = validatorRootShapesOf(after);
5592
+ for (const shape of validatorDocumentShapesOf(after)) {
5593
+ const same = shapesBefore.length === 0 ? void 0 : sameKindOfDocumentIn(shapesBefore, shape);
5594
+ if (shapesBefore.length > 0 && same === void 0) continue;
5595
+ const previous = same?.shape ?? {
5596
+ required: [],
5597
+ properties: {}
5598
+ };
5599
+ const ownedByRoot = /* @__PURE__ */ new Set([...rootsAfter[shape.rootIndex]?.required ?? [], ...shape.discriminator === void 0 ? [] : [shape.discriminator.field]]);
5600
+ for (const field of shape.required) {
5601
+ if (field === "_id" || previous.required.includes(field)) continue;
5602
+ requires.add(field);
5603
+ if (same?.matchedBy === "root" && shape.discriminator !== void 0 && !ownedByRoot.has(field)) {
5604
+ if (!ofNewVariants.has(field)) ofNewVariants.set(field, shape.discriminator);
5605
+ } else ofOldKinds.add(field);
5606
+ }
5607
+ for (const [field, schema] of Object.entries(shape.properties)) {
5608
+ const items = itemsOf(schema);
5609
+ if (isRecord(items) && isRecord(items["properties"])) objectLists.add(field);
5610
+ if (!Object.hasOwn(previous.properties, field)) continue;
5611
+ const schemaBefore = previous.properties[field];
5612
+ const canonical = canonicalize(schema);
5613
+ if (canonicalize(schemaBefore) !== canonical && canonical !== ANY_VALUE) retypes.add(field);
5614
+ for (const steps of nestedRequiredKeys(schemaBefore, schema, [])) {
5615
+ const dotted = [field, ...steps.flatMap((step) => step.kind === "member" ? [step.name] : [])].join(".");
5616
+ requires.add(dotted);
5617
+ keys.set(dotted, {
5618
+ field,
5619
+ path: jsonPathText(steps),
5620
+ steps
5621
+ });
5622
+ }
3019
5623
  }
3020
5624
  }
3021
5625
  return {
3022
5626
  requires: [...requires].sort(),
3023
- retypes: [...retypes].filter((field) => !requires.has(field)).sort()
5627
+ retypes: [...retypes].filter((field) => !requires.has(field)).sort(),
5628
+ keys,
5629
+ ofNewVariants: new Map([...ofNewVariants].filter(([field]) => !ofOldKinds.has(field))),
5630
+ objectLists
3024
5631
  };
3025
5632
  }
3026
5633
  function quotedList(fields) {
3027
5634
  const quoted = fields.map((field) => JSON.stringify(field));
3028
5635
  return quoted.length <= 1 ? quoted.join("") : `${quoted.slice(0, -1).join(", ")} and ${quoted.at(-1)}`;
3029
5636
  }
3030
- /** What a validator change touches, in words: `This validator change makes "plan" required.` */
3031
- function touchedSentence(touched) {
3032
- const parts = [...touched.requires.length === 0 ? [] : [`makes ${quotedList(touched.requires)} required`], ...touched.retypes.length === 0 ? [] : [`changes what ${quotedList(touched.retypes)} ${touched.retypes.length === 1 ? "accepts" : "accept"}`]];
5637
+ /** What a document holds at the objects a required key's steps lead to: an object without the key, or a list element that is not an object. */
5638
+ function keyFindingsOf(value, steps) {
5639
+ const [step, ...rest] = steps;
5640
+ if (step === void 0) return /* @__PURE__ */ new Set();
5641
+ if (step.kind === "wildcard") {
5642
+ if (!Array.isArray(value)) return /* @__PURE__ */ new Set();
5643
+ return new Set(value.flatMap((element) => rest[0]?.kind === "member" && !isRecord(element) ? ["notObject"] : [...keyFindingsOf(element, rest)]));
5644
+ }
5645
+ if (!isRecord(value)) return /* @__PURE__ */ new Set();
5646
+ if (!Object.hasOwn(value, step.name)) return new Set(rest.length === 0 ? ["lacks"] : []);
5647
+ return keyFindingsOf(value[step.name], rest);
5648
+ }
5649
+ /**
5650
+ * Of the nested keys a validator change makes required, those the document shown lacks, and the
5651
+ * fields whose list elements must be objects, or hold keys the change requires, that hold an
5652
+ * element that is not an object.
5653
+ */
5654
+ function nestedFindings(touched, shown) {
5655
+ const lacking = [];
5656
+ const notObjects = /* @__PURE__ */ new Set();
5657
+ for (const [dotted, key] of touched.keys) {
5658
+ const findings = keyFindingsOf(shown.get(key.field)?.value, key.steps);
5659
+ if (findings.has("lacks")) lacking.push(dotted);
5660
+ if (findings.has("notObject")) notObjects.add(key.field);
5661
+ }
5662
+ for (const field of touched.objectLists) {
5663
+ const value = shown.get(field)?.value;
5664
+ if (Array.isArray(value) && value.some((element) => !isRecord(element))) notObjects.add(field);
5665
+ }
5666
+ return {
5667
+ lacking,
5668
+ notObjects: [...notObjects].sort()
5669
+ };
5670
+ }
5671
+ /**
5672
+ * What a validator change touches, in words: `This validator change makes "plan" required.` Only
5673
+ * what the document shown breaks is named: the fields and nested keys it lacks, and the fields it
5674
+ * holds whose accepted values change.
5675
+ */
5676
+ function touchedSentence(touched, shown) {
5677
+ const lackingNested = new Set(nestedFindings(touched, shown).lacking);
5678
+ const lacking = touched.requires.filter((field) => touched.keys.has(field) ? lackingNested.has(field) : !shown.has(field));
5679
+ const retypes = touched.retypes.filter((field) => shown.has(field));
5680
+ const parts = [...lacking.length === 0 ? [] : [`makes ${quotedList(lacking)} required`], ...retypes.length === 0 ? [] : [`changes what ${quotedList(retypes)} ${retypes.length === 1 ? "accepts" : "accept"}`]];
3033
5681
  return parts.length === 0 ? "" : `This validator change ${parts.join(" and ")}. `;
3034
5682
  }
3035
5683
  const MONGO_UNAUTHORIZED = 13;
@@ -3169,6 +5817,18 @@ function rewriteFailure(op, error, position) {
3169
5817
  }
3170
5818
  });
3171
5819
  }
5820
+ /** The edit that keeps the documents a failed removal found: the removal, replaced by a replacement whose new value is the placeholder `"<stored value>"`. */
5821
+ function keepRemovedValueEdit(options) {
5822
+ const { value, required: _required, enumValue: _enumValue, ...at } = options;
5823
+ return {
5824
+ remove: callInSentence(new RemoveStoredValueCall(options)),
5825
+ replaceWith: callInSentence(new ReplaceStoredValueCall({
5826
+ ...at,
5827
+ from: value,
5828
+ to: "<stored value>"
5829
+ }))
5830
+ };
5831
+ }
3172
5832
  var MongoMigrationRunner = class {
3173
5833
  deps;
3174
5834
  constructor(deps) {
@@ -3219,7 +5879,10 @@ var MongoMigrationRunner = class {
3219
5879
  const failing = await this.firstFailingCheck(operation.precheck, inspectionExecutor, filterEvaluator);
3220
5880
  if (failing !== void 0) {
3221
5881
  const failedCheck = failedCheckOf(operation.id, "precheck", failing);
3222
- return renameTargetExistsRefusal(operation, failing) ?? await this.documentsFailingValidatorRefusal(operation, failing, options.destinationContract) ?? runnerFailure("MIGRATION.PRECHECK_FAILED", `Operation ${operation.id} failed during precheck: ${failing.description}`, {
5882
+ return renameTargetExistsRefusal(operation, failing) ?? await this.documentsFailingValidatorRefusal(operation, failing, options.destinationContract, {
5883
+ operationIndex,
5884
+ plan: operations
5885
+ }) ?? runnerFailure("MIGRATION.PRECHECK_FAILED", `Operation ${operation.id} failed during precheck: ${failing.description}`, {
3223
5886
  why: failedPrecheckWhy(failedCheck),
3224
5887
  meta: { failedCheck }
3225
5888
  });
@@ -3232,7 +5895,7 @@ var MongoMigrationRunner = class {
3232
5895
  const { command } = step;
3233
5896
  if (!(command instanceof CreateIndexCommand) || !isDuplicateKeyError(error)) throw error;
3234
5897
  const inPlace = await this.collectionInPlace(command.collection);
3235
- return indexBuildFailure(operation, command, error, (fields) => backfillStatements(options.destinationContract, command.collection, fields, inPlace.rewrite).map(({ statement }) => `--backfill ${statement}`));
5898
+ return indexBuildFailure(operation, command, error, (fields) => backfillStatements(options.destinationContract, command.collection, fields, /* @__PURE__ */ new Map(), inPlace.rewrite).map(({ statement }) => `--backfill ${statement}`));
3236
5899
  }
3237
5900
  wrote();
3238
5901
  }
@@ -3327,7 +5990,7 @@ var MongoMigrationRunner = class {
3327
5990
  if (runPrechecks && op.precheck.length > 0) {
3328
5991
  if (!await this.evaluateDmlChecks(op.precheck, filterEvaluator)) return {
3329
5992
  executed: false,
3330
- failure: renameOverwriteRefusal(op) ?? runnerFailure("MIGRATION.PRECHECK_FAILED", `Operation ${op.id} failed during precheck`, { meta: {
5993
+ failure: renameOverwriteRefusal(op) ?? storedKeyRenameRefusal(op) ?? await this.documentsHoldingRemovedValueRefusal(op, position) ?? runnerFailure("MIGRATION.PRECHECK_FAILED", `Operation ${op.id} failed during precheck`, { meta: {
3331
5994
  operationId: op.id,
3332
5995
  name: op.name
3333
5996
  } })
@@ -3368,10 +6031,70 @@ var MongoMigrationRunner = class {
3368
6031
  return { executed: true };
3369
6032
  }
3370
6033
  /**
6034
+ * The failure when a stored value removal's precheck finds documents whose required field or key
6035
+ * holds the value: how many, one `_id`, the query that finds them all, and how to keep them.
6036
+ */
6037
+ async documentsHoldingRemovedValueRefusal(op, position) {
6038
+ const rewrite = op.meta?.rewrite;
6039
+ const [check] = op.precheck;
6040
+ if (rewrite?.kind !== "removeStoredValue" || check === void 0) return void 0;
6041
+ const { command } = check.source;
6042
+ if (!(command instanceof RawAggregateCommand)) return void 0;
6043
+ const match = Reflect.get(command.pipeline[0] ?? {}, "$match");
6044
+ if (typeof match !== "object" || match === null) return void 0;
6045
+ const counted = await this.firstRow({
6046
+ ...check.source,
6047
+ command: new RawAggregateCommand(command.collection, [{ $match: match }, { $group: {
6048
+ _id: null,
6049
+ documents: { $sum: 1 },
6050
+ documentId: { $first: "$_id" }
6051
+ } }])
6052
+ });
6053
+ const documents = Number(counted?.["documents"] ?? 0);
6054
+ if (documents === 0) return void 0;
6055
+ const one = documents === 1;
6056
+ const { collection, field, path, value, enumValue, required, filter } = rewrite;
6057
+ const place = path === void 0 ? `in field ${field}` : `at path ${JSON.stringify(path)} in field ${field}`;
6058
+ const query = `db.getCollection(${JSON.stringify(collection)}).find(${JSON.stringify(match)})`;
6059
+ const coordinate = enumValue === void 0 ? void 0 : {
6060
+ ...ifDefined("namespaceId", enumValue.namespaceId === void 0 ? void 0 : asNamespaceId(enumValue.namespaceId)),
6061
+ enum: enumValue.enum,
6062
+ member: enumValue.member
6063
+ };
6064
+ const named = coordinate === void 0 ? "the enum value" : `enum value "${enumValueStatementText(position.contract, enumValueCoordinateFromJson(coordinate))}"`;
6065
+ return runnerFailure("MIGRATION.PRECHECK_FAILED", `Operation ${op.id} stopped: ${documents} ${one ? "document" : "documents"} in collection ${collection} ${one ? "holds" : "hold"} value ${JSON.stringify(value)} ${place}, which is required`, {
6066
+ why: [
6067
+ `${one ? "It has" : "One of them has"} _id ${mongoshValue(counted?.["documentId"])}. Find them all with ${query} in mongosh.`,
6068
+ `To keep them, add a new member to the enum and rename ${named} to it instead of removing it, or change those documents to another value first.`,
6069
+ "Once the documents are fixed, run the command again; operations that already ran are skipped."
6070
+ ].join(" "),
6071
+ meta: {
6072
+ operationId: op.id,
6073
+ name: op.name,
6074
+ ...coordinate === void 0 || typeof value === "boolean" ? {} : { rowsHoldingValue: [{
6075
+ enumValue: coordinate,
6076
+ value,
6077
+ rows: documents,
6078
+ operationIndex: position.operationIndex,
6079
+ keepDataEdit: keepRemovedValueEdit({
6080
+ collection,
6081
+ field,
6082
+ ...ifDefined("path", path),
6083
+ ...ifDefined("filter", filter),
6084
+ value,
6085
+ ...ifDefined("required", required),
6086
+ ...ifDefined("enumValue", enumValue)
6087
+ })
6088
+ }] }
6089
+ }
6090
+ });
6091
+ }
6092
+ /**
3371
6093
  * The failure when a validator change's precheck finds documents the new validator rejects: how
3372
6094
  * many, one `_id`, and the query that finds them all.
3373
6095
  */
3374
- async documentsFailingValidatorRefusal(op, failed, contract) {
6096
+ async documentsFailingValidatorRefusal(op, failed, contract, position) {
6097
+ const { operationIndex, plan } = position;
3375
6098
  if (isInspectionCheckSource(failed.source)) return void 0;
3376
6099
  const collMod = validatorSetBy(op);
3377
6100
  if (collMod?.validator === void 0) return void 0;
@@ -3401,14 +6124,29 @@ var MongoMigrationRunner = class {
3401
6124
  collection,
3402
6125
  id: documentObjectId
3403
6126
  }, fields, shape, "");
6127
+ const nested = nestedFindings(touched, fields);
6128
+ const shownProblems = [...problems, ...[...nested.lacking.map((key) => `has no ${JSON.stringify(key)}`), ...nested.notObjects.map((field) => `holds an element in ${JSON.stringify(field)} that is not an object`)].filter((text) => !problems.some((problem) => problem.text === text)).map((text) => ({
6129
+ text,
6130
+ uncheckable: false
6131
+ }))];
3404
6132
  const hadValidator = typeof validatorBefore === "object" && validatorBefore !== null;
3405
6133
  const found = [
3406
- `${touchedSentence(touched)}${one ? "It has" : "One of them has"} _id ${documentId}.`,
3407
- ...problems.length === 0 ? [] : [`That document ${joinedList(problems.map(({ text }) => text))}.`],
6134
+ `${touchedSentence(touched, fields)}${one ? "It has" : "One of them has"} _id ${documentId}.`,
6135
+ ...shownProblems.length === 0 ? [] : [`That document ${joinedList(shownProblems.map(({ text }) => text))}.`],
3408
6136
  `Find them all with ${query} in mongosh.`
3409
6137
  ];
3410
6138
  const uncheckable = problems.length > 0 && problems.every((problem) => problem.uncheckable);
3411
- const advice = uncheckable ? ["A document that holds such a field can never fit the validator, so changing the documents does not help: store the field under a name without \".\" that does not start with \"$\"."] : hadValidator ? [...backfillAdvice(contract, collection, await this.fieldsSomeDocumentLacks(failed.source, collection, failing, touched.requires), inPlace.rewrite), byHandAdvice(collection)] : ["Change them with a dataTransform in migration.ts, or with an update in mongosh."];
6139
+ const backfills = uncheckable || !hadValidator ? {
6140
+ sentences: [],
6141
+ rowsWithoutValue: []
6142
+ } : backfillAdvice(contract, {
6143
+ collection,
6144
+ documents: await this.documentsLacking(failed.source, collection, failing, touched)
6145
+ }, touched, inPlace.rewrite, {
6146
+ operationIndex,
6147
+ renamed: renamedInto(plan, collection)
6148
+ });
6149
+ const advice = uncheckable ? ["A document that holds such a field can never fit the validator, so changing the documents does not help: store the field under a name without \".\" that does not start with \"$\"."] : hadValidator ? [...backfills.sentences, byHandAdvice(collection)] : ["Change them with a dataTransform in migration.ts, or with an update in mongosh."];
3412
6150
  const why = [
3413
6151
  ...found,
3414
6152
  ...advice,
@@ -3427,7 +6165,8 @@ var MongoMigrationRunner = class {
3427
6165
  query,
3428
6166
  requires: touched.requires,
3429
6167
  retypes: touched.retypes
3430
- }
6168
+ },
6169
+ ...backfills.rowsWithoutValue.length === 0 ? {} : { rowsWithoutValue: backfills.rowsWithoutValue }
3431
6170
  }
3432
6171
  });
3433
6172
  }
@@ -3500,12 +6239,25 @@ var MongoMigrationRunner = class {
3500
6239
  }
3501
6240
  };
3502
6241
  }
3503
- /** Each of `fields` that a document matching `failing` lacks or holds as `null`. */
3504
- async fieldsSomeDocumentLacks(source, collection, failing, fields) {
3505
- return (await Promise.all(fields.map(async (field) => unrewritableFieldNameReason(field) === void 0 && await this.firstRow({
3506
- ...source,
3507
- command: new RawAggregateCommand(collection, [{ $match: { $and: [failing, { [field]: null }] } }, { $limit: 1 }])
3508
- }) !== void 0 ? [field] : []))).flat();
6242
+ /** Each field or nested key the change makes required that documents matching `failing` lack or hold as `null`, with how many do; a nested key counts only in an object that the document holds. */
6243
+ async documentsLacking(source, collection, failing, touched) {
6244
+ const lackingFilter = (field) => {
6245
+ const key = touched.keys.get(field);
6246
+ if (key === void 0) return unrewritableFieldNameReason(field) === void 0 ? { [field]: null } : void 0;
6247
+ const parsed = storedKeyPath(key.field, key.path);
6248
+ return parsed.ok ? documentsWithObject(parsed.value, (object) => lacksKey(object, parsed.value.key)) : void 0;
6249
+ };
6250
+ const lacking = await Promise.all(touched.requires.map(async (field) => {
6251
+ const filter = lackingFilter(field);
6252
+ if (filter === void 0) return [];
6253
+ const counted = await this.firstRow({
6254
+ ...source,
6255
+ command: new RawAggregateCommand(collection, [{ $match: { $and: [failing, filter] } }, { $count: "documents" }])
6256
+ });
6257
+ const documents = Number(counted?.["documents"] ?? 0);
6258
+ return documents === 0 ? [] : [[field, documents]];
6259
+ }));
6260
+ return new Map(lacking.flat());
3509
6261
  }
3510
6262
  /**
3511
6263
  * Each field of the document with `_id`, at `input`, `$$ROOT` or the path of an embedded document:
@@ -3755,4 +6507,4 @@ const mongoTargetDescriptor = {
3755
6507
  //#endregion
3756
6508
  export { schemaIndexToCreateIndexOptions as C, schemaCollectionToCreateCollectionOptions as S, deserializeMongoOp as _, DropIndexCall as a, renderCallsToTypeScript as b, MongoMigrationRunner as c, MongoTargetSchemaVerifier as d, MongoTargetUnboundDatabase as f, UnsetFieldCall as g, RenameFieldCall as h, DropCollectionCall as i, MongoTargetContractSerializer as l, RenameCollectionCall as m, CreateCollectionCall as n, FilterEvaluator as o, PlannerProducedMongoMigration as p, CreateIndexCall as r, MongoMigrationPlanner as s, CollModCall as t, MongoTargetDatabase as u, deserializeMongoOps as v, serializeMongoOps as w, renderOps as x, mongoTargetDescriptor as y };
3757
6509
 
3758
- //# sourceMappingURL=control-BlMckWwK.mjs.map
6510
+ //# sourceMappingURL=control-ADZk3Q1S.mjs.map