-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmigration-chain.test.ts
More file actions
263 lines (243 loc) · 9.1 KB
/
Copy pathmigration-chain.test.ts
File metadata and controls
263 lines (243 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { readFileSync } from 'node:fs';
import {
createMongoRunnerDeps,
extractDb,
introspectSchema,
} from '@prisma-next/adapter-mongo/control';
import { MongoDriverImpl } from '@prisma-next/driver-mongo';
import mongoControlDriver from '@prisma-next/driver-mongo/control';
import { createMongoFamilyInstance } from '@prisma-next/family-mongo/control';
import { verifyMongoSchema } from '@prisma-next/family-mongo/schema-verify';
import type { MongoContract } from '@prisma-next/mongo-contract';
import { MongoMigrationRunner } from '@prisma-next/target-mongo/control';
import { timeouts } from '@prisma-next/test-utils';
import { type Db, MongoClient } from 'mongodb';
import { MongoMemoryReplSet } from 'mongodb-memory-server';
import { resolve } from 'pathe';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const ALL_POLICY = {
allowedOperationClasses: ['additive', 'widening', 'destructive', 'data'] as const,
};
const MIG1_DIR = '20260513T0505_initial';
const MIG2_DIR = '20260513T0507_add_product_category_index';
const MIG3_DIR = '20260513T0508_backfill_product_status';
const UNBOUND_NAMESPACE_ID = '__unbound__' as const;
type FlatMongoStorage = {
storageHash: string;
collections: Record<string, unknown>;
};
function namespacedMongoContract(contract: MongoContract): MongoContract {
const storage = contract.storage;
if ('namespaces' in storage && storage.namespaces != null) {
return contract;
}
if (!('collections' in storage)) {
return contract;
}
const { collections, storageHash, ...rest } = storage as FlatMongoStorage;
// Test-only rewrap of a legacy on-disk end-contract.json whose storageHash
// is a plain string. MongoContract's storage carries a branded StorageHash;
// the brand is purely a type-level marker and the runtime payload is
// identical, so a structural rewrap is safe here.
return {
...contract,
storage: {
...rest,
storageHash,
namespaces: {
[UNBOUND_NAMESPACE_ID]: {
id: UNBOUND_NAMESPACE_ID,
kind: 'mongo-namespace' as const,
collections,
},
},
},
} as unknown as MongoContract;
}
function loadMigration(dirName: string): {
ops: ReturnType<typeof JSON.parse>;
endContract: MongoContract;
} {
const dir = resolve(import.meta.dirname, '../migrations/app', dirName);
const ops = JSON.parse(readFileSync(resolve(dir, 'ops.json'), 'utf8'));
const endContract = namespacedMongoContract(
JSON.parse(readFileSync(resolve(dir, 'end-contract.json'), 'utf8')) as MongoContract,
);
return { ops, endContract };
}
function makeFamily(): ReturnType<typeof createMongoFamilyInstance> {
// ControlStack arg is unused by the mongo factory; an empty object suffices.
return createMongoFamilyInstance(
{} as unknown as Parameters<typeof createMongoFamilyInstance>[0],
);
}
describe('full retail-store migration chain (m1 → m2 → m3)', {
timeout: timeouts.spinUpMongoMemoryServer,
}, () => {
let replSet: MongoMemoryReplSet;
let client: MongoClient;
let db: Db;
const dbName = 'migration_chain_test';
beforeAll(async () => {
replSet = await MongoMemoryReplSet.create({
instanceOpts: [
{ launchTimeout: timeouts.spinUpMongoMemoryServer, storageEngine: 'wiredTiger' },
],
replSet: { count: 1, storageEngine: 'wiredTiger' },
});
client = new MongoClient(replSet.getUri());
await client.connect();
db = client.db(dbName);
}, timeouts.spinUpMongoMemoryServer);
beforeEach(async () => {
await db.dropDatabase();
});
afterAll(async () => {
try {
await client?.close();
await replSet?.stop();
} catch {
// ignore cleanup errors
}
}, timeouts.spinUpMongoMemoryServer);
it('applies all three migrations, post-apply schema satisfies the contract, and pre-existing products are backfilled', async () => {
const m1 = loadMigration(MIG1_DIR);
const m2 = loadMigration(MIG2_DIR);
const m3 = loadMigration(MIG3_DIR);
const controlDriver = await mongoControlDriver.create(replSet.getUri(dbName));
try {
const runner = new MongoMigrationRunner(
createMongoRunnerDeps(
controlDriver,
MongoDriverImpl.fromDb(extractDb(controlDriver)),
makeFamily(),
),
);
// Migration 1 — bootstrap. No origin (greenfield); strict verify on.
const r1 = await runner.execute({
plan: {
targetId: 'mongo',
destination: { storageHash: m1.endContract.storage.storageHash },
operations: m1.ops,
},
destinationContract: m1.endContract,
policy: ALL_POLICY,
frameworkComponents: [],
strictVerification: true,
migrationEdges: [
{
migrationHash: m1.endContract.storage.storageHash,
dirName: MIG1_DIR,
from: '',
to: m1.endContract.storage.storageHash,
operationCount: m1.ops.length,
},
],
});
expect(r1.ok, `m1 failed: ${JSON.stringify(r1)}`).toBe(true);
// Migration 2 — add a non-validator index.
const r2 = await runner.execute({
plan: {
targetId: 'mongo',
origin: { storageHash: m1.endContract.storage.storageHash },
destination: { storageHash: m2.endContract.storage.storageHash },
operations: m2.ops,
},
destinationContract: m2.endContract,
policy: ALL_POLICY,
frameworkComponents: [],
strictVerification: true,
migrationEdges: [
{
migrationHash: m2.endContract.storage.storageHash,
dirName: MIG2_DIR,
from: m1.endContract.storage.storageHash,
to: m2.endContract.storage.storageHash,
operationCount: m2.ops.length,
},
],
});
expect(r2.ok, `m2 failed: ${JSON.stringify(r2)}`).toBe(true);
// Seed two pre-existing products that pre-date the `status` field.
// These conform to the state-2 strict validator (which doesn't yet
// include `status`); migration 3 adds `status` to the validator and
// backfills the value.
await db.collection('products').insertMany([
{
name: 'Pre-existing widget A',
brand: 'Acme',
code: 'A001',
description: 'A widget that pre-dates the status field',
primaryCategory: 'Apparel',
subCategory: 'Topwear',
articleType: 'T-Shirts',
price: { amount: 10.99, currency: 'USD' },
image: { url: 'http://example.com/a.png' },
},
{
name: 'Pre-existing widget B',
brand: 'Acme',
code: 'B001',
description: 'Another pre-existing widget',
primaryCategory: 'Apparel',
subCategory: 'Bottomwear',
articleType: 'Trousers',
price: { amount: 20.99, currency: 'USD' },
image: { url: 'http://example.com/b.png' },
},
]);
// Migration 3 — refresh validator + dataTransform backfill.
const r3 = await runner.execute({
plan: {
targetId: 'mongo',
origin: { storageHash: m2.endContract.storage.storageHash },
destination: { storageHash: m3.endContract.storage.storageHash },
operations: m3.ops,
},
destinationContract: m3.endContract,
policy: ALL_POLICY,
frameworkComponents: [],
strictVerification: true,
migrationEdges: [
{
migrationHash: m3.endContract.storage.storageHash,
dirName: MIG3_DIR,
from: m2.endContract.storage.storageHash,
to: m3.endContract.storage.storageHash,
operationCount: m3.ops.length,
},
],
});
expect(r3.ok, `m3 failed: ${JSON.stringify(r3)}`).toBe(true);
// Independent verify call: introspect the live DB and diff against
// the state-3 contract. Belt-and-braces — the runner already verified
// post-apply (gating its marker advance on it), but this re-run
// makes the assertion explicit at the test layer and would catch
// any future drift between runner-internal verify and stand-alone
// `verifyMongoSchema`.
const liveSchema = await introspectSchema(extractDb(controlDriver));
const verifyResult = verifyMongoSchema({
contract: m3.endContract,
schema: liveSchema,
strict: true,
frameworkComponents: [],
});
expect(verifyResult.ok, `post-chain verify failed: ${JSON.stringify(verifyResult)}`).toBe(
true,
);
// Marker should have advanced to the state-3 hash via migration 3.
const marker = await db
.collection<{ _id: string; storageHash: string }>('_prisma_migrations')
.findOne({ _id: 'app' });
expect(marker?.storageHash).toBe(m3.endContract.storage.storageHash);
// Backfill: pre-existing products now carry `status: 'active'`.
const products = await db.collection('products').find({}).toArray();
expect(products).toHaveLength(2);
for (const p of products) {
expect(p['status']).toBe('active');
}
} finally {
await controlDriver.close();
}
});
});