Draft add visualID to tags Migration
MODIFY
src/migration/tasks/AddVisualIDPropertyToTagsTask.ts
src/migration/tasks/AddVisualIDPropertyToTagsTask.ts
@@ -3,6 +3,7 @@ import Logging from '../../utils/Logging';
import MigrationTask from '../MigrationTask';
import { ObjectID } from 'mongodb';
import { ServerAction } from '../../types/Server';
+import TagStorage from '../../storage/mongodb/TagStorage';
import Tenant from '../../types/Tenant';
import TenantStorage from '../../storage/mongodb/TenantStorage';
import Utils from '../../utils/Utils';
@@ -19,19 +20,22 @@ export default class AddVisualIDPropertyToTagsTask extends MigrationTask {
}
async migrateTenant(tenant: Tenant): Promise<void> {
- // Add the visualID property to tags
+ let tags = await TagStorage.getTags(tenant.id, {}, { limit: Constants.BATCH_PAGE_SIZE, onlyRecordCount: true, skip: 0 });
+ const count = tags.count;
+ let skip = 0;
let updated = 0;
- // Get Tags
- const tagsMDB = await global.database.getCollection<any>(tenant.id, 'tags')
- .find({}).toArray();
- if (!Utils.isEmptyArray(tagsMDB)) {
- for (const tagMDB of tagsMDB) {
+ do {
+ tags = await TagStorage.getTags(tenant.id, {}, { limit: Constants.BATCH_PAGE_SIZE, skip: skip });
+ if (!Utils.isEmptyArray(tags.result)) {
+ for (const tag of tags.result) {
await global.database.getCollection<any>(tenant.id, 'tags').findOneAndUpdate(
- { _id: tagMDB._id },
- { $set: { visualID: new ObjectID().toString() } });
+ { _id: tag.id },
+ { $set: { visualID: new ObjectID().toHexString() } });
updated++;
}
}
+ skip += Constants.BATCH_PAGE_SIZE;
+ } while (skip < count);
// Log in the default tenant
if (updated > 0) {
await Logging.logDebug({

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -105,7 +105,6 @@ export default class MongoDBStorage {
{ fields: { deleted: 1, createdOn: 1 } },
{ fields: { issuer: 1, createdOn: 1 } },
{ fields: { userID: 1, issuer: 1 } },
- { fields: { visualID: 1 }, options: { unique: true } }
]);
// Sites/Users
await this.handleIndexesInCollection(tenantID, 'siteusers', [

MODIFY
src/utils/Constants.ts
src/utils/Constants.ts
@@ -36,6 +36,7 @@ export default class Constants {
public static readonly EXPORT_RECORD_MAX_COUNT = 100000;
public static readonly IMPORT_PAGE_SIZE = 1000;
public static readonly IMPORT_BATCH_INSERT_SIZE = 250;
+ public static readonly BATCH_PAGE_SIZE = 1000;
public static readonly HEALTH_CHECK_ROUTE = '/health-check';

Draft add visualID to tags Migration