Skip to content

Commit e34acb4

Browse files
committed
Use batching
1 parent 42c15cb commit e34acb4

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/**
2-
* This migration updates date from format `dd-mm-YYYY` to midnight unixtime
3-
* so that each client with different timezone could convert it to local time
2+
* This migration sets timestamp for event repetitions if it was omitted because it's the same as original event one
43
*/
54
module.exports = {
6-
async up(db) {
5+
async up(db, client) {
76
const collections = await db.listCollections({}, {
87
authorizedCollections: true,
98
nameOnly: true,
@@ -19,25 +18,33 @@ module.exports = {
1918
}
2019
});
2120

22-
for (const projectId of projectIds) {
23-
const originalEvents = await db.collection(`${EVENTS}:${projectId}`).find({}).toArray();
24-
const repetitions = await db.collection(`${REPETITIONS}:${projectId}`).find({
25-
'payload.timestamp': { $eq: null }
26-
}).toArray();
21+
const session = client.startSession();
2722

28-
for (const event of originalEvents) {
29-
const eventRepetitions = repetitions.filter(rep => rep.groupHash === event.groupHash);
23+
try {
24+
session.withTransaction(async () => {
25+
for (const projectId of projectIds) {
26+
const originalEvents = await db.collection(`${EVENTS}:${projectId}`).find({}).toArray();
27+
const repetitions = await db.collection(`${REPETITIONS}:${projectId}`).find({
28+
'payload.timestamp': {$eq: null}
29+
}).toArray();
3030

31-
for (const repetition of eventRepetitions) {
32-
await db.collection(`${REPETITIONS}:${projectId}`).updateOne({
33-
_id: repetition._id,
34-
}, {
35-
$set: {
36-
'payload.timestamp': event.payload.timestamp,
37-
},
38-
});
31+
for (const event of originalEvents) {
32+
const eventRepetitions = repetitions.filter(rep => rep.groupHash === event.groupHash);
33+
34+
for (const repetition of eventRepetitions) {
35+
await db.collection(`${REPETITIONS}:${projectId}`).updateOne({
36+
_id: repetition._id,
37+
}, {
38+
$set: {
39+
'payload.timestamp': event.payload.timestamp,
40+
},
41+
});
42+
}
43+
}
3944
}
40-
}
45+
})
46+
} finally {
47+
session.endSession();
4148
}
4249
},
4350
};

0 commit comments

Comments
 (0)