Improve performance of migration 211 (adding mostImportantRole to sessions) (#19700)

pull/19698/head
Rodrigo Nascimento 5 years ago committed by GitHub
parent 90d7f318d7
commit ad337fe029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/models/server/models/Sessions.js
  2. 15
      server/startup/migrations/v211.js

@ -434,6 +434,7 @@ export class Sessions extends Base {
this.tryEnsureIndex({ instanceId: 1, sessionId: 1, userId: 1 });
this.tryEnsureIndex({ instanceId: 1, sessionId: 1 });
this.tryEnsureIndex({ sessionId: 1 });
this.tryEnsureIndex({ userId: 1 });
this.tryEnsureIndex({ year: 1, month: 1, day: 1, type: 1 });
this.tryEnsureIndex({ type: 1 });
this.tryEnsureIndex({ ip: 1, loginAt: 1 });

@ -28,8 +28,21 @@ async function migrateSessions() {
$match: { 'user.roles.0': { $exists: 1 } },
}]);
let actions = [];
for await (const session of cursor) {
await Sessions.col.updateMany({ userId: session._id }, { $set: { mostImportantRole: getMostImportantRole(session.user.roles) } });
actions.push({
updateMany: {
filter: { userId: session._id },
update: { $set: { mostImportantRole: getMostImportantRole(session.user.roles) } },
},
});
if (actions.length === 100) {
await Sessions.col.bulkWrite(actions, { ordered: false });
actions = [];
}
}
if (actions.length) {
await Sessions.col.bulkWrite(actions, { ordered: false });
}
}

Loading…
Cancel
Save