fix: upgrade script, get rid of nested processSortedSet

This commit is contained in:
Barış Soner Uşaklı
2023-08-19 21:53:44 -04:00
parent 38149a57c5
commit 78a7cfe9c2
2 changed files with 63 additions and 66 deletions

View File

@@ -11,10 +11,11 @@ module.exports = {
method: async function () {
const { progress } = this;
progress.total = await db.sortedSetCard(`chat:rooms`);
await batch.processSortedSet(`chat:rooms`, async (roomIds) => {
progress.incr(roomIds.length);
for (const roomId of roomIds) {
const allRoomIds = await db.getSortedSetRange(`chat:rooms`, 0, -1);
progress.total = allRoomIds.length;
for (const roomId of allRoomIds) {
await batch.processSortedSet(`chat:room:${roomId}:mids`, async (mids) => {
let messageData = await db.getObjects(mids.map(mid => `message:${mid}`));
messageData.forEach((m, idx) => {
@@ -37,10 +38,9 @@ module.exports = {
}, {
batch: 500,
});
progress.incr(1);
}
}, {
batch: 500,
});
const count = await db.sortedSetCard(`messages:mid`);
await db.setObjectField('global', 'messageCount', count);
},

View File

@@ -38,7 +38,7 @@ module.exports = {
if (roomData) {
const midsSeen = {};
const { roomId } = roomData;
await batch.processSortedSet(`chat:room:${roomId}:uids`, async (uids) => {
const uids = await db.getSortedSetRange(`chat:room:${roomId}:uids`, 0, -1);
for (const uid of uids) {
await batch.processSortedSet(`uid:${uid}:chat:room:${roomId}:mids`, async (mids) => {
const uniqMids = mids.filter(mid => !midsSeen.hasOwnProperty(mid));
@@ -72,13 +72,10 @@ module.exports = {
});
// eslint-disable-next-line no-await-in-loop
await db.deleteAll(`uid:${uid}:chat:room:${roomId}:mids`);
progress.incr(1);
}
progress.incr(uids.length);
}, {
batch: 500,
});
const userCount = await db.sortedSetCard(`chat:room:${roomId}:uids`);
await db.setObjectField(`chat:room:${roomId}`, 'userCount', userCount);
await db.setObjectField(`chat:room:${roomId}`, 'userCount', uids.length);
}
}
}, {