@aneuhold/be-ts-db-lib 3.0.2 → 3.0.3
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.
|
@@ -80,374 +80,387 @@ export default class MigrationService {
|
|
|
80
80
|
`Found ${legacyDocs.length} documents with legacy ObjectId IDs and ${newDocs.length} documents with string IDs.`
|
|
81
81
|
);
|
|
82
82
|
|
|
83
|
-
// Delete all existing
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
DR.logger.info(`
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
83
|
+
// Delete all existing legacy docs (with object IDs)
|
|
84
|
+
if (legacyDocs.length > 0) {
|
|
85
|
+
const userIdsToDelete = users.filter((u) => typeof u._id === 'object').map((u) => u._id);
|
|
86
|
+
const apiKeyIdsToDelete = apiKeys.filter((k) => typeof k._id === 'object').map((k) => k._id);
|
|
87
|
+
const taskIdsToDelete = tasks.filter((t) => typeof t._id === 'object').map((t) => t._id);
|
|
88
|
+
const configIdsToDelete = configs.filter((c) => typeof c._id === 'object').map((c) => c._id);
|
|
89
|
+
const nonogramItemIdsToDelete = nonogramItems
|
|
90
|
+
.filter((i) => typeof i._id === 'object')
|
|
91
|
+
.map((i) => i._id);
|
|
92
|
+
const nonogramUpgradeIdsToDelete = nonogramUpgrades
|
|
93
|
+
.filter((u) => typeof u._id === 'object')
|
|
94
|
+
.map((u) => u._id);
|
|
95
|
+
|
|
96
|
+
if (!dryRun) {
|
|
97
|
+
DR.logger.info(`Deleting ${legacyDocs.length} legacy documents...`);
|
|
98
|
+
|
|
99
|
+
if (userIdsToDelete.length > 0) {
|
|
100
|
+
const result = await (
|
|
101
|
+
await userRepo.getCollection()
|
|
102
|
+
).deleteMany({ _id: { $in: userIdsToDelete } });
|
|
103
|
+
DR.logger.info(`Deleted ${result.deletedCount} users.`);
|
|
104
|
+
}
|
|
105
|
+
if (apiKeyIdsToDelete.length > 0) {
|
|
106
|
+
const result = await (
|
|
107
|
+
await apiKeyRepo.getCollection()
|
|
108
|
+
).deleteMany({ _id: { $in: apiKeyIdsToDelete } });
|
|
109
|
+
DR.logger.info(`Deleted ${result.deletedCount} API keys.`);
|
|
110
|
+
}
|
|
111
|
+
if (taskIdsToDelete.length > 0) {
|
|
112
|
+
const result = await (
|
|
113
|
+
await taskRepo.getCollection()
|
|
114
|
+
).deleteMany({ _id: { $in: taskIdsToDelete } });
|
|
115
|
+
DR.logger.info(`Deleted ${result.deletedCount} tasks.`);
|
|
116
|
+
}
|
|
117
|
+
if (configIdsToDelete.length > 0) {
|
|
118
|
+
const result = await (
|
|
119
|
+
await configRepo.getCollection()
|
|
120
|
+
).deleteMany({ _id: { $in: configIdsToDelete } });
|
|
121
|
+
DR.logger.info(`Deleted ${result.deletedCount} configs.`);
|
|
122
|
+
}
|
|
123
|
+
if (nonogramItemIdsToDelete.length > 0) {
|
|
124
|
+
const result = await (
|
|
125
|
+
await nonogramItemRepo.getCollection()
|
|
126
|
+
).deleteMany({ _id: { $in: nonogramItemIdsToDelete } });
|
|
127
|
+
DR.logger.info(`Deleted ${result.deletedCount} nonogram items.`);
|
|
128
|
+
}
|
|
129
|
+
if (nonogramUpgradeIdsToDelete.length > 0) {
|
|
130
|
+
const result = await (
|
|
131
|
+
await nonogramUpgradesRepo.getCollection()
|
|
132
|
+
).deleteMany({ _id: { $in: nonogramUpgradeIdsToDelete } });
|
|
133
|
+
DR.logger.info(`Deleted ${result.deletedCount} nonogram upgrades.`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
DR.logger.info('Deletion complete.');
|
|
137
|
+
} else {
|
|
138
|
+
DR.logger.info(`Dry run: Would delete ${legacyDocs.length} legacy documents.`);
|
|
139
|
+
if (userIdsToDelete.length > 0) DR.logger.info(` - ${userIdsToDelete.length} users`);
|
|
140
|
+
if (apiKeyIdsToDelete.length > 0)
|
|
141
|
+
DR.logger.info(` - ${apiKeyIdsToDelete.length} API keys`);
|
|
142
|
+
if (taskIdsToDelete.length > 0) DR.logger.info(` - ${taskIdsToDelete.length} tasks`);
|
|
143
|
+
if (configIdsToDelete.length > 0) DR.logger.info(` - ${configIdsToDelete.length} configs`);
|
|
144
|
+
if (nonogramItemIdsToDelete.length > 0)
|
|
145
|
+
DR.logger.info(` - ${nonogramItemIdsToDelete.length} nonogram items`);
|
|
146
|
+
if (nonogramUpgradeIdsToDelete.length > 0)
|
|
147
|
+
DR.logger.info(` - ${nonogramUpgradeIdsToDelete.length} nonogram upgrades`);
|
|
118
148
|
}
|
|
119
|
-
|
|
120
|
-
DR.logger.info('Deletion complete. Starting fresh migration...');
|
|
121
|
-
} else if (dryRun && newDocs.length > 0) {
|
|
122
|
-
DR.logger.info(`Dry run: Would delete ${newDocs.length} existing migrated documents.`);
|
|
123
149
|
}
|
|
150
|
+
// We started with 1204 docs
|
|
151
|
+
}
|
|
152
|
+
}
|
|
124
153
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const newUsersToCreate: User[] = [];
|
|
129
|
-
const newApiKeysToCreate: ApiKey[] = [];
|
|
130
|
-
const newTasksToCreate: DashboardTask[] = [];
|
|
131
|
-
const newConfigsToCreate: DashboardUserConfig[] = [];
|
|
132
|
-
const newNonogramItemsToCreate: NonogramKatanaItem[] = [];
|
|
133
|
-
const newNonogramUpgradesToCreate: NonogramKatanaUpgrade[] = [];
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Gets a UUID for a given legacy ObjectId. If one already exists in the map,
|
|
137
|
-
* it is returned. Otherwise, a new one is generated and added to the map.
|
|
138
|
-
*/
|
|
139
|
-
const getUUID = (oldId: ObjectId): UUID => {
|
|
140
|
-
const oldIdStr = oldId.toString();
|
|
141
|
-
if (mapFromObjectIdToUUID.has(oldIdStr)) {
|
|
142
|
-
return mapFromObjectIdToUUID.get(oldIdStr);
|
|
143
|
-
}
|
|
144
|
-
const newId = uuidv7();
|
|
145
|
-
mapFromObjectIdToUUID.set(oldIdStr, newId);
|
|
146
|
-
return newId;
|
|
147
|
-
};
|
|
154
|
+
async function Migration2025UUID() {
|
|
155
|
+
// Now create the actual map
|
|
156
|
+
const mapFromObjectIdToUUID = new Map<string, UUID>();
|
|
148
157
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
158
|
+
const newUsersToCreate: User[] = [];
|
|
159
|
+
const newApiKeysToCreate: ApiKey[] = [];
|
|
160
|
+
const newTasksToCreate: DashboardTask[] = [];
|
|
161
|
+
const newConfigsToCreate: DashboardUserConfig[] = [];
|
|
162
|
+
const newNonogramItemsToCreate: NonogramKatanaItem[] = [];
|
|
163
|
+
const newNonogramUpgradesToCreate: NonogramKatanaUpgrade[] = [];
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Gets a UUID for a given legacy ObjectId. If one already exists in the map,
|
|
167
|
+
* it is returned. Otherwise, a new one is generated and added to the map.
|
|
168
|
+
*/
|
|
169
|
+
const getUUID = (oldId: ObjectId): UUID => {
|
|
170
|
+
const oldIdStr = oldId.toString();
|
|
171
|
+
if (mapFromObjectIdToUUID.has(oldIdStr)) {
|
|
172
|
+
return mapFromObjectIdToUUID.get(oldIdStr);
|
|
173
|
+
}
|
|
174
|
+
const newId = uuidv7();
|
|
175
|
+
mapFromObjectIdToUUID.set(oldIdStr, newId);
|
|
176
|
+
return newId;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// Create a map of UUID to documents
|
|
180
|
+
const mapFromUUIDToDocument = new Map<UUID, any>();
|
|
181
|
+
|
|
182
|
+
const verifyDoc = (oldDoc: any, newDoc: any) => {
|
|
183
|
+
const oldJson = JSON.stringify(oldDoc, null, 2);
|
|
184
|
+
const newJson = JSON.stringify(newDoc, null, 2);
|
|
185
|
+
const oldLines = oldJson.split('\n').length;
|
|
186
|
+
const newLines = newJson.split('\n').length;
|
|
187
|
+
if (newLines !== oldLines + 1) {
|
|
188
|
+
DR.logger.warn(
|
|
189
|
+
`Line count mismatch for ${oldDoc.docType || 'doc'} ${oldDoc._id}: Old=${oldLines}, New=${newLines}.`
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
function createNewDoc(oldDoc) {
|
|
195
|
+
if (
|
|
196
|
+
mapFromObjectIdToUUID.has(oldDoc._id.toString()) &&
|
|
197
|
+
mapFromUUIDToDocument.has(mapFromObjectIdToUUID.get(oldDoc._id.toString()))
|
|
198
|
+
) {
|
|
199
|
+
console.warn(
|
|
200
|
+
`Skipping document ${oldDoc._id} as it was already migrated.` +
|
|
168
201
|
` The created document is ${JSON.stringify(mapFromUUIDToDocument.get(mapFromObjectIdToUUID.get(oldDoc._id.toString())))}`
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
const newDocId = getUUID(oldDoc._id);
|
|
173
|
-
const newDoc = DocumentService.deepCopy(oldDoc);
|
|
174
|
-
newDoc._id = newDocId;
|
|
175
|
-
newDoc.oldOId = oldDoc._id.toString();
|
|
176
|
-
mapFromUUIDToDocument.set(newDocId, newDoc);
|
|
177
|
-
return newDoc;
|
|
202
|
+
);
|
|
203
|
+
return;
|
|
178
204
|
}
|
|
205
|
+
const newDocId = getUUID(oldDoc._id);
|
|
206
|
+
const newDoc = DocumentService.deepCopy(oldDoc);
|
|
207
|
+
newDoc._id = newDocId;
|
|
208
|
+
newDoc.oldOId = oldDoc._id.toString();
|
|
209
|
+
mapFromUUIDToDocument.set(newDocId, newDoc);
|
|
210
|
+
return newDoc;
|
|
211
|
+
}
|
|
179
212
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
213
|
+
// Filter to only legacy users with the chosen usernames
|
|
214
|
+
// const userNames = ['demoUser1', 'demoUser2', "testUser", "testUser2"];
|
|
215
|
+
// const userNames = ["testUser"];
|
|
216
|
+
// const legacyUsers = users.filter((u) => userNames.includes(u.userName) && typeof u._id === 'object');
|
|
217
|
+
const legacyUsers = users.filter((u) => typeof u._id === 'object');
|
|
218
|
+
DR.logger.info(`Found ${legacyUsers.length} legacy users to migrate.`);
|
|
219
|
+
|
|
220
|
+
// First, create all user documents and generate their UUIDs that way relationships between
|
|
221
|
+
// users in the collaborators field can be mapped correctly.
|
|
222
|
+
legacyUsers.forEach((oldUserDoc) => {
|
|
223
|
+
const newUser = createNewDoc(oldUserDoc);
|
|
224
|
+
if (!newUser) return;
|
|
225
|
+
verifyDoc(oldUserDoc, newUser);
|
|
226
|
+
newUsersToCreate.push(newUser);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Create the function that will flesh out new documents for a given user document
|
|
230
|
+
function createNewDocsForUser(oldUserDoc: User) {
|
|
231
|
+
const oldUserIdStr = oldUserDoc._id.toString();
|
|
232
|
+
const newUserId = getUUID(oldUserDoc._id);
|
|
233
|
+
|
|
234
|
+
// 1. Create new API keys
|
|
235
|
+
const userApiKeys = apiKeys.filter((k) => k.userId.toString() === oldUserIdStr);
|
|
236
|
+
userApiKeys.forEach((oldKey) => {
|
|
237
|
+
const newKey = createNewDoc(oldKey);
|
|
238
|
+
if (!newKey) return;
|
|
239
|
+
newKey.userId = newUserId;
|
|
240
|
+
verifyDoc(oldKey, newKey);
|
|
241
|
+
newApiKeysToCreate.push(newKey);
|
|
193
242
|
});
|
|
194
243
|
|
|
195
|
-
// Create
|
|
196
|
-
|
|
197
|
-
const oldUserIdStr = oldUserDoc._id.toString();
|
|
198
|
-
const newUserId = getUUID(oldUserDoc._id);
|
|
199
|
-
|
|
200
|
-
// 1. Create new API keys
|
|
201
|
-
const userApiKeys = apiKeys.filter((k) => k.userId.toString() === oldUserIdStr);
|
|
202
|
-
userApiKeys.forEach((oldKey) => {
|
|
203
|
-
const newKey = createNewDoc(oldKey);
|
|
204
|
-
if (!newKey) return;
|
|
205
|
-
newKey.userId = newUserId;
|
|
206
|
-
verifyDoc(oldKey, newKey);
|
|
207
|
-
newApiKeysToCreate.push(newKey);
|
|
208
|
-
});
|
|
244
|
+
// 2. Create new Tasks
|
|
245
|
+
const userTasks = tasks.filter((t) => t.userId.toString() === oldUserIdStr);
|
|
209
246
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
);
|
|
247
|
+
const taskChildrenMap = new Map<string, DashboardTask[]>();
|
|
248
|
+
const rootTasks: DashboardTask[] = [];
|
|
249
|
+
const userTaskIds = new Set(userTasks.map((t) => t._id.toString()));
|
|
214
250
|
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
|
|
251
|
+
userTasks.forEach((task) => {
|
|
252
|
+
const pId = task.parentTaskId?.toString();
|
|
253
|
+
if (pId && userTaskIds.has(pId)) {
|
|
254
|
+
if (!taskChildrenMap.has(pId)) {
|
|
255
|
+
taskChildrenMap.set(pId, []);
|
|
256
|
+
}
|
|
257
|
+
taskChildrenMap.get(pId).push(task);
|
|
258
|
+
} else {
|
|
259
|
+
rootTasks.push(task);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
218
262
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
263
|
+
const queue = [...rootTasks];
|
|
264
|
+
while (queue.length > 0) {
|
|
265
|
+
const oldTask = queue.shift()!;
|
|
266
|
+
const newTask = createNewDoc(oldTask);
|
|
267
|
+
|
|
268
|
+
if (newTask) {
|
|
269
|
+
newTask.userId = newUserId;
|
|
270
|
+
|
|
271
|
+
// Map user IDs in tags keys
|
|
272
|
+
const newTags: any = {};
|
|
273
|
+
Object.keys(oldTask.tags).forEach((tagUserId) => {
|
|
274
|
+
const newTagUserId = getUUID(tagUserId);
|
|
275
|
+
newTags[newTagUserId] = oldTask.tags[tagUserId];
|
|
276
|
+
});
|
|
277
|
+
newTask.tags = newTags;
|
|
278
|
+
|
|
279
|
+
// Map user IDs in filterSettings keys
|
|
280
|
+
const newFilterSettings: any = {};
|
|
281
|
+
Object.keys(oldTask.filterSettings).forEach((fsUserId) => {
|
|
282
|
+
const newFsUserId = getUUID(fsUserId);
|
|
283
|
+
newFilterSettings[newFsUserId] = {
|
|
284
|
+
...oldTask.filterSettings[fsUserId],
|
|
285
|
+
userId: newFsUserId
|
|
286
|
+
};
|
|
287
|
+
});
|
|
288
|
+
newTask.filterSettings = newFilterSettings;
|
|
289
|
+
|
|
290
|
+
// Map user IDs in sortSettings keys
|
|
291
|
+
const newSortSettings: any = {};
|
|
292
|
+
Object.keys(oldTask.sortSettings).forEach((ssUserId) => {
|
|
293
|
+
const newSsUserId = getUUID(ssUserId);
|
|
294
|
+
newSortSettings[newSsUserId] = {
|
|
295
|
+
...oldTask.sortSettings[ssUserId],
|
|
296
|
+
userId: newSsUserId
|
|
297
|
+
};
|
|
298
|
+
});
|
|
299
|
+
newTask.sortSettings = newSortSettings;
|
|
300
|
+
|
|
301
|
+
newTask.sharedWith = oldTask.sharedWith.map((id: any) => getUUID(id));
|
|
302
|
+
if (oldTask.assignedTo) {
|
|
303
|
+
newTask.assignedTo = getUUID(oldTask.assignedTo);
|
|
304
|
+
}
|
|
305
|
+
if (oldTask.parentTaskId) {
|
|
306
|
+
newTask.parentTaskId = getUUID(oldTask.parentTaskId);
|
|
307
|
+
}
|
|
308
|
+
if (oldTask.parentRecurringTaskInfo) {
|
|
309
|
+
newTask.parentRecurringTaskInfo = {
|
|
310
|
+
...oldTask.parentRecurringTaskInfo,
|
|
311
|
+
taskId: getUUID(oldTask.parentRecurringTaskInfo.taskId)
|
|
312
|
+
};
|
|
228
313
|
}
|
|
229
|
-
});
|
|
230
314
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const newTask = createNewDoc(oldTask);
|
|
235
|
-
|
|
236
|
-
if (newTask) {
|
|
237
|
-
newTask.userId = newUserId;
|
|
238
|
-
|
|
239
|
-
// Map user IDs in tags keys
|
|
240
|
-
const newTags: any = {};
|
|
241
|
-
Object.keys(oldTask.tags).forEach((tagUserId) => {
|
|
242
|
-
const newTagUserId = getUUID(tagUserId);
|
|
243
|
-
newTags[newTagUserId] = oldTask.tags[tagUserId];
|
|
244
|
-
});
|
|
245
|
-
newTask.tags = newTags;
|
|
246
|
-
|
|
247
|
-
// Map user IDs in filterSettings keys
|
|
248
|
-
const newFilterSettings: any = {};
|
|
249
|
-
Object.keys(oldTask.filterSettings).forEach((fsUserId) => {
|
|
250
|
-
const newFsUserId = getUUID(fsUserId);
|
|
251
|
-
newFilterSettings[newFsUserId] = {
|
|
252
|
-
...oldTask.filterSettings[fsUserId],
|
|
253
|
-
userId: newFsUserId
|
|
254
|
-
};
|
|
255
|
-
});
|
|
256
|
-
newTask.filterSettings = newFilterSettings;
|
|
257
|
-
|
|
258
|
-
// Map user IDs in sortSettings keys
|
|
259
|
-
const newSortSettings: any = {};
|
|
260
|
-
Object.keys(oldTask.sortSettings).forEach((ssUserId) => {
|
|
261
|
-
const newSsUserId = getUUID(ssUserId);
|
|
262
|
-
newSortSettings[newSsUserId] = {
|
|
263
|
-
...oldTask.sortSettings[ssUserId],
|
|
264
|
-
userId: newSsUserId
|
|
265
|
-
};
|
|
266
|
-
});
|
|
267
|
-
newTask.sortSettings = newSortSettings;
|
|
315
|
+
verifyDoc(oldTask, newTask);
|
|
316
|
+
newTasksToCreate.push(newTask);
|
|
317
|
+
}
|
|
268
318
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
319
|
+
const children = taskChildrenMap.get(oldTask._id.toString());
|
|
320
|
+
if (children) {
|
|
321
|
+
queue.push(...children);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// 3. Create new Configs
|
|
326
|
+
const userConfigs = configs.filter((c) => c.userId.toString() === oldUserIdStr);
|
|
327
|
+
userConfigs.forEach((oldConfig) => {
|
|
328
|
+
const newConfig = createNewDoc(oldConfig);
|
|
329
|
+
if (!newConfig) return;
|
|
330
|
+
newConfig.userId = newUserId;
|
|
331
|
+
newConfig.collaborators = (oldConfig.collaborators || []).map((id) => getUUID(id));
|
|
332
|
+
if (newConfig.taskListSortSettings) {
|
|
333
|
+
Object.entries(newConfig.taskListSortSettings).forEach(
|
|
334
|
+
([category, settings]: [string, any]) => {
|
|
335
|
+
const newSettings = {
|
|
336
|
+
userId: getUUID(settings.userId),
|
|
337
|
+
sortList: settings.sortList
|
|
280
338
|
};
|
|
339
|
+
newConfig.taskListSortSettings[category] = newSettings;
|
|
281
340
|
}
|
|
282
|
-
|
|
283
|
-
verifyDoc(oldTask, newTask);
|
|
284
|
-
newTasksToCreate.push(newTask);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
const children = taskChildrenMap.get(oldTask._id.toString());
|
|
288
|
-
if (children) {
|
|
289
|
-
queue.push(...children);
|
|
290
|
-
}
|
|
341
|
+
);
|
|
291
342
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
343
|
+
if (newConfig.taskListFilterSettings) {
|
|
344
|
+
Object.entries(newConfig.taskListFilterSettings).forEach(
|
|
345
|
+
([category, settings]: [string, any]) => {
|
|
346
|
+
const newSettings = {
|
|
347
|
+
...settings,
|
|
348
|
+
userId: getUUID(settings.userId)
|
|
349
|
+
};
|
|
350
|
+
newConfig.taskListFilterSettings[category] = newSettings;
|
|
351
|
+
}
|
|
301
352
|
);
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
userId: getUUID(settings.userId),
|
|
307
|
-
sortList: settings.sortList
|
|
308
|
-
};
|
|
309
|
-
newConfig.taskListSortSettings[category] = newSettings;
|
|
310
|
-
}
|
|
311
|
-
);
|
|
312
|
-
}
|
|
313
|
-
if (newConfig.taskListFilterSettings) {
|
|
314
|
-
Object.entries(newConfig.taskListFilterSettings).forEach(
|
|
315
|
-
([category, settings]: [string, any]) => {
|
|
316
|
-
const newSettings = {
|
|
317
|
-
...settings,
|
|
318
|
-
userId: getUUID(settings.userId)
|
|
319
|
-
};
|
|
320
|
-
newConfig.taskListFilterSettings[category] = newSettings;
|
|
321
|
-
}
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
verifyDoc(oldConfig, newConfig);
|
|
325
|
-
newConfigsToCreate.push(newConfig);
|
|
326
|
-
});
|
|
327
|
-
|
|
328
|
-
// 4. Nonogram Items
|
|
329
|
-
const userNonogramItems = nonogramItems.filter((i) => i.userId.toString() === oldUserIdStr);
|
|
330
|
-
userNonogramItems.forEach((oldItem) => {
|
|
331
|
-
const newItem = createNewDoc(oldItem);
|
|
332
|
-
if (!newItem) return;
|
|
333
|
-
newItem.userId = newUserId;
|
|
334
|
-
verifyDoc(oldItem, newItem);
|
|
335
|
-
newNonogramItemsToCreate.push(newItem);
|
|
336
|
-
});
|
|
353
|
+
}
|
|
354
|
+
verifyDoc(oldConfig, newConfig);
|
|
355
|
+
newConfigsToCreate.push(newConfig);
|
|
356
|
+
});
|
|
337
357
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
}
|
|
358
|
+
// 4. Nonogram Items
|
|
359
|
+
const userNonogramItems = nonogramItems.filter((i) => i.userId.toString() === oldUserIdStr);
|
|
360
|
+
userNonogramItems.forEach((oldItem) => {
|
|
361
|
+
const newItem = createNewDoc(oldItem);
|
|
362
|
+
if (!newItem) return;
|
|
363
|
+
newItem.userId = newUserId;
|
|
364
|
+
verifyDoc(oldItem, newItem);
|
|
365
|
+
newNonogramItemsToCreate.push(newItem);
|
|
366
|
+
});
|
|
348
367
|
|
|
349
|
-
//
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
)
|
|
360
|
-
|
|
361
|
-
legacyUserIds.has(c.userId.toString())
|
|
362
|
-
).length;
|
|
363
|
-
const expectedNonogramItems = nonogramItems.filter((i) =>
|
|
364
|
-
legacyUserIds.has(i.userId.toString())
|
|
365
|
-
).length;
|
|
366
|
-
const expectedNonogramUpgrades = nonogramUpgrades.filter((u) =>
|
|
367
|
-
legacyUserIds.has(u.userId.toString())
|
|
368
|
-
).length;
|
|
369
|
-
|
|
370
|
-
if (newUsersToCreate.length !== expectedUsers) {
|
|
371
|
-
DR.logger.error(
|
|
372
|
-
`Expected ${expectedUsers} users, but prepped ${newUsersToCreate.length}.`
|
|
373
|
-
);
|
|
374
|
-
}
|
|
375
|
-
if (newApiKeysToCreate.length !== expectedApiKeys) {
|
|
376
|
-
DR.logger.error(
|
|
377
|
-
`Expected ${expectedApiKeys} API keys, but prepped ${newApiKeysToCreate.length}.`
|
|
378
|
-
);
|
|
379
|
-
}
|
|
380
|
-
if (newTasksToCreate.length !== expectedTasks) {
|
|
381
|
-
DR.logger.error(
|
|
382
|
-
`Expected ${expectedTasks} tasks, but prepped ${newTasksToCreate.length}.`
|
|
383
|
-
);
|
|
384
|
-
}
|
|
385
|
-
if (newConfigsToCreate.length !== expectedConfigs) {
|
|
386
|
-
DR.logger.error(
|
|
387
|
-
`Expected ${expectedConfigs} configs, but prepped ${newConfigsToCreate.length}.`
|
|
388
|
-
);
|
|
389
|
-
}
|
|
390
|
-
if (newNonogramItemsToCreate.length !== expectedNonogramItems) {
|
|
391
|
-
DR.logger.error(
|
|
392
|
-
`Expected ${expectedNonogramItems} nonogram items, but prepped ${newNonogramItemsToCreate.length}.`
|
|
393
|
-
);
|
|
394
|
-
}
|
|
395
|
-
if (newNonogramUpgradesToCreate.length !== expectedNonogramUpgrades) {
|
|
396
|
-
DR.logger.error(
|
|
397
|
-
`Expected ${expectedNonogramUpgrades} nonogram upgrades, but prepped ${newNonogramUpgradesToCreate.length}.`
|
|
398
|
-
);
|
|
399
|
-
}
|
|
368
|
+
// 5. Nonogram Upgrades
|
|
369
|
+
const userNonogramUpgrades = nonogramUpgrades.filter(
|
|
370
|
+
(u) => u.userId.toString() === oldUserIdStr
|
|
371
|
+
);
|
|
372
|
+
userNonogramUpgrades.forEach((oldUpgrade) => {
|
|
373
|
+
const newUpgrade = createNewDoc(oldUpgrade);
|
|
374
|
+
if (!newUpgrade) return;
|
|
375
|
+
newUpgrade.userId = newUserId;
|
|
376
|
+
verifyDoc(oldUpgrade, newUpgrade);
|
|
377
|
+
newNonogramUpgradesToCreate.push(newUpgrade);
|
|
378
|
+
});
|
|
379
|
+
}
|
|
400
380
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
381
|
+
// Now create related documents for each user
|
|
382
|
+
legacyUsers.forEach((u) => createNewDocsForUser(u));
|
|
383
|
+
|
|
384
|
+
const legacyUserIds = new Set(legacyUsers.map((u) => u._id.toString()));
|
|
385
|
+
const expectedUsers = legacyUsers.length;
|
|
386
|
+
const expectedApiKeys = apiKeys.filter((k) => legacyUserIds.has(k.userId.toString())).length;
|
|
387
|
+
const expectedTasks = tasks.filter((t) => legacyUserIds.has(t.userId.toString())).length;
|
|
388
|
+
const expectedConfigs = configs.filter((c) => legacyUserIds.has(c.userId.toString())).length;
|
|
389
|
+
const expectedNonogramItems = nonogramItems.filter((i) =>
|
|
390
|
+
legacyUserIds.has(i.userId.toString())
|
|
391
|
+
).length;
|
|
392
|
+
const expectedNonogramUpgrades = nonogramUpgrades.filter((u) =>
|
|
393
|
+
legacyUserIds.has(u.userId.toString())
|
|
394
|
+
).length;
|
|
395
|
+
|
|
396
|
+
if (newUsersToCreate.length !== expectedUsers) {
|
|
397
|
+
DR.logger.error(`Expected ${expectedUsers} users, but prepped ${newUsersToCreate.length}.`);
|
|
398
|
+
}
|
|
399
|
+
if (newApiKeysToCreate.length !== expectedApiKeys) {
|
|
400
|
+
DR.logger.error(
|
|
401
|
+
`Expected ${expectedApiKeys} API keys, but prepped ${newApiKeysToCreate.length}.`
|
|
407
402
|
);
|
|
408
|
-
|
|
409
|
-
|
|
403
|
+
}
|
|
404
|
+
if (newTasksToCreate.length !== expectedTasks) {
|
|
405
|
+
DR.logger.error(`Expected ${expectedTasks} tasks, but prepped ${newTasksToCreate.length}.`);
|
|
406
|
+
}
|
|
407
|
+
if (newConfigsToCreate.length !== expectedConfigs) {
|
|
408
|
+
DR.logger.error(
|
|
409
|
+
`Expected ${expectedConfigs} configs, but prepped ${newConfigsToCreate.length}.`
|
|
410
410
|
);
|
|
411
|
-
|
|
411
|
+
}
|
|
412
|
+
if (newNonogramItemsToCreate.length !== expectedNonogramItems) {
|
|
413
|
+
DR.logger.error(
|
|
414
|
+
`Expected ${expectedNonogramItems} nonogram items, but prepped ${newNonogramItemsToCreate.length}.`
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
if (newNonogramUpgradesToCreate.length !== expectedNonogramUpgrades) {
|
|
418
|
+
DR.logger.error(
|
|
419
|
+
`Expected ${expectedNonogramUpgrades} nonogram upgrades, but prepped ${newNonogramUpgradesToCreate.length}.`
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
DR.logger.info(`Prepped ${newUsersToCreate.length} new users.`);
|
|
424
|
+
DR.logger.info(`Prepped ${newApiKeysToCreate.length} new API keys.`);
|
|
425
|
+
DR.logger.info(`Prepped ${newTasksToCreate.length} new tasks.`);
|
|
426
|
+
DR.logger.info(`Prepped ${newConfigsToCreate.length} new configs.`);
|
|
427
|
+
DR.logger.info(`Prepped ${newNonogramItemsToCreate.length} new nonogram items.`);
|
|
428
|
+
DR.logger.info(`Prepped ${newNonogramUpgradesToCreate.length} new nonogram upgrades.`);
|
|
429
|
+
DR.logger.info(
|
|
430
|
+
`In total, prepped ${
|
|
412
431
|
newUsersToCreate.length +
|
|
413
432
|
newApiKeysToCreate.length +
|
|
414
433
|
newTasksToCreate.length +
|
|
415
434
|
newConfigsToCreate.length +
|
|
416
435
|
newNonogramItemsToCreate.length +
|
|
417
436
|
newNonogramUpgradesToCreate.length
|
|
418
|
-
} documents for insertion.`
|
|
437
|
+
} documents for insertion.`
|
|
438
|
+
);
|
|
419
439
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
if (newApiKeysToCreate.length > 0) {
|
|
426
|
-
await (await apiKeyRepo.getCollection()).insertMany(newApiKeysToCreate);
|
|
427
|
-
}
|
|
428
|
-
if (newTasksToCreate.length > 0) {
|
|
429
|
-
await (await taskRepo.getCollection()).insertMany(newTasksToCreate);
|
|
430
|
-
}
|
|
431
|
-
if (newConfigsToCreate.length > 0) {
|
|
432
|
-
await (await configRepo.getCollection()).insertMany(newConfigsToCreate);
|
|
433
|
-
}
|
|
434
|
-
if (newNonogramItemsToCreate.length > 0) {
|
|
435
|
-
await (
|
|
436
|
-
await nonogramItemRepo.getCollection()
|
|
437
|
-
).insertMany(newNonogramItemsToCreate);
|
|
438
|
-
}
|
|
439
|
-
if (newNonogramUpgradesToCreate.length > 0) {
|
|
440
|
-
await (
|
|
441
|
-
await nonogramUpgradesRepo.getCollection()
|
|
442
|
-
).insertMany(newNonogramUpgradesToCreate);
|
|
443
|
-
}
|
|
444
|
-
DR.logger.info('Insertion complete.');
|
|
445
|
-
} else {
|
|
446
|
-
DR.logger.info('Dry run is true. Skipping DB insertion.');
|
|
440
|
+
if (!dryRun) {
|
|
441
|
+
DR.logger.info('Dry run is false. Inserting documents into DB...');
|
|
442
|
+
if (newUsersToCreate.length > 0) {
|
|
443
|
+
await(await userRepo.getCollection()).insertMany(newUsersToCreate);
|
|
447
444
|
}
|
|
448
|
-
|
|
449
|
-
|
|
445
|
+
if (newApiKeysToCreate.length > 0) {
|
|
446
|
+
await(await apiKeyRepo.getCollection()).insertMany(newApiKeysToCreate);
|
|
447
|
+
}
|
|
448
|
+
if (newTasksToCreate.length > 0) {
|
|
449
|
+
await(await taskRepo.getCollection()).insertMany(newTasksToCreate);
|
|
450
|
+
}
|
|
451
|
+
if (newConfigsToCreate.length > 0) {
|
|
452
|
+
await(await configRepo.getCollection()).insertMany(newConfigsToCreate);
|
|
453
|
+
}
|
|
454
|
+
if (newNonogramItemsToCreate.length > 0) {
|
|
455
|
+
await(await nonogramItemRepo.getCollection()).insertMany(newNonogramItemsToCreate);
|
|
456
|
+
}
|
|
457
|
+
if (newNonogramUpgradesToCreate.length > 0) {
|
|
458
|
+
await(await nonogramUpgradesRepo.getCollection()).insertMany(newNonogramUpgradesToCreate);
|
|
459
|
+
}
|
|
460
|
+
DR.logger.info('Insertion complete.');
|
|
461
|
+
} else {
|
|
462
|
+
DR.logger.info('Dry run is true. Skipping DB insertion.');
|
|
450
463
|
}
|
|
451
464
|
|
|
452
|
-
|
|
465
|
+
return;
|
|
453
466
|
}
|