mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-29 11:33:29 -05:00
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
|
/* eslint-disable no-unused-vars */
|
||
|
|
||
|
exports.tags = ['server-ce', 'server-pro', 'saas']
|
||
|
|
||
|
async function removeDuplicates(collection, field) {
|
||
|
const duplicates = await collection.aggregate(
|
||
|
[
|
||
|
{
|
||
|
$group: {
|
||
|
_id: { projectId: `$deleterData.${field}` },
|
||
|
dups: { $addToSet: '$_id' },
|
||
|
count: { $sum: 1 },
|
||
|
},
|
||
|
},
|
||
|
{ $match: { count: { $gt: 1 } } },
|
||
|
],
|
||
|
{ allowDiskUse: true }
|
||
|
)
|
||
|
let duplicate
|
||
|
while ((duplicate = await duplicates.next())) {
|
||
|
// find duplicate items, ignore the most recent and delete the rest
|
||
|
const items = await collection
|
||
|
.find(
|
||
|
{ _id: { $in: duplicate.dups } },
|
||
|
{ projection: { _id: 1 }, sort: { 'deleterData.deletedAt': -1 } }
|
||
|
)
|
||
|
.toArray()
|
||
|
items.pop()
|
||
|
const ids = items.map(item => item._id)
|
||
|
await collection.deleteMany({ _id: { $in: ids } })
|
||
|
}
|
||
|
}
|
||
|
|
||
|
exports.migrate = async client => {
|
||
|
const { db } = client
|
||
|
await removeDuplicates(db.deletedProjects, 'deletedProjectId')
|
||
|
await removeDuplicates(db.deletedUsers, 'deletedUserId')
|
||
|
}
|
||
|
|
||
|
exports.rollback = async client => {
|
||
|
// can't really do anything here
|
||
|
}
|