Created
August 19, 2023 21:43
-
-
Save barisusakli/0b0bd6a14e97a0ef6164d7b882cc7e4c to your computer and use it in GitHub Desktop.
upgrade script for mongodb cursor error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-await-in-loop */ | |
'use strict'; | |
const db = require('../../database'); | |
const batch = require('../../batch'); | |
module.exports = { | |
name: 'Update chat messages to add roomId field', | |
timestamp: Date.UTC(2023, 6, 2), | |
method: async function () { | |
const { progress } = this; | |
const nextChatRoomId = await db.getObjectField('global', 'nextChatRoomId'); | |
const allRoomIds = []; | |
for (let i = 1; i <= nextChatRoomId; i++) { | |
allRoomIds.push(i); | |
} | |
progress.total = 0; | |
// calculate user count and set progress.total | |
await batch.processArray(allRoomIds, async (roomIds) => { | |
const arrayOfRoomData = await db.getObjects(roomIds.map(roomId => `chat:room:${roomId}`)); | |
await Promise.all(roomIds.map(async (roomId, idx) => { | |
const roomData = arrayOfRoomData[idx]; | |
if (roomData) { | |
const userCount = await db.sortedSetCard(`chat:room:${roomId}:uids`); | |
progress.total += userCount; | |
} | |
})); | |
}, { | |
batch: 500, | |
}); | |
await batch.processArray(allRoomIds, async (roomIds) => { | |
const arrayOfRoomData = await db.getObjects(roomIds.map(roomId => `chat:room:${roomId}`)); | |
for (const roomData of arrayOfRoomData) { | |
if (roomData) { | |
const midsSeen = {}; | |
const { roomId } = roomData; | |
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)); | |
if (!uniqMids.length) { | |
return; | |
} | |
let messageData = await db.getObjects(uniqMids.map(mid => `message:${mid}`)); | |
messageData.forEach((m, idx) => { | |
if (m) { | |
m.mid = parseInt(uniqMids[idx], 10); | |
} | |
}); | |
messageData = messageData.filter(Boolean); | |
const bulkSet = messageData.map( | |
msg => [`message:${msg.mid}`, { roomId: roomId }] | |
); | |
await db.setObjectBulk(bulkSet); | |
await db.sortedSetAdd( | |
`chat:room:${roomId}:mids`, | |
messageData.map(m => m.timestamp), | |
messageData.map(m => m.mid), | |
); | |
uniqMids.forEach((mid) => { | |
midsSeen[mid] = 1; | |
}); | |
}, { | |
batch: 500, | |
}); | |
// eslint-disable-next-line no-await-in-loop | |
await db.deleteAll(`uid:${uid}:chat:room:${roomId}:mids`); | |
progress.incr(1); | |
} | |
await db.setObjectField(`chat:room:${roomId}`, 'userCount', uids.length); | |
} | |
} | |
}, { | |
batch: 500, | |
}); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment