mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Merge pull request #3306 from overleaf/jpa-script-validate-data-of-model
[scripts] validate-data-of-model: add the new script GitOrigin-RevId: c432598a1efee58e95f3751ac0f63113ec837344
This commit is contained in:
parent
21070a01a8
commit
224c5a0f23
2 changed files with 50 additions and 1 deletions
|
@ -8,6 +8,7 @@ if (process.env.BATCH_LAST_ID) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNextBatch(collection, query, maxId, projection) {
|
async function getNextBatch(collection, query, maxId, projection) {
|
||||||
|
maxId = maxId || BATCH_LAST_ID
|
||||||
if (maxId) {
|
if (maxId) {
|
||||||
query['_id'] = { $gt: maxId }
|
query['_id'] = { $gt: maxId }
|
||||||
}
|
}
|
||||||
|
@ -35,7 +36,7 @@ async function batchedUpdate(collectionName, query, update, projection) {
|
||||||
projection = projection || { _id: 1 }
|
projection = projection || { _id: 1 }
|
||||||
let nextBatch
|
let nextBatch
|
||||||
let updated = 0
|
let updated = 0
|
||||||
let maxId = BATCH_LAST_ID
|
let maxId
|
||||||
while (
|
while (
|
||||||
(nextBatch = await getNextBatch(collection, query, maxId, projection))
|
(nextBatch = await getNextBatch(collection, query, maxId, projection))
|
||||||
.length
|
.length
|
||||||
|
@ -66,6 +67,7 @@ function batchedUpdateWithResultHandling(collection, query, update) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
getNextBatch,
|
||||||
batchedUpdate,
|
batchedUpdate,
|
||||||
batchedUpdateWithResultHandling
|
batchedUpdateWithResultHandling
|
||||||
}
|
}
|
||||||
|
|
47
services/web/scripts/validate-data-of-model.js
Normal file
47
services/web/scripts/validate-data-of-model.js
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
const { getNextBatch } = require('./helpers/batchedUpdate')
|
||||||
|
const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
|
||||||
|
|
||||||
|
const MODEL_NAME = process.argv.pop()
|
||||||
|
const Model = require(`../app/src/models/${MODEL_NAME}`)[MODEL_NAME]
|
||||||
|
|
||||||
|
function processBatch(batch) {
|
||||||
|
for (const doc of batch) {
|
||||||
|
const error = new Model(doc).validateSync()
|
||||||
|
if (error) {
|
||||||
|
const { errors } = error
|
||||||
|
console.log(JSON.stringify({ _id: doc._id, errors }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
await waitForDb()
|
||||||
|
const collection = db[Model.collection.name]
|
||||||
|
|
||||||
|
const query = {}
|
||||||
|
const projection = {}
|
||||||
|
|
||||||
|
let nextBatch
|
||||||
|
let processed = 0
|
||||||
|
let maxId
|
||||||
|
while (
|
||||||
|
(nextBatch = await getNextBatch(collection, query, maxId, projection))
|
||||||
|
.length
|
||||||
|
) {
|
||||||
|
maxId = nextBatch[nextBatch.length - 1]._id
|
||||||
|
processed += nextBatch.length
|
||||||
|
console.error(maxId, processed)
|
||||||
|
|
||||||
|
processBatch(nextBatch)
|
||||||
|
}
|
||||||
|
console.error('done')
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
.then(() => {
|
||||||
|
process.exit(0)
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error({ error })
|
||||||
|
process.exit(1)
|
||||||
|
})
|
Loading…
Reference in a new issue