@verdant-web/store 3.7.0 → 3.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +650 -21
- package/dist/bundle/index.js +12 -12
- package/dist/bundle/index.js.map +4 -4
- package/dist/esm/DocumentManager.d.ts +1 -0
- package/dist/esm/DocumentManager.js +8 -3
- package/dist/esm/DocumentManager.js.map +1 -1
- package/dist/esm/__tests__/documents.test.js +16 -0
- package/dist/esm/__tests__/documents.test.js.map +1 -1
- package/dist/esm/client/Client.d.ts +3 -3
- package/dist/esm/client/Client.js +5 -5
- package/dist/esm/client/Client.js.map +1 -1
- package/dist/esm/client/ClientDescriptor.d.ts +1 -0
- package/dist/esm/client/ClientDescriptor.js +6 -3
- package/dist/esm/client/ClientDescriptor.js.map +1 -1
- package/dist/esm/context.d.ts +1 -0
- package/dist/esm/entities/EntityStore.d.ts +3 -3
- package/dist/esm/entities/EntityStore.js +5 -0
- package/dist/esm/entities/EntityStore.js.map +1 -1
- package/dist/esm/idb.d.ts +6 -0
- package/dist/esm/idb.js +17 -1
- package/dist/esm/idb.js.map +1 -1
- package/dist/esm/migration/db.d.ts +9 -3
- package/dist/esm/migration/db.js +23 -11
- package/dist/esm/migration/db.js.map +1 -1
- package/dist/esm/migration/engine.d.ts +15 -0
- package/dist/esm/migration/engine.js +159 -0
- package/dist/esm/migration/engine.js.map +1 -0
- package/dist/esm/migration/migrations.d.ts +17 -0
- package/dist/esm/migration/migrations.js +242 -0
- package/dist/esm/migration/migrations.js.map +1 -0
- package/dist/esm/migration/openQueryDatabase.d.ts +10 -0
- package/dist/esm/migration/openQueryDatabase.js +27 -0
- package/dist/esm/migration/openQueryDatabase.js.map +1 -0
- package/dist/esm/migration/openWIPDatabase.d.ts +11 -0
- package/dist/esm/migration/openWIPDatabase.js +65 -0
- package/dist/esm/migration/openWIPDatabase.js.map +1 -0
- package/dist/esm/migration/types.d.ts +3 -0
- package/dist/esm/migration/types.js +2 -0
- package/dist/esm/migration/types.js.map +1 -0
- package/dist/esm/queries/QueryableStorage.js +1 -1
- package/dist/esm/queries/QueryableStorage.js.map +1 -1
- package/dist/esm/sync/PushPullSync.d.ts +3 -2
- package/dist/esm/sync/PushPullSync.js +7 -1
- package/dist/esm/sync/PushPullSync.js.map +1 -1
- package/package.json +4 -3
- package/src/DocumentManager.ts +11 -2
- package/src/__tests__/documents.test.ts +19 -0
- package/src/client/Client.ts +6 -8
- package/src/client/ClientDescriptor.ts +7 -6
- package/src/context.ts +1 -0
- package/src/entities/EntityStore.ts +8 -2
- package/src/idb.ts +20 -1
- package/src/migration/db.ts +62 -20
- package/src/migration/engine.ts +248 -0
- package/src/migration/migrations.ts +347 -0
- package/src/migration/openQueryDatabase.ts +63 -0
- package/src/migration/openWIPDatabase.ts +97 -0
- package/src/migration/types.ts +4 -0
- package/src/queries/QueryableStorage.ts +1 -1
- package/src/sync/PushPullSync.ts +10 -0
- package/dist/esm/migration/openDatabase.d.ts +0 -20
- package/dist/esm/migration/openDatabase.js +0 -463
- package/dist/esm/migration/openDatabase.js.map +0 -1
- package/src/migration/openDatabase.ts +0 -749
|
@@ -1,749 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CollectionFilter,
|
|
3
|
-
Migration,
|
|
4
|
-
MigrationEngine,
|
|
5
|
-
ObjectIdentifier,
|
|
6
|
-
StorageSchema,
|
|
7
|
-
addFieldDefaults,
|
|
8
|
-
assert,
|
|
9
|
-
assignIndexValues,
|
|
10
|
-
assignOidPropertiesToAllSubObjects,
|
|
11
|
-
assignOidsToAllSubObjects,
|
|
12
|
-
cloneDeep,
|
|
13
|
-
createOid,
|
|
14
|
-
decomposeOid,
|
|
15
|
-
diffToPatches,
|
|
16
|
-
getIndexValues,
|
|
17
|
-
getOid,
|
|
18
|
-
getOidRoot,
|
|
19
|
-
hasOid,
|
|
20
|
-
initialToPatches,
|
|
21
|
-
removeOidPropertiesFromAllSubObjects,
|
|
22
|
-
} from '@verdant-web/common';
|
|
23
|
-
import { Context } from '../context.js';
|
|
24
|
-
import { storeRequestPromise } from '../idb.js';
|
|
25
|
-
import { Metadata } from '../metadata/Metadata.js';
|
|
26
|
-
import { ClientOperation } from '../metadata/OperationsStore.js';
|
|
27
|
-
import { findAllOids, findOneOid } from '../queries/dbQueries.js';
|
|
28
|
-
import {
|
|
29
|
-
acquireLock,
|
|
30
|
-
closeDatabase,
|
|
31
|
-
getDatabaseVersion,
|
|
32
|
-
openDatabase,
|
|
33
|
-
upgradeDatabase,
|
|
34
|
-
} from './db.js';
|
|
35
|
-
import { getMigrationPath } from './paths.js';
|
|
36
|
-
|
|
37
|
-
const globalIDB =
|
|
38
|
-
typeof window !== 'undefined' ? window.indexedDB : (undefined as any);
|
|
39
|
-
|
|
40
|
-
type OpenDocumentDbContext = Omit<Context, 'documentDb'>;
|
|
41
|
-
|
|
42
|
-
export async function openDocumentDatabase({
|
|
43
|
-
version,
|
|
44
|
-
indexedDB = globalIDB,
|
|
45
|
-
migrations,
|
|
46
|
-
meta,
|
|
47
|
-
context,
|
|
48
|
-
}: {
|
|
49
|
-
version: number;
|
|
50
|
-
migrations: Migration<any>[];
|
|
51
|
-
indexedDB?: IDBFactory;
|
|
52
|
-
meta: Metadata;
|
|
53
|
-
context: OpenDocumentDbContext;
|
|
54
|
-
}) {
|
|
55
|
-
if (context.schema.wip) {
|
|
56
|
-
throw new Error('Cannot open a production client with a WIP schema!');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const currentVersion = await getDatabaseVersion(
|
|
60
|
-
indexedDB,
|
|
61
|
-
context.namespace,
|
|
62
|
-
version,
|
|
63
|
-
context.log,
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
context.log(
|
|
67
|
-
'debug',
|
|
68
|
-
'Current database version:',
|
|
69
|
-
currentVersion,
|
|
70
|
-
'target version:',
|
|
71
|
-
version,
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
const toRun = getMigrationPath({
|
|
75
|
-
currentVersion,
|
|
76
|
-
targetVersion: version,
|
|
77
|
-
migrations,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
if (toRun.length > 0) {
|
|
81
|
-
context.log(
|
|
82
|
-
'debug',
|
|
83
|
-
'Migrations to run:',
|
|
84
|
-
toRun.map((m) => m.version),
|
|
85
|
-
);
|
|
86
|
-
await runMigrations({ context, toRun, meta, indexedDB });
|
|
87
|
-
}
|
|
88
|
-
return openDatabase(indexedDB, context.namespace, version, context.log);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export async function openWIPDocumentDatabase({
|
|
92
|
-
version,
|
|
93
|
-
indexedDB = globalIDB,
|
|
94
|
-
migrations,
|
|
95
|
-
meta,
|
|
96
|
-
context,
|
|
97
|
-
wipNamespace,
|
|
98
|
-
}: {
|
|
99
|
-
version: number;
|
|
100
|
-
migrations: Migration<any>[];
|
|
101
|
-
indexedDB?: IDBFactory;
|
|
102
|
-
meta: Metadata;
|
|
103
|
-
context: OpenDocumentDbContext;
|
|
104
|
-
wipNamespace: string;
|
|
105
|
-
}) {
|
|
106
|
-
context.log('debug', 'Opening WIP database', wipNamespace);
|
|
107
|
-
const currentWIPVersion = await getDatabaseVersion(
|
|
108
|
-
indexedDB,
|
|
109
|
-
wipNamespace,
|
|
110
|
-
version,
|
|
111
|
-
context.log,
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
if (currentWIPVersion === version) {
|
|
115
|
-
context.log('info', `WIP schema is up-to-date; not refreshing database`);
|
|
116
|
-
} else {
|
|
117
|
-
context.log('info', `WIP schema is out-of-date; refreshing database`);
|
|
118
|
-
|
|
119
|
-
// first we need to copy the data from the production database to the WIP database
|
|
120
|
-
// at the current (non-wip) version.
|
|
121
|
-
|
|
122
|
-
const initialToRun = getMigrationPath({
|
|
123
|
-
currentVersion: currentWIPVersion,
|
|
124
|
-
targetVersion: version - 1,
|
|
125
|
-
migrations,
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
if (initialToRun.length > 0) {
|
|
129
|
-
await runMigrations({
|
|
130
|
-
context,
|
|
131
|
-
toRun: initialToRun,
|
|
132
|
-
meta,
|
|
133
|
-
indexedDB,
|
|
134
|
-
namespace: wipNamespace,
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// now, we copy the data from the main database.
|
|
138
|
-
const mainDatabase = await openDatabase(
|
|
139
|
-
indexedDB,
|
|
140
|
-
context.namespace,
|
|
141
|
-
version - 1,
|
|
142
|
-
context.log,
|
|
143
|
-
);
|
|
144
|
-
|
|
145
|
-
const wipDatabase = await openDatabase(
|
|
146
|
-
indexedDB,
|
|
147
|
-
wipNamespace,
|
|
148
|
-
version - 1,
|
|
149
|
-
context.log,
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
// DOMStringList... doesn't have iterable... why
|
|
153
|
-
const mainDatabaseStoreNames = new Array<string>();
|
|
154
|
-
for (let i = 0; i < mainDatabase.objectStoreNames.length; i++) {
|
|
155
|
-
mainDatabaseStoreNames.push(mainDatabase.objectStoreNames[i]);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const copyFromTransaction = mainDatabase.transaction(
|
|
159
|
-
mainDatabaseStoreNames,
|
|
160
|
-
'readonly',
|
|
161
|
-
);
|
|
162
|
-
const copyFromStores = mainDatabaseStoreNames.map((name) =>
|
|
163
|
-
copyFromTransaction.objectStore(name),
|
|
164
|
-
);
|
|
165
|
-
const allObjects = await Promise.all(
|
|
166
|
-
copyFromStores.map((store) => storeRequestPromise(store.getAll())),
|
|
167
|
-
);
|
|
168
|
-
|
|
169
|
-
const copyToTransaction = wipDatabase.transaction(
|
|
170
|
-
mainDatabaseStoreNames,
|
|
171
|
-
'readwrite',
|
|
172
|
-
);
|
|
173
|
-
const copyToStores = mainDatabaseStoreNames.map((name) =>
|
|
174
|
-
copyToTransaction.objectStore(name),
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
for (let i = 0; i < copyToStores.length; i++) {
|
|
178
|
-
await Promise.all(
|
|
179
|
-
allObjects[i].map((obj) => {
|
|
180
|
-
return storeRequestPromise(copyToStores[i].put(obj));
|
|
181
|
-
}),
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const toRun = getMigrationPath({
|
|
187
|
-
currentVersion: version - 1,
|
|
188
|
-
targetVersion: version,
|
|
189
|
-
migrations,
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
if (toRun.length > 0) {
|
|
193
|
-
await runMigrations({
|
|
194
|
-
context,
|
|
195
|
-
toRun,
|
|
196
|
-
meta,
|
|
197
|
-
indexedDB,
|
|
198
|
-
namespace: wipNamespace,
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return openDatabase(indexedDB, wipNamespace, version, context.log);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async function runMigrations({
|
|
207
|
-
context,
|
|
208
|
-
toRun,
|
|
209
|
-
meta,
|
|
210
|
-
indexedDB = globalIDB,
|
|
211
|
-
namespace = context.namespace,
|
|
212
|
-
}: {
|
|
213
|
-
context: OpenDocumentDbContext;
|
|
214
|
-
toRun: Migration<any>[];
|
|
215
|
-
meta: Metadata;
|
|
216
|
-
indexedDB?: IDBFactory;
|
|
217
|
-
namespace?: string;
|
|
218
|
-
}) {
|
|
219
|
-
await acquireLock(namespace, async () => {
|
|
220
|
-
// now the fun part
|
|
221
|
-
for (const migration of toRun) {
|
|
222
|
-
// special case: if this is the version 1 migration, we have no pre-existing database
|
|
223
|
-
// to use for the migration.
|
|
224
|
-
let engine: MigrationEngine;
|
|
225
|
-
// migrations from 0 (i.e. initial migrations) don't attempt to open an existing db
|
|
226
|
-
if (migration.oldSchema.version === 0) {
|
|
227
|
-
engine = getInitialMigrationEngine({
|
|
228
|
-
meta,
|
|
229
|
-
migration,
|
|
230
|
-
context,
|
|
231
|
-
});
|
|
232
|
-
await migration.migrate(engine);
|
|
233
|
-
} else {
|
|
234
|
-
// open the database with the current (old) version for this migration. this should
|
|
235
|
-
// align with the database's current version.
|
|
236
|
-
const originalDatabase = await openDatabase(
|
|
237
|
-
indexedDB,
|
|
238
|
-
namespace,
|
|
239
|
-
migration.oldSchema.version,
|
|
240
|
-
context.log,
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
// this will only write to our metadata store via operations!
|
|
244
|
-
engine = getMigrationEngine({
|
|
245
|
-
meta,
|
|
246
|
-
migration,
|
|
247
|
-
context: {
|
|
248
|
-
...context,
|
|
249
|
-
documentDb: originalDatabase,
|
|
250
|
-
},
|
|
251
|
-
});
|
|
252
|
-
try {
|
|
253
|
-
await migration.migrate(engine);
|
|
254
|
-
// wait on any out-of-band async operations to complete
|
|
255
|
-
await Promise.all(engine.awaitables);
|
|
256
|
-
} catch (err) {
|
|
257
|
-
context.log(
|
|
258
|
-
'critical',
|
|
259
|
-
`Migration failed (${migration.oldSchema.version} -> ${migration.newSchema.version})`,
|
|
260
|
-
err,
|
|
261
|
-
);
|
|
262
|
-
if (err instanceof Error) {
|
|
263
|
-
throw err;
|
|
264
|
-
} else {
|
|
265
|
-
throw new Error('Unknown error during migration');
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// now we have to open the database again with the next version and
|
|
270
|
-
// make the appropriate schema changes during the upgrade.
|
|
271
|
-
await closeDatabase(originalDatabase);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
context.log(
|
|
275
|
-
'debug',
|
|
276
|
-
'Upgrading database',
|
|
277
|
-
namespace,
|
|
278
|
-
'to version',
|
|
279
|
-
migration.newSchema.version,
|
|
280
|
-
);
|
|
281
|
-
await upgradeDatabase(
|
|
282
|
-
indexedDB,
|
|
283
|
-
namespace,
|
|
284
|
-
migration.newSchema.version,
|
|
285
|
-
(transaction, db) => {
|
|
286
|
-
for (const newCollection of migration.addedCollections) {
|
|
287
|
-
db.createObjectStore(newCollection, {
|
|
288
|
-
keyPath:
|
|
289
|
-
migration.newSchema.collections[newCollection].primaryKey,
|
|
290
|
-
autoIncrement: false,
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
for (const collection of migration.allCollections) {
|
|
295
|
-
const store = transaction.objectStore(collection);
|
|
296
|
-
// apply new indexes
|
|
297
|
-
for (const newIndex of migration.addedIndexes[collection] || []) {
|
|
298
|
-
store.createIndex(newIndex.name, newIndex.name, {
|
|
299
|
-
multiEntry: newIndex.multiEntry,
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
// remove old indexes
|
|
303
|
-
for (const oldIndex of migration.removedIndexes[collection] || []) {
|
|
304
|
-
store.deleteIndex(oldIndex.name);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
for (const removedCollection of migration.removedCollections) {
|
|
308
|
-
// !! can't delete the store, because old operations that relate to
|
|
309
|
-
// this store may still exist in history. instead, we can clear it out
|
|
310
|
-
// and leave it in place
|
|
311
|
-
transaction.objectStore(removedCollection).clear();
|
|
312
|
-
}
|
|
313
|
-
},
|
|
314
|
-
context.log,
|
|
315
|
-
);
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* In cases where operations from the future have been
|
|
319
|
-
* received by this client, we may have created entire
|
|
320
|
-
* documents in metadata which were not written to storage
|
|
321
|
-
* because all of their operations were in the future (
|
|
322
|
-
* i.e. in the next version). We have to find those documents
|
|
323
|
-
* and also write their snapshots to storage, because they
|
|
324
|
-
* won't be present in storage already to 'refresh,' so
|
|
325
|
-
* if we don't analyze metadata for 'future' operations like
|
|
326
|
-
* this, we won't know they exist.
|
|
327
|
-
*
|
|
328
|
-
* This led to behavior where the metadata would be properly
|
|
329
|
-
* synced, but after upgrading the app and migrating, items
|
|
330
|
-
* would be missing from findAll and findOne queries.
|
|
331
|
-
*/
|
|
332
|
-
const docsWithUnappliedMigrations = await getDocsWithUnappliedMigrations({
|
|
333
|
-
meta,
|
|
334
|
-
currentVersion: migration.oldSchema.version,
|
|
335
|
-
newVersion: migration.newSchema.version,
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
// once the schema is ready, we can write back the migrated documents
|
|
339
|
-
const upgradedDatabase = await openDatabase(
|
|
340
|
-
indexedDB,
|
|
341
|
-
namespace,
|
|
342
|
-
migration.newSchema.version,
|
|
343
|
-
context.log,
|
|
344
|
-
);
|
|
345
|
-
for (const collection of migration.allCollections) {
|
|
346
|
-
// first step is to read in all the keys we need to rewrite
|
|
347
|
-
const documentReadTransaction = upgradedDatabase.transaction(
|
|
348
|
-
collection,
|
|
349
|
-
'readwrite',
|
|
350
|
-
);
|
|
351
|
-
const readStore = documentReadTransaction.objectStore(collection);
|
|
352
|
-
const keys = await getAllKeys(readStore);
|
|
353
|
-
// map the keys to OIDs
|
|
354
|
-
const oids = keys.map((key) => createOid(collection, `${key}`));
|
|
355
|
-
oids.push(
|
|
356
|
-
...engine.newOids.filter((oid) => {
|
|
357
|
-
return decomposeOid(oid).collection === collection;
|
|
358
|
-
}),
|
|
359
|
-
...docsWithUnappliedMigrations.filter((oid) => {
|
|
360
|
-
return decomposeOid(oid).collection === collection;
|
|
361
|
-
}),
|
|
362
|
-
);
|
|
363
|
-
|
|
364
|
-
// add 'touch' operations to all root OIDs of all documents.
|
|
365
|
-
// this marks documents which have undergone a migration
|
|
366
|
-
// so that other clients know when they're working
|
|
367
|
-
// with unmigrated data - by seeing that there are no
|
|
368
|
-
// existing operations or baselines with a timestamp
|
|
369
|
-
// that matches the current version.
|
|
370
|
-
// UPDATE: no longer necessary now that pruning is a thing.
|
|
371
|
-
// await Promise.all(
|
|
372
|
-
// oids.map((oid) =>
|
|
373
|
-
// meta.insertLocalOperations([
|
|
374
|
-
// {
|
|
375
|
-
// oid,
|
|
376
|
-
// timestamp: meta.time.zero(migration.version),
|
|
377
|
-
// data: { op: 'touch' },
|
|
378
|
-
// },
|
|
379
|
-
// ]),
|
|
380
|
-
// ),
|
|
381
|
-
// );
|
|
382
|
-
|
|
383
|
-
const snapshots = await Promise.all(
|
|
384
|
-
oids.map(async (oid) => {
|
|
385
|
-
try {
|
|
386
|
-
const snap = await meta.getDocumentSnapshot(oid);
|
|
387
|
-
return [oid, snap];
|
|
388
|
-
} catch (e) {
|
|
389
|
-
// this seems to happen with baselines/ops which are not fully
|
|
390
|
-
// cleaned up after deletion?
|
|
391
|
-
context.log(
|
|
392
|
-
'error',
|
|
393
|
-
'Could not regenerate snapshot during migration for oid',
|
|
394
|
-
oid,
|
|
395
|
-
'this document will not be preserved',
|
|
396
|
-
e,
|
|
397
|
-
);
|
|
398
|
-
return null;
|
|
399
|
-
}
|
|
400
|
-
}),
|
|
401
|
-
);
|
|
402
|
-
|
|
403
|
-
const views = snapshots
|
|
404
|
-
.filter((s): s is [string, any] => !!s)
|
|
405
|
-
.map(([oid, snapshot]) => {
|
|
406
|
-
if (!snapshot) return [oid, undefined];
|
|
407
|
-
const view = getIndexValues(
|
|
408
|
-
migration.newSchema.collections[collection],
|
|
409
|
-
snapshot,
|
|
410
|
-
);
|
|
411
|
-
return [oid, view];
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
// now we can write the documents back
|
|
415
|
-
const documentWriteTransaction = upgradedDatabase.transaction(
|
|
416
|
-
collection,
|
|
417
|
-
'readwrite',
|
|
418
|
-
);
|
|
419
|
-
const writeStore = documentWriteTransaction.objectStore(collection);
|
|
420
|
-
await Promise.all(
|
|
421
|
-
views.map(([oid, view]) => {
|
|
422
|
-
if (view) {
|
|
423
|
-
return putView(writeStore, view);
|
|
424
|
-
} else {
|
|
425
|
-
const { id } = decomposeOid(oid);
|
|
426
|
-
return deleteView(writeStore, id);
|
|
427
|
-
}
|
|
428
|
-
}),
|
|
429
|
-
);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
await closeDatabase(upgradedDatabase);
|
|
433
|
-
|
|
434
|
-
context.log('debug', `Migration of ${namespace} complete.`);
|
|
435
|
-
context.log(`
|
|
436
|
-
⬆️ v${migration.newSchema.version} Migration complete. Here's the rundown:
|
|
437
|
-
- Added collections: ${migration.addedCollections.join(', ')}
|
|
438
|
-
- Removed collections: ${migration.removedCollections.join(', ')}
|
|
439
|
-
- Changed collections: ${migration.changedCollections.join(', ')}
|
|
440
|
-
- New indexes: ${Object.keys(migration.addedIndexes)
|
|
441
|
-
.map((col) =>
|
|
442
|
-
migration.addedIndexes[col].map((i) => `${col}.${i.name}`),
|
|
443
|
-
)
|
|
444
|
-
.flatMap((i) => i)
|
|
445
|
-
.join(', ')}
|
|
446
|
-
- Removed indexes: ${Object.keys(migration.removedIndexes)
|
|
447
|
-
.map((col) =>
|
|
448
|
-
migration.removedIndexes[col].map((i) => `${col}.${i.name}`),
|
|
449
|
-
)
|
|
450
|
-
.flatMap((i) => i)
|
|
451
|
-
.join(', ')}
|
|
452
|
-
`);
|
|
453
|
-
}
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
function getMigrationMutations({
|
|
458
|
-
migration,
|
|
459
|
-
meta,
|
|
460
|
-
getMigrationNow,
|
|
461
|
-
newOids,
|
|
462
|
-
}: {
|
|
463
|
-
migration: Migration<any>;
|
|
464
|
-
newOids: string[];
|
|
465
|
-
getMigrationNow: () => string;
|
|
466
|
-
meta: Metadata;
|
|
467
|
-
}) {
|
|
468
|
-
return migration.allCollections.reduce((acc, collectionName) => {
|
|
469
|
-
acc[collectionName] = {
|
|
470
|
-
put: async (doc: any) => {
|
|
471
|
-
// add defaults
|
|
472
|
-
addFieldDefaults(migration.newSchema.collections[collectionName], doc);
|
|
473
|
-
const primaryKey =
|
|
474
|
-
doc[migration.newSchema.collections[collectionName].primaryKey];
|
|
475
|
-
const oid = createOid(collectionName, primaryKey);
|
|
476
|
-
newOids.push(oid);
|
|
477
|
-
await meta.insertLocalOperations(
|
|
478
|
-
initialToPatches(doc, oid, getMigrationNow),
|
|
479
|
-
);
|
|
480
|
-
return doc;
|
|
481
|
-
},
|
|
482
|
-
delete: async (id: string) => {
|
|
483
|
-
const rootOid = createOid(collectionName, id);
|
|
484
|
-
const allOids = await meta.getAllDocumentRelatedOids(rootOid);
|
|
485
|
-
return meta.insertLocalOperations(
|
|
486
|
-
allOids.map((oid) => ({
|
|
487
|
-
oid,
|
|
488
|
-
timestamp: getMigrationNow(),
|
|
489
|
-
data: { op: 'delete' },
|
|
490
|
-
})),
|
|
491
|
-
);
|
|
492
|
-
},
|
|
493
|
-
};
|
|
494
|
-
return acc;
|
|
495
|
-
}, {} as any);
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
function getMigrationQueries({
|
|
499
|
-
migration,
|
|
500
|
-
context,
|
|
501
|
-
meta,
|
|
502
|
-
}: {
|
|
503
|
-
migration: Migration<any>;
|
|
504
|
-
context: Context;
|
|
505
|
-
meta: Metadata;
|
|
506
|
-
}) {
|
|
507
|
-
return migration.oldCollections.reduce((acc, collectionName) => {
|
|
508
|
-
acc[collectionName] = {
|
|
509
|
-
get: async (id: string) => {
|
|
510
|
-
const oid = createOid(collectionName, id);
|
|
511
|
-
const doc = await meta.getDocumentSnapshot(oid, {
|
|
512
|
-
// only get the snapshot up to the previous version (newer operations may have synced)
|
|
513
|
-
to: meta.time.now(migration.oldSchema.version),
|
|
514
|
-
});
|
|
515
|
-
return doc;
|
|
516
|
-
},
|
|
517
|
-
findOne: async (filter: CollectionFilter) => {
|
|
518
|
-
const oid = await findOneOid({
|
|
519
|
-
collection: collectionName,
|
|
520
|
-
index: filter,
|
|
521
|
-
context,
|
|
522
|
-
});
|
|
523
|
-
if (!oid) return null;
|
|
524
|
-
const doc = await meta.getDocumentSnapshot(oid, {
|
|
525
|
-
// only get the snapshot up to the previous version (newer operations may have synced)
|
|
526
|
-
to: meta.time.now(migration.oldSchema.version),
|
|
527
|
-
});
|
|
528
|
-
return doc;
|
|
529
|
-
},
|
|
530
|
-
findAll: async (filter: CollectionFilter) => {
|
|
531
|
-
const oids = await findAllOids({
|
|
532
|
-
collection: collectionName,
|
|
533
|
-
index: filter,
|
|
534
|
-
context,
|
|
535
|
-
});
|
|
536
|
-
const docs = await Promise.all(
|
|
537
|
-
oids.map((oid) =>
|
|
538
|
-
meta.getDocumentSnapshot(oid, {
|
|
539
|
-
// only get the snapshot up to the previous version (newer operations may have synced)
|
|
540
|
-
to: meta.time.now(migration.oldSchema.version),
|
|
541
|
-
}),
|
|
542
|
-
),
|
|
543
|
-
);
|
|
544
|
-
return docs;
|
|
545
|
-
},
|
|
546
|
-
};
|
|
547
|
-
return acc;
|
|
548
|
-
}, {} as any);
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
function getMigrationEngine({
|
|
552
|
-
meta,
|
|
553
|
-
migration,
|
|
554
|
-
context,
|
|
555
|
-
}: {
|
|
556
|
-
log?: (...args: any[]) => void;
|
|
557
|
-
migration: Migration;
|
|
558
|
-
meta: Metadata;
|
|
559
|
-
context: Context;
|
|
560
|
-
}): MigrationEngine {
|
|
561
|
-
function getMigrationNow() {
|
|
562
|
-
return meta.time.zero(migration.version);
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
const newOids = new Array<ObjectIdentifier>();
|
|
566
|
-
|
|
567
|
-
const queries = getMigrationQueries({
|
|
568
|
-
migration,
|
|
569
|
-
context,
|
|
570
|
-
meta,
|
|
571
|
-
});
|
|
572
|
-
const mutations = getMigrationMutations({
|
|
573
|
-
migration,
|
|
574
|
-
getMigrationNow,
|
|
575
|
-
newOids,
|
|
576
|
-
meta,
|
|
577
|
-
});
|
|
578
|
-
const deleteCollection = async (collection: string) => {
|
|
579
|
-
const allOids = await meta.getAllCollectionRelatedOids(collection);
|
|
580
|
-
return meta.insertLocalOperations(
|
|
581
|
-
allOids.map((oid) => ({
|
|
582
|
-
oid,
|
|
583
|
-
timestamp: getMigrationNow(),
|
|
584
|
-
data: { op: 'delete' },
|
|
585
|
-
})),
|
|
586
|
-
);
|
|
587
|
-
};
|
|
588
|
-
const awaitables = new Array<Promise<any>>();
|
|
589
|
-
const engine: MigrationEngine = {
|
|
590
|
-
log: context.log,
|
|
591
|
-
newOids,
|
|
592
|
-
deleteCollection,
|
|
593
|
-
migrate: async (collection, strategy) => {
|
|
594
|
-
const docs = await queries[collection].findAll();
|
|
595
|
-
|
|
596
|
-
await Promise.all(
|
|
597
|
-
docs.filter(Boolean).map(async (doc: any) => {
|
|
598
|
-
const rootOid = getOid(doc);
|
|
599
|
-
assert(
|
|
600
|
-
!!rootOid,
|
|
601
|
-
`Document is missing an OID: ${JSON.stringify(doc)}`,
|
|
602
|
-
);
|
|
603
|
-
const original = cloneDeep(doc);
|
|
604
|
-
// @ts-ignore - excessive type resolution
|
|
605
|
-
const newValue = await strategy(doc);
|
|
606
|
-
if (newValue) {
|
|
607
|
-
// the migration has altered the shape of our document. we need
|
|
608
|
-
// to create the operation from the diff and write it to meta as
|
|
609
|
-
// a migration patch
|
|
610
|
-
removeOidPropertiesFromAllSubObjects(original);
|
|
611
|
-
removeOidPropertiesFromAllSubObjects(newValue);
|
|
612
|
-
assignOidsToAllSubObjects(newValue);
|
|
613
|
-
const patches = diffToPatches(
|
|
614
|
-
original,
|
|
615
|
-
newValue,
|
|
616
|
-
getMigrationNow,
|
|
617
|
-
undefined,
|
|
618
|
-
[],
|
|
619
|
-
{
|
|
620
|
-
mergeUnknownObjects: true,
|
|
621
|
-
},
|
|
622
|
-
);
|
|
623
|
-
if (patches.length > 0) {
|
|
624
|
-
await meta.insertLocalOperations(patches);
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
}),
|
|
628
|
-
);
|
|
629
|
-
},
|
|
630
|
-
queries,
|
|
631
|
-
mutations,
|
|
632
|
-
awaitables,
|
|
633
|
-
};
|
|
634
|
-
return engine;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
function getInitialMigrationEngine({
|
|
638
|
-
meta,
|
|
639
|
-
migration,
|
|
640
|
-
context,
|
|
641
|
-
}: {
|
|
642
|
-
context: OpenDocumentDbContext;
|
|
643
|
-
migration: Migration;
|
|
644
|
-
meta: Metadata;
|
|
645
|
-
}): MigrationEngine {
|
|
646
|
-
function getMigrationNow() {
|
|
647
|
-
return meta.time.zero(migration.version);
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
const newOids = new Array<ObjectIdentifier>();
|
|
651
|
-
|
|
652
|
-
const queries = new Proxy({} as any, {
|
|
653
|
-
get() {
|
|
654
|
-
throw new Error(
|
|
655
|
-
'Queries are not available in initial migrations; there is no database yet!',
|
|
656
|
-
);
|
|
657
|
-
},
|
|
658
|
-
}) as any;
|
|
659
|
-
|
|
660
|
-
const mutations = getMigrationMutations({
|
|
661
|
-
migration,
|
|
662
|
-
getMigrationNow,
|
|
663
|
-
newOids,
|
|
664
|
-
meta,
|
|
665
|
-
});
|
|
666
|
-
const engine: MigrationEngine = {
|
|
667
|
-
log: context.log,
|
|
668
|
-
newOids,
|
|
669
|
-
deleteCollection: () => {
|
|
670
|
-
throw new Error(
|
|
671
|
-
'Calling deleteCollection() in initial migrations is not supported! Use initial migrations to seed initial data using mutations.',
|
|
672
|
-
);
|
|
673
|
-
},
|
|
674
|
-
migrate: () => {
|
|
675
|
-
throw new Error(
|
|
676
|
-
'Calling migrate() in initial migrations is not supported! Use initial migrations to seed initial data using mutations.',
|
|
677
|
-
);
|
|
678
|
-
},
|
|
679
|
-
queries,
|
|
680
|
-
mutations,
|
|
681
|
-
awaitables: [],
|
|
682
|
-
};
|
|
683
|
-
return engine;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
async function getAllKeys(store: IDBObjectStore) {
|
|
687
|
-
return new Promise<IDBValidKey[]>((resolve, reject) => {
|
|
688
|
-
const request = store.getAllKeys();
|
|
689
|
-
request.onsuccess = (event) => {
|
|
690
|
-
resolve(request.result);
|
|
691
|
-
};
|
|
692
|
-
request.onerror = (event) => {
|
|
693
|
-
reject(request.error);
|
|
694
|
-
};
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
async function deleteView(store: IDBObjectStore, id: string) {
|
|
699
|
-
const request = store.delete(id);
|
|
700
|
-
return new Promise<void>((resolve, reject) => {
|
|
701
|
-
request.onsuccess = (event) => {
|
|
702
|
-
resolve();
|
|
703
|
-
};
|
|
704
|
-
request.onerror = (event) => {
|
|
705
|
-
reject(request.error);
|
|
706
|
-
};
|
|
707
|
-
});
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
async function putView(store: IDBObjectStore, view: any) {
|
|
711
|
-
const request = store.put(view);
|
|
712
|
-
return new Promise<void>((resolve, reject) => {
|
|
713
|
-
request.onsuccess = (event) => {
|
|
714
|
-
resolve();
|
|
715
|
-
};
|
|
716
|
-
request.onerror = (event) => {
|
|
717
|
-
reject(request.error);
|
|
718
|
-
};
|
|
719
|
-
});
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
/**
|
|
723
|
-
* Gets a list of root OIDs for all documents which had operations stored already
|
|
724
|
-
* that were not applied to their queryable snapshots because they were in the
|
|
725
|
-
* future. These documents need to be refreshed in storage.
|
|
726
|
-
*/
|
|
727
|
-
async function getDocsWithUnappliedMigrations({
|
|
728
|
-
meta,
|
|
729
|
-
currentVersion,
|
|
730
|
-
newVersion: _,
|
|
731
|
-
}: {
|
|
732
|
-
currentVersion: number;
|
|
733
|
-
newVersion: number;
|
|
734
|
-
meta: Metadata;
|
|
735
|
-
}) {
|
|
736
|
-
// scan for all operations in metadata after the current version.
|
|
737
|
-
// this could be more efficient if also filtering below or equal newVersion but
|
|
738
|
-
// that seems so unlikely in practice...
|
|
739
|
-
const unappliedOperations: ClientOperation[] = [];
|
|
740
|
-
await meta.operations.iterateOverAllOperations(
|
|
741
|
-
(op) => unappliedOperations.push(op),
|
|
742
|
-
{
|
|
743
|
-
from: meta.time.zero(currentVersion + 1),
|
|
744
|
-
},
|
|
745
|
-
);
|
|
746
|
-
return Array.from(
|
|
747
|
-
new Set(unappliedOperations.map((op) => getOidRoot(op.oid))),
|
|
748
|
-
);
|
|
749
|
-
}
|