mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-24 03:43:41 +00:00
Merge pull request #1367 from sharelatex/ew-remove-duplicate-v1-ids
remove duplicate v1 ids GitOrigin-RevId: 9edc94e992f4a8ac653a7cd79490207a0154b3ae
This commit is contained in:
parent
ea415ea639
commit
d30c0a964c
1 changed files with 59 additions and 0 deletions
59
services/web/scripts/remove_duplicate_v1_ids.js
Normal file
59
services/web/scripts/remove_duplicate_v1_ids.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
const { db } = require('../app/js/infrastructure/mongojs')
|
||||
const async = require('async')
|
||||
const minimist = require('minimist')
|
||||
|
||||
const argv = minimist(process.argv.slice(2))
|
||||
const commit = argv.commit !== undefined
|
||||
|
||||
if (!commit) {
|
||||
console.log('DOING DRY RUN. TO SAVE CHANGES PASS --commit')
|
||||
}
|
||||
|
||||
db.users.aggregate(
|
||||
[
|
||||
{ $match: { 'overleaf.id': { $exists: true } } },
|
||||
{ $group: { _id: '$overleaf.id', count: { $sum: 1 } } },
|
||||
{ $match: { count: { $gt: 1 } } }
|
||||
],
|
||||
{ allowDiskUse: true },
|
||||
function(err, results) {
|
||||
if (err) throw err
|
||||
console.log('FOUND ' + results.length + ' DUPLICATES')
|
||||
async.mapSeries(results, removeDuplicates, function(err) {
|
||||
if (err) throw err
|
||||
console.log('DONE')
|
||||
process.exit()
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
function removeDuplicates(duplicate, callback) {
|
||||
db.users.findOne({ 'overleaf.id': duplicate._id }, function(err, keepUser) {
|
||||
if (err) throw err
|
||||
console.log('KEEPING USER ' + keepUser._id + ' FOR OL ' + duplicate._id)
|
||||
db.users.find(
|
||||
{ 'overleaf.id': duplicate._id, _id: { $ne: keepUser._id } },
|
||||
function(err, duplicateUsers) {
|
||||
if (err) throw err
|
||||
async.mapSeries(
|
||||
duplicateUsers,
|
||||
function(user, cb) {
|
||||
console.log(
|
||||
'UNLINKING USER ' + user._id + ' FOR OL ' + duplicate._id
|
||||
)
|
||||
if (!commit) return cb()
|
||||
db.users.update(
|
||||
{ _id: user._id },
|
||||
{ $unset: { 'overleaf.id': '' } },
|
||||
cb
|
||||
)
|
||||
},
|
||||
function(err) {
|
||||
if (err) throw err
|
||||
callback()
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue