@verdant-web/store 2.5.8 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle/index.js +15 -10
- package/dist/bundle/index.js.map +4 -4
- package/dist/cjs/{entities/FakeWeakRef.d.ts → FakeWeakRef.d.ts} +2 -2
- package/dist/cjs/{entities/FakeWeakRef.js → FakeWeakRef.js} +4 -4
- package/dist/cjs/FakeWeakRef.js.map +1 -0
- package/dist/cjs/IDBService.d.ts +1 -1
- package/dist/cjs/IDBService.js +18 -1
- package/dist/cjs/IDBService.js.map +1 -1
- package/dist/cjs/__tests__/documents.test.js +17 -0
- package/dist/cjs/__tests__/documents.test.js.map +1 -1
- package/dist/cjs/__tests__/fixtures/testStorage.d.ts +1 -1
- package/dist/cjs/__tests__/fixtures/testStorage.js +3 -2
- package/dist/cjs/__tests__/fixtures/testStorage.js.map +1 -1
- package/dist/cjs/__tests__/mutations.test.d.ts +1 -0
- package/dist/cjs/__tests__/mutations.test.js +42 -0
- package/dist/cjs/__tests__/mutations.test.js.map +1 -0
- package/dist/cjs/__tests__/queries.test.js +2 -0
- package/dist/cjs/__tests__/queries.test.js.map +1 -1
- package/dist/cjs/client/Client.d.ts +6 -4
- package/dist/cjs/client/Client.js +24 -16
- package/dist/cjs/client/Client.js.map +1 -1
- package/dist/cjs/client/ClientDescriptor.d.ts +15 -4
- package/dist/cjs/client/ClientDescriptor.js +117 -36
- package/dist/cjs/client/ClientDescriptor.js.map +1 -1
- package/dist/cjs/context.d.ts +1 -0
- package/dist/cjs/entities/DocumentFamiliyCache.d.ts +22 -2
- package/dist/cjs/entities/DocumentFamiliyCache.js +39 -21
- package/dist/cjs/entities/DocumentFamiliyCache.js.map +1 -1
- package/dist/cjs/entities/Entity.d.ts +7 -2
- package/dist/cjs/entities/Entity.js +33 -3
- package/dist/cjs/entities/Entity.js.map +1 -1
- package/dist/cjs/entities/EntityStore.d.ts +2 -1
- package/dist/cjs/entities/EntityStore.js +50 -20
- package/dist/cjs/entities/EntityStore.js.map +1 -1
- package/dist/cjs/idb.d.ts +2 -0
- package/dist/cjs/idb.js +9 -1
- package/dist/cjs/idb.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/metadata/BaselinesStore.js +15 -5
- package/dist/cjs/metadata/BaselinesStore.js.map +1 -1
- package/dist/cjs/metadata/openMetadataDatabase.d.ts +11 -2
- package/dist/cjs/metadata/openMetadataDatabase.js +56 -3
- package/dist/cjs/metadata/openMetadataDatabase.js.map +1 -1
- package/dist/cjs/migration/db.d.ts +1 -1
- package/dist/cjs/migration/db.js +5 -2
- package/dist/cjs/migration/db.js.map +1 -1
- package/dist/cjs/migration/openDatabase.d.ts +8 -0
- package/dist/cjs/migration/openDatabase.js +217 -165
- package/dist/cjs/migration/openDatabase.js.map +1 -1
- package/dist/cjs/queries/BaseQuery.js +12 -1
- package/dist/cjs/queries/BaseQuery.js.map +1 -1
- package/dist/cjs/sync/Sync.d.ts +6 -5
- package/dist/cjs/sync/Sync.js.map +1 -1
- package/dist/cjs/sync/WebSocketSync.js +4 -3
- package/dist/cjs/sync/WebSocketSync.js.map +1 -1
- package/dist/esm/{entities/FakeWeakRef.d.ts → FakeWeakRef.d.ts} +2 -2
- package/dist/esm/{entities/FakeWeakRef.js → FakeWeakRef.js} +2 -2
- package/dist/esm/FakeWeakRef.js.map +1 -0
- package/dist/esm/IDBService.d.ts +1 -1
- package/dist/esm/IDBService.js +18 -1
- package/dist/esm/IDBService.js.map +1 -1
- package/dist/esm/__tests__/documents.test.js +17 -0
- package/dist/esm/__tests__/documents.test.js.map +1 -1
- package/dist/esm/__tests__/fixtures/testStorage.d.ts +1 -1
- package/dist/esm/__tests__/fixtures/testStorage.js +4 -3
- package/dist/esm/__tests__/fixtures/testStorage.js.map +1 -1
- package/dist/esm/__tests__/mutations.test.d.ts +1 -0
- package/dist/esm/__tests__/mutations.test.js +40 -0
- package/dist/esm/__tests__/mutations.test.js.map +1 -0
- package/dist/esm/__tests__/queries.test.js +2 -0
- package/dist/esm/__tests__/queries.test.js.map +1 -1
- package/dist/esm/client/Client.d.ts +6 -4
- package/dist/esm/client/Client.js +25 -17
- package/dist/esm/client/Client.js.map +1 -1
- package/dist/esm/client/ClientDescriptor.d.ts +15 -4
- package/dist/esm/client/ClientDescriptor.js +121 -40
- package/dist/esm/client/ClientDescriptor.js.map +1 -1
- package/dist/esm/context.d.ts +1 -0
- package/dist/esm/entities/DocumentFamiliyCache.d.ts +22 -2
- package/dist/esm/entities/DocumentFamiliyCache.js +39 -21
- package/dist/esm/entities/DocumentFamiliyCache.js.map +1 -1
- package/dist/esm/entities/Entity.d.ts +7 -2
- package/dist/esm/entities/Entity.js +33 -3
- package/dist/esm/entities/Entity.js.map +1 -1
- package/dist/esm/entities/EntityStore.d.ts +2 -1
- package/dist/esm/entities/EntityStore.js +51 -21
- package/dist/esm/entities/EntityStore.js.map +1 -1
- package/dist/esm/idb.d.ts +2 -0
- package/dist/esm/idb.js +6 -0
- package/dist/esm/idb.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/metadata/BaselinesStore.js +16 -6
- package/dist/esm/metadata/BaselinesStore.js.map +1 -1
- package/dist/esm/metadata/openMetadataDatabase.d.ts +11 -2
- package/dist/esm/metadata/openMetadataDatabase.js +54 -2
- package/dist/esm/metadata/openMetadataDatabase.js.map +1 -1
- package/dist/esm/migration/db.d.ts +1 -1
- package/dist/esm/migration/db.js +5 -2
- package/dist/esm/migration/db.js.map +1 -1
- package/dist/esm/migration/openDatabase.d.ts +8 -0
- package/dist/esm/migration/openDatabase.js +215 -164
- package/dist/esm/migration/openDatabase.js.map +1 -1
- package/dist/esm/queries/BaseQuery.js +12 -1
- package/dist/esm/queries/BaseQuery.js.map +1 -1
- package/dist/esm/sync/Sync.d.ts +6 -5
- package/dist/esm/sync/Sync.js.map +1 -1
- package/dist/esm/sync/WebSocketSync.js +4 -3
- package/dist/esm/sync/WebSocketSync.js.map +1 -1
- package/dist/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -4
- package/src/{entities/FakeWeakRef.ts → FakeWeakRef.ts} +2 -2
- package/src/IDBService.ts +20 -2
- package/src/__tests__/documents.test.ts +19 -0
- package/src/__tests__/fixtures/testStorage.ts +4 -7
- package/src/__tests__/mutations.test.ts +51 -0
- package/src/__tests__/queries.test.ts +3 -0
- package/src/client/Client.ts +29 -21
- package/src/client/ClientDescriptor.ts +176 -53
- package/src/context.ts +1 -0
- package/src/entities/DocumentFamiliyCache.ts +66 -21
- package/src/entities/Entity.ts +41 -6
- package/src/entities/EntityStore.ts +68 -21
- package/src/idb.ts +10 -0
- package/src/index.ts +1 -0
- package/src/metadata/BaselinesStore.ts +17 -6
- package/src/metadata/openMetadataDatabase.ts +96 -13
- package/src/migration/db.ts +14 -1
- package/src/migration/openDatabase.ts +350 -219
- package/src/queries/BaseQuery.ts +14 -1
- package/src/sync/Sync.ts +13 -9
- package/src/sync/WebSocketSync.ts +1 -0
- package/dist/cjs/entities/FakeWeakRef.js.map +0 -1
- package/dist/cjs/indexes.d.ts +0 -3
- package/dist/cjs/indexes.js +0 -20
- package/dist/cjs/indexes.js.map +0 -1
- package/dist/esm/entities/FakeWeakRef.js.map +0 -1
- package/dist/esm/indexes.d.ts +0 -3
- package/dist/esm/indexes.js +0 -15
- package/dist/esm/indexes.js.map +0 -1
- package/src/indexes.ts +0 -31
|
@@ -7,23 +7,22 @@ import {
|
|
|
7
7
|
addFieldDefaults,
|
|
8
8
|
assert,
|
|
9
9
|
assignIndexValues,
|
|
10
|
-
assignOid,
|
|
11
10
|
assignOidPropertiesToAllSubObjects,
|
|
12
11
|
assignOidsToAllSubObjects,
|
|
13
12
|
cloneDeep,
|
|
14
13
|
createOid,
|
|
15
14
|
decomposeOid,
|
|
16
15
|
diffToPatches,
|
|
17
|
-
|
|
16
|
+
getIndexValues,
|
|
18
17
|
getOidRoot,
|
|
19
18
|
hasOid,
|
|
20
19
|
initialToPatches,
|
|
21
|
-
migrationRange,
|
|
22
20
|
removeOidPropertiesFromAllSubObjects,
|
|
23
|
-
removeOidsFromAllSubObjects,
|
|
24
21
|
} from '@verdant-web/common';
|
|
25
22
|
import { Context } from '../context.js';
|
|
23
|
+
import { storeRequestPromise } from '../idb.js';
|
|
26
24
|
import { Metadata } from '../metadata/Metadata.js';
|
|
25
|
+
import { ClientOperation } from '../metadata/OperationsStore.js';
|
|
27
26
|
import { findAllOids, findOneOid } from '../queries/dbQueries.js';
|
|
28
27
|
import {
|
|
29
28
|
acquireLock,
|
|
@@ -33,7 +32,6 @@ import {
|
|
|
33
32
|
upgradeDatabase,
|
|
34
33
|
} from './db.js';
|
|
35
34
|
import { getMigrationPath } from './paths.js';
|
|
36
|
-
import { ClientOperation } from '../metadata/OperationsStore.js';
|
|
37
35
|
|
|
38
36
|
const globalIDB =
|
|
39
37
|
typeof window !== 'undefined' ? window.indexedDB : (undefined as any);
|
|
@@ -53,6 +51,10 @@ export async function openDocumentDatabase({
|
|
|
53
51
|
meta: Metadata;
|
|
54
52
|
context: OpenDocumentDbContext;
|
|
55
53
|
}) {
|
|
54
|
+
if (context.schema.wip) {
|
|
55
|
+
throw new Error('Cannot open a production client with a WIP schema!');
|
|
56
|
+
}
|
|
57
|
+
|
|
56
58
|
const currentVersion = await getDatabaseVersion(
|
|
57
59
|
indexedDB,
|
|
58
60
|
context.namespace,
|
|
@@ -61,6 +63,7 @@ export async function openDocumentDatabase({
|
|
|
61
63
|
);
|
|
62
64
|
|
|
63
65
|
context.log(
|
|
66
|
+
'debug',
|
|
64
67
|
'Current database version:',
|
|
65
68
|
currentVersion,
|
|
66
69
|
'target version:',
|
|
@@ -74,220 +77,360 @@ export async function openDocumentDatabase({
|
|
|
74
77
|
});
|
|
75
78
|
|
|
76
79
|
if (toRun.length > 0) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
meta,
|
|
87
|
-
migration,
|
|
88
|
-
context,
|
|
89
|
-
});
|
|
90
|
-
await migration.migrate(engine);
|
|
91
|
-
} else {
|
|
92
|
-
// open the database with the current (old) version for this migration. this should
|
|
93
|
-
// align with the database's current version.
|
|
94
|
-
const originalDatabase = await openDatabase(
|
|
95
|
-
indexedDB,
|
|
96
|
-
context.namespace,
|
|
97
|
-
migration.oldSchema.version,
|
|
98
|
-
);
|
|
80
|
+
context.log(
|
|
81
|
+
'debug',
|
|
82
|
+
'Migrations to run:',
|
|
83
|
+
toRun.map((m) => m.version),
|
|
84
|
+
);
|
|
85
|
+
await runMigrations({ context, toRun, meta, indexedDB });
|
|
86
|
+
}
|
|
87
|
+
return openDatabase(indexedDB, context.namespace, version, context.log);
|
|
88
|
+
}
|
|
99
89
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
90
|
+
export async function openWIPDocumentDatabase({
|
|
91
|
+
version,
|
|
92
|
+
indexedDB = globalIDB,
|
|
93
|
+
migrations,
|
|
94
|
+
meta,
|
|
95
|
+
context,
|
|
96
|
+
wipNamespace,
|
|
97
|
+
}: {
|
|
98
|
+
version: number;
|
|
99
|
+
migrations: Migration<any>[];
|
|
100
|
+
indexedDB?: IDBFactory;
|
|
101
|
+
meta: Metadata;
|
|
102
|
+
context: OpenDocumentDbContext;
|
|
103
|
+
wipNamespace: string;
|
|
104
|
+
}) {
|
|
105
|
+
context.log('debug', 'Opening WIP database', wipNamespace);
|
|
106
|
+
const currentWIPVersion = await getDatabaseVersion(
|
|
107
|
+
indexedDB,
|
|
108
|
+
wipNamespace,
|
|
109
|
+
version,
|
|
110
|
+
context.log,
|
|
111
|
+
);
|
|
121
112
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
113
|
+
if (currentWIPVersion === version) {
|
|
114
|
+
context.log('info', `WIP schema is up-to-date; not refreshing database`);
|
|
115
|
+
} else {
|
|
116
|
+
context.log('info', `WIP schema is out-of-date; refreshing database`);
|
|
126
117
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
context.namespace,
|
|
130
|
-
migration.newSchema.version,
|
|
131
|
-
(transaction, db) => {
|
|
132
|
-
for (const newCollection of migration.addedCollections) {
|
|
133
|
-
db.createObjectStore(newCollection, {
|
|
134
|
-
keyPath:
|
|
135
|
-
migration.newSchema.collections[newCollection].primaryKey,
|
|
136
|
-
autoIncrement: false,
|
|
137
|
-
});
|
|
138
|
-
}
|
|
118
|
+
// first we need to copy the data from the production database to the WIP database
|
|
119
|
+
// at the current (non-wip) version.
|
|
139
120
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
121
|
+
const initialToRun = getMigrationPath({
|
|
122
|
+
currentVersion: currentWIPVersion,
|
|
123
|
+
targetVersion: version - 1,
|
|
124
|
+
migrations,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
if (initialToRun.length > 0) {
|
|
128
|
+
await runMigrations({
|
|
129
|
+
context,
|
|
130
|
+
toRun: initialToRun,
|
|
131
|
+
meta,
|
|
132
|
+
indexedDB,
|
|
133
|
+
namespace: wipNamespace,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// now, we copy the data from the main database.
|
|
137
|
+
const mainDatabase = await openDatabase(
|
|
138
|
+
indexedDB,
|
|
139
|
+
context.namespace,
|
|
140
|
+
version - 1,
|
|
141
|
+
context.log,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const wipDatabase = await openDatabase(
|
|
145
|
+
indexedDB,
|
|
146
|
+
wipNamespace,
|
|
147
|
+
version - 1,
|
|
148
|
+
context.log,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// DOMStringList... doesn't have iterable... why
|
|
152
|
+
const mainDatabaseStoreNames = new Array<string>();
|
|
153
|
+
for (let i = 0; i < mainDatabase.objectStoreNames.length; i++) {
|
|
154
|
+
mainDatabaseStoreNames.push(mainDatabase.objectStoreNames[i]);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const copyFromTransaction = mainDatabase.transaction(
|
|
158
|
+
mainDatabaseStoreNames,
|
|
159
|
+
'readonly',
|
|
160
|
+
);
|
|
161
|
+
const copyFromStores = mainDatabaseStoreNames.map((name) =>
|
|
162
|
+
copyFromTransaction.objectStore(name),
|
|
163
|
+
);
|
|
164
|
+
const allObjects = await Promise.all(
|
|
165
|
+
copyFromStores.map((store) => storeRequestPromise(store.getAll())),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const copyToTransaction = wipDatabase.transaction(
|
|
169
|
+
mainDatabaseStoreNames,
|
|
170
|
+
'readwrite',
|
|
171
|
+
);
|
|
172
|
+
const copyToStores = mainDatabaseStoreNames.map((name) =>
|
|
173
|
+
copyToTransaction.objectStore(name),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
for (let i = 0; i < copyToStores.length; i++) {
|
|
177
|
+
await Promise.all(
|
|
178
|
+
allObjects[i].map((obj) => {
|
|
179
|
+
return storeRequestPromise(copyToStores[i].put(obj));
|
|
180
|
+
}),
|
|
162
181
|
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
163
184
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
* i.e. in the next version). We have to find those documents
|
|
170
|
-
* and also write their snapshots to storage, because they
|
|
171
|
-
* won't be present in storage already to 'refresh,' so
|
|
172
|
-
* if we don't analyze metadata for 'future' operations like
|
|
173
|
-
* this, we won't know they exist.
|
|
174
|
-
*
|
|
175
|
-
* This led to behavior where the metadata would be properly
|
|
176
|
-
* synced, but after upgrading the app and migrating, items
|
|
177
|
-
* would be missing from findAll and findOne queries.
|
|
178
|
-
*/
|
|
179
|
-
const docsWithUnappliedMigrations =
|
|
180
|
-
await getDocsWithUnappliedMigrations({
|
|
181
|
-
meta,
|
|
182
|
-
currentVersion: migration.oldSchema.version,
|
|
183
|
-
newVersion: migration.newSchema.version,
|
|
184
|
-
});
|
|
185
|
+
const toRun = getMigrationPath({
|
|
186
|
+
currentVersion: version - 1,
|
|
187
|
+
targetVersion: version,
|
|
188
|
+
migrations,
|
|
189
|
+
});
|
|
185
190
|
|
|
186
|
-
|
|
187
|
-
|
|
191
|
+
if (toRun.length > 0) {
|
|
192
|
+
await runMigrations({
|
|
193
|
+
context,
|
|
194
|
+
toRun,
|
|
195
|
+
meta,
|
|
196
|
+
indexedDB,
|
|
197
|
+
namespace: wipNamespace,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return openDatabase(indexedDB, wipNamespace, version, context.log);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function runMigrations({
|
|
206
|
+
context,
|
|
207
|
+
toRun,
|
|
208
|
+
meta,
|
|
209
|
+
indexedDB = globalIDB,
|
|
210
|
+
namespace = context.namespace,
|
|
211
|
+
}: {
|
|
212
|
+
context: OpenDocumentDbContext;
|
|
213
|
+
toRun: Migration<any>[];
|
|
214
|
+
meta: Metadata;
|
|
215
|
+
indexedDB?: IDBFactory;
|
|
216
|
+
namespace?: string;
|
|
217
|
+
}) {
|
|
218
|
+
await acquireLock(namespace, async () => {
|
|
219
|
+
// now the fun part
|
|
220
|
+
for (const migration of toRun) {
|
|
221
|
+
// special case: if this is the version 1 migration, we have no pre-existing database
|
|
222
|
+
// to use for the migration.
|
|
223
|
+
let engine: MigrationEngine;
|
|
224
|
+
// migrations from 0 (i.e. initial migrations) don't attempt to open an existing db
|
|
225
|
+
if (migration.oldSchema.version === 0) {
|
|
226
|
+
engine = getInitialMigrationEngine({
|
|
227
|
+
meta,
|
|
228
|
+
migration,
|
|
229
|
+
context,
|
|
230
|
+
});
|
|
231
|
+
await migration.migrate(engine);
|
|
232
|
+
} else {
|
|
233
|
+
// open the database with the current (old) version for this migration. this should
|
|
234
|
+
// align with the database's current version.
|
|
235
|
+
const originalDatabase = await openDatabase(
|
|
188
236
|
indexedDB,
|
|
189
|
-
|
|
190
|
-
migration.
|
|
237
|
+
namespace,
|
|
238
|
+
migration.oldSchema.version,
|
|
239
|
+
context.log,
|
|
191
240
|
);
|
|
192
|
-
for (const collection of migration.allCollections) {
|
|
193
|
-
// first step is to read in all the keys we need to rewrite
|
|
194
|
-
const documentReadTransaction = upgradedDatabase.transaction(
|
|
195
|
-
collection,
|
|
196
|
-
'readwrite',
|
|
197
|
-
);
|
|
198
|
-
const readStore = documentReadTransaction.objectStore(collection);
|
|
199
|
-
const keys = await getAllKeys(readStore);
|
|
200
|
-
// map the keys to OIDs
|
|
201
|
-
const oids = keys.map((key) => createOid(collection, `${key}`));
|
|
202
|
-
oids.push(
|
|
203
|
-
...engine.newOids.filter((oid) => {
|
|
204
|
-
return decomposeOid(oid).collection === collection;
|
|
205
|
-
}),
|
|
206
|
-
...docsWithUnappliedMigrations.filter((oid) => {
|
|
207
|
-
return decomposeOid(oid).collection === collection;
|
|
208
|
-
}),
|
|
209
|
-
);
|
|
210
241
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
}),
|
|
242
|
+
// this will only write to our metadata store via operations!
|
|
243
|
+
engine = getMigrationEngine({
|
|
244
|
+
meta,
|
|
245
|
+
migration,
|
|
246
|
+
context: {
|
|
247
|
+
...context,
|
|
248
|
+
documentDb: originalDatabase,
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
try {
|
|
252
|
+
await migration.migrate(engine);
|
|
253
|
+
// wait on any out-of-band async operations to complete
|
|
254
|
+
await Promise.all(engine.awaitables);
|
|
255
|
+
} catch (err) {
|
|
256
|
+
context.log(
|
|
257
|
+
'critical',
|
|
258
|
+
`Migration failed (${migration.oldSchema.version} -> ${migration.newSchema.version})`,
|
|
259
|
+
err,
|
|
231
260
|
);
|
|
261
|
+
throw err;
|
|
262
|
+
}
|
|
232
263
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
264
|
+
// now we have to open the database again with the next version and
|
|
265
|
+
// make the appropriate schema changes during the upgrade.
|
|
266
|
+
await closeDatabase(originalDatabase);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
context.log(
|
|
270
|
+
'debug',
|
|
271
|
+
'Upgrading database',
|
|
272
|
+
namespace,
|
|
273
|
+
'to version',
|
|
274
|
+
migration.newSchema.version,
|
|
275
|
+
);
|
|
276
|
+
await upgradeDatabase(
|
|
277
|
+
indexedDB,
|
|
278
|
+
namespace,
|
|
279
|
+
migration.newSchema.version,
|
|
280
|
+
(transaction, db) => {
|
|
281
|
+
for (const newCollection of migration.addedCollections) {
|
|
282
|
+
db.createObjectStore(newCollection, {
|
|
283
|
+
keyPath:
|
|
284
|
+
migration.newSchema.collections[newCollection].primaryKey,
|
|
285
|
+
autoIncrement: false,
|
|
244
286
|
});
|
|
287
|
+
}
|
|
245
288
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
289
|
+
for (const collection of migration.allCollections) {
|
|
290
|
+
const store = transaction.objectStore(collection);
|
|
291
|
+
// apply new indexes
|
|
292
|
+
for (const newIndex of migration.addedIndexes[collection] || []) {
|
|
293
|
+
store.createIndex(newIndex.name, newIndex.name, {
|
|
294
|
+
multiEntry: newIndex.multiEntry,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
// remove old indexes
|
|
298
|
+
for (const oldIndex of migration.removedIndexes[collection] || []) {
|
|
299
|
+
store.deleteIndex(oldIndex.name);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
for (const removedCollection of migration.removedCollections) {
|
|
303
|
+
// !! can't delete the store, because old operations that relate to
|
|
304
|
+
// this store may still exist in history. instead, we can clear it out
|
|
305
|
+
// and leave it in place
|
|
306
|
+
transaction.objectStore(removedCollection).clear();
|
|
307
|
+
}
|
|
308
|
+
},
|
|
309
|
+
context.log,
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* In cases where operations from the future have been
|
|
314
|
+
* received by this client, we may have created entire
|
|
315
|
+
* documents in metadata which were not written to storage
|
|
316
|
+
* because all of their operations were in the future (
|
|
317
|
+
* i.e. in the next version). We have to find those documents
|
|
318
|
+
* and also write their snapshots to storage, because they
|
|
319
|
+
* won't be present in storage already to 'refresh,' so
|
|
320
|
+
* if we don't analyze metadata for 'future' operations like
|
|
321
|
+
* this, we won't know they exist.
|
|
322
|
+
*
|
|
323
|
+
* This led to behavior where the metadata would be properly
|
|
324
|
+
* synced, but after upgrading the app and migrating, items
|
|
325
|
+
* would be missing from findAll and findOne queries.
|
|
326
|
+
*/
|
|
327
|
+
const docsWithUnappliedMigrations = await getDocsWithUnappliedMigrations({
|
|
328
|
+
meta,
|
|
329
|
+
currentVersion: migration.oldSchema.version,
|
|
330
|
+
newVersion: migration.newSchema.version,
|
|
331
|
+
});
|
|
263
332
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
333
|
+
// once the schema is ready, we can write back the migrated documents
|
|
334
|
+
const upgradedDatabase = await openDatabase(
|
|
335
|
+
indexedDB,
|
|
336
|
+
namespace,
|
|
337
|
+
migration.newSchema.version,
|
|
338
|
+
context.log,
|
|
339
|
+
);
|
|
340
|
+
for (const collection of migration.allCollections) {
|
|
341
|
+
// first step is to read in all the keys we need to rewrite
|
|
342
|
+
const documentReadTransaction = upgradedDatabase.transaction(
|
|
343
|
+
collection,
|
|
344
|
+
'readwrite',
|
|
345
|
+
);
|
|
346
|
+
const readStore = documentReadTransaction.objectStore(collection);
|
|
347
|
+
const keys = await getAllKeys(readStore);
|
|
348
|
+
// map the keys to OIDs
|
|
349
|
+
const oids = keys.map((key) => createOid(collection, `${key}`));
|
|
350
|
+
oids.push(
|
|
351
|
+
...engine.newOids.filter((oid) => {
|
|
352
|
+
return decomposeOid(oid).collection === collection;
|
|
353
|
+
}),
|
|
354
|
+
...docsWithUnappliedMigrations.filter((oid) => {
|
|
355
|
+
return decomposeOid(oid).collection === collection;
|
|
356
|
+
}),
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
const snapshots = await Promise.all(
|
|
360
|
+
oids.map(async (oid) => {
|
|
361
|
+
try {
|
|
362
|
+
const snap = await meta.getDocumentSnapshot(oid);
|
|
363
|
+
return [oid, snap];
|
|
364
|
+
} catch (e) {
|
|
365
|
+
// this seems to happen with baselines/ops which are not fully
|
|
366
|
+
// cleaned up after deletion?
|
|
367
|
+
context.log(
|
|
368
|
+
'error',
|
|
369
|
+
'Could not regenerate snapshot during migration for oid',
|
|
370
|
+
oid,
|
|
371
|
+
'this document will not be preserved',
|
|
372
|
+
e,
|
|
373
|
+
);
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
}),
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
const views = snapshots
|
|
380
|
+
.filter((s): s is [string, any] => !!s)
|
|
381
|
+
.map(([oid, snapshot]) => {
|
|
382
|
+
if (!snapshot) return [oid, undefined];
|
|
383
|
+
const view = getIndexValues(
|
|
384
|
+
migration.newSchema.collections[collection],
|
|
385
|
+
snapshot,
|
|
386
|
+
);
|
|
387
|
+
return [oid, view];
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
// now we can write the documents back
|
|
391
|
+
const documentWriteTransaction = upgradedDatabase.transaction(
|
|
392
|
+
collection,
|
|
393
|
+
'readwrite',
|
|
394
|
+
);
|
|
395
|
+
const writeStore = documentWriteTransaction.objectStore(collection);
|
|
396
|
+
await Promise.all(
|
|
397
|
+
views.map(([oid, view]) => {
|
|
398
|
+
if (view) {
|
|
399
|
+
return putView(writeStore, view).catch((err) => {
|
|
400
|
+
view;
|
|
401
|
+
throw err;
|
|
402
|
+
});
|
|
403
|
+
} else {
|
|
404
|
+
const { id } = decomposeOid(oid);
|
|
405
|
+
return deleteView(writeStore, id);
|
|
406
|
+
}
|
|
407
|
+
}),
|
|
408
|
+
);
|
|
284
409
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
410
|
+
|
|
411
|
+
await closeDatabase(upgradedDatabase);
|
|
412
|
+
|
|
413
|
+
context.log('debug', `Migration of ${namespace} complete.`);
|
|
414
|
+
context.log(`
|
|
415
|
+
⬆️ v${migration.newSchema.version} Migration complete. Here's the rundown:
|
|
416
|
+
- Added collections: ${migration.addedCollections.join(', ')}
|
|
417
|
+
- Removed collections: ${migration.removedCollections.join(', ')}
|
|
418
|
+
- Changed collections: ${migration.changedCollections.join(', ')}
|
|
419
|
+
- New indexes: ${Object.keys(migration.addedIndexes)
|
|
420
|
+
.map((col) =>
|
|
421
|
+
migration.addedIndexes[col].map((i) => `${col}.${i.name}`),
|
|
422
|
+
)
|
|
423
|
+
.flatMap((i) => i)
|
|
424
|
+
.join(', ')}
|
|
425
|
+
- Removed indexes: ${Object.keys(migration.removedIndexes)
|
|
426
|
+
.map((col) =>
|
|
427
|
+
migration.removedIndexes[col].map((i) => `${col}.${i.name}`),
|
|
428
|
+
)
|
|
429
|
+
.flatMap((i) => i)
|
|
430
|
+
.join(', ')}
|
|
431
|
+
`);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
291
434
|
}
|
|
292
435
|
|
|
293
436
|
function getMigrationMutations({
|
|
@@ -347,7 +490,6 @@ function getMigrationQueries({
|
|
|
347
490
|
// only get the snapshot up to the previous version (newer operations may have synced)
|
|
348
491
|
to: meta.time.now(migration.oldSchema.version),
|
|
349
492
|
});
|
|
350
|
-
// removeOidsFromAllSubObjects(doc);
|
|
351
493
|
return doc;
|
|
352
494
|
},
|
|
353
495
|
findOne: async (filter: CollectionFilter) => {
|
|
@@ -361,7 +503,6 @@ function getMigrationQueries({
|
|
|
361
503
|
// only get the snapshot up to the previous version (newer operations may have synced)
|
|
362
504
|
to: meta.time.now(migration.oldSchema.version),
|
|
363
505
|
});
|
|
364
|
-
// removeOidsFromAllSubObjects(doc);
|
|
365
506
|
return doc;
|
|
366
507
|
},
|
|
367
508
|
findAll: async (filter: CollectionFilter) => {
|
|
@@ -378,7 +519,6 @@ function getMigrationQueries({
|
|
|
378
519
|
}),
|
|
379
520
|
),
|
|
380
521
|
);
|
|
381
|
-
// docs.forEach((doc) => removeOidsFromAllSubObjects(doc));
|
|
382
522
|
return docs;
|
|
383
523
|
},
|
|
384
524
|
};
|
|
@@ -395,7 +535,7 @@ function getMigrationEngine({
|
|
|
395
535
|
migration: Migration;
|
|
396
536
|
meta: Metadata;
|
|
397
537
|
context: Context;
|
|
398
|
-
}): MigrationEngine
|
|
538
|
+
}): MigrationEngine {
|
|
399
539
|
function getMigrationNow() {
|
|
400
540
|
return meta.time.zero(migration.version);
|
|
401
541
|
}
|
|
@@ -414,7 +554,7 @@ function getMigrationEngine({
|
|
|
414
554
|
meta,
|
|
415
555
|
});
|
|
416
556
|
const awaitables = new Array<Promise<any>>();
|
|
417
|
-
const engine: MigrationEngine
|
|
557
|
+
const engine: MigrationEngine = {
|
|
418
558
|
log: context.log,
|
|
419
559
|
newOids,
|
|
420
560
|
migrate: async (collection, strategy) => {
|
|
@@ -427,15 +567,6 @@ function getMigrationEngine({
|
|
|
427
567
|
`Document is missing an OID: ${JSON.stringify(doc)}`,
|
|
428
568
|
);
|
|
429
569
|
const original = cloneDeep(doc);
|
|
430
|
-
// remove any indexes before computing the diff
|
|
431
|
-
// const collectionSpec = migration.oldSchema.collections[collection];
|
|
432
|
-
// const indexKeys = [
|
|
433
|
-
// ...Object.keys(collectionSpec.synthetics || {}),
|
|
434
|
-
// ...Object.keys(collectionSpec.compounds || {}),
|
|
435
|
-
// ];
|
|
436
|
-
// indexKeys.forEach((key) => {
|
|
437
|
-
// delete doc[key];
|
|
438
|
-
// });
|
|
439
570
|
// @ts-ignore - excessive type resolution
|
|
440
571
|
const newValue = await strategy(doc);
|
|
441
572
|
if (newValue) {
|
|
@@ -477,7 +608,7 @@ function getInitialMigrationEngine({
|
|
|
477
608
|
context: OpenDocumentDbContext;
|
|
478
609
|
migration: Migration;
|
|
479
610
|
meta: Metadata;
|
|
480
|
-
}): MigrationEngine
|
|
611
|
+
}): MigrationEngine {
|
|
481
612
|
function getMigrationNow() {
|
|
482
613
|
return meta.time.zero(migration.version);
|
|
483
614
|
}
|
|
@@ -498,7 +629,7 @@ function getInitialMigrationEngine({
|
|
|
498
629
|
newOids,
|
|
499
630
|
meta,
|
|
500
631
|
});
|
|
501
|
-
const engine: MigrationEngine
|
|
632
|
+
const engine: MigrationEngine = {
|
|
502
633
|
log: context.log,
|
|
503
634
|
newOids,
|
|
504
635
|
migrate: () => {
|