Merge pull request #1495 from sharelatex/sk-migrate-read-and-write-token-prefix

Add migrations for new project property: `tokens.readAndWritePrefix`

GitOrigin-RevId: 276a9e53533ae76e04e20fd94234f65999874662
This commit is contained in:
Shane Kilkelly 2019-02-14 10:08:48 +00:00 committed by sharelatex
parent 00cdc008d5
commit 25a0ea8752
3 changed files with 98 additions and 0 deletions

View file

@ -0,0 +1,23 @@
const mongojs = require('../../app/js/infrastructure/mongojs')
const { db } = mongojs
const keys = { 'tokens.readAndWritePrefix': 1 }
const opts = {
unique: true,
partialFilterExpression: {
'tokens.readAndWritePrefix': { $exists: true }
},
background: true
}
console.log(
`>> Creating index on ${JSON.stringify(keys)}, ${JSON.stringify(opts)}`
)
db.projects.createIndex(keys, opts, err => {
if (err) {
throw err
}
console.log('>> done')
process.exit(0)
})

View file

@ -0,0 +1,31 @@
const mongojs = require('../../app/js/infrastructure/mongojs')
const { db, ObjectId } = mongojs
const Async = require('async')
const projectIds = [
// put ids here
]
Async.eachLimit(
projectIds,
5,
(projectId, cb) => {
db.projects.update(
{ _id: ObjectId(projectId) },
{
$unset: { tokens: 1 },
$set: { publicAccesLevel: 'private' }
},
err => {
if (err) return cb(err)
console.log(`Deactivated tokens for ${projectId}`)
cb()
}
)
},
err => {
if (err) throw err
console.log('>> Done')
process.exit(0)
}
)

View file

@ -0,0 +1,44 @@
const mongojs = require('../../app/js/infrastructure/mongojs')
const { db } = mongojs
const Async = require('async')
db.projects.find(
{
'tokens.readAndWrite': { $exists: true },
'tokens.readAndWritePrefix': { $exists: false }
},
{ tokens: 1 },
(err, projects) => {
if (err) {
throw err
}
console.log(`>> Updating ${projects.length} projects`)
Async.eachLimit(
projects,
5,
(project, cb) => {
const rwToken = project.tokens.readAndWrite
const prefixMatch = rwToken.match(/^(\d+).*$/)
if (!prefixMatch) {
const err = new Error(
`no prefix on token: ${project._id}, ${rwToken}`
)
console.log(`>> Error, ${err.message}`)
return cb(err)
}
db.projects.update(
{ _id: project._id },
{ $set: { 'tokens.readAndWritePrefix': prefixMatch[1] } },
cb
)
},
err => {
if (err) {
throw err
}
console.log('>> done')
process.exit(0)
}
)
}
)