2022-07-08 05:20:30 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
|
|
|
|
const DocumentUpdaterHandler = require('../app/src/Features/DocumentUpdater/DocumentUpdaterHandler')
|
|
|
|
|
|
|
|
const PROJECT_ID = process.env.PROJECT_ID
|
|
|
|
const DOC_ID = process.env.DOC_ID
|
|
|
|
const VERBOSE_LOGGING = process.env.VERBOSE_LOGGING === 'true'
|
|
|
|
const DRY_RUN = process.env.DRY_RUN !== 'false'
|
|
|
|
|
|
|
|
console.log({
|
|
|
|
PROJECT_ID,
|
|
|
|
DOC_ID,
|
|
|
|
VERBOSE_LOGGING,
|
|
|
|
DRY_RUN,
|
|
|
|
})
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
await waitForDb()
|
|
|
|
|
|
|
|
const { lines, version, ranges } = await getDocument()
|
|
|
|
const size = lines.reduce((size, line) => size + line.length + 1, 0)
|
|
|
|
|
|
|
|
console.log('doc stats:', {
|
|
|
|
lineCount: lines.length,
|
|
|
|
size,
|
|
|
|
version,
|
|
|
|
})
|
|
|
|
if (!DRY_RUN) {
|
|
|
|
console.log(`updating doc ${DOC_ID} in mongo for project ${PROJECT_ID}`)
|
2023-02-28 08:26:18 -05:00
|
|
|
const result = await db.docs.updateOne(
|
2023-12-18 05:54:01 -05:00
|
|
|
{ _id: new ObjectId(DOC_ID), project_id: new ObjectId(PROJECT_ID) },
|
2022-07-08 05:20:30 -04:00
|
|
|
{
|
|
|
|
$set: { lines, version, ranges },
|
|
|
|
$inc: { rev: 1 }, // maintain same behaviour as Docstore upsertIntoDocCollection
|
|
|
|
$unset: {
|
|
|
|
inS3: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
console.log('mongo result', result)
|
2023-02-28 08:26:18 -05:00
|
|
|
if (
|
|
|
|
result.matchedCount !== 1 ||
|
|
|
|
result.modifiedCount !== 1 ||
|
|
|
|
!result.acknowledged
|
|
|
|
) {
|
2022-07-08 05:20:30 -04:00
|
|
|
throw new Error('unexpected result from mongo update')
|
|
|
|
}
|
|
|
|
console.log(`deleting doc ${DOC_ID} from redis for project ${PROJECT_ID}`)
|
|
|
|
await DocumentUpdaterHandler.promises.deleteDoc(PROJECT_ID, DOC_ID, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDocument() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
DocumentUpdaterHandler.getDocument(
|
|
|
|
PROJECT_ID,
|
|
|
|
DOC_ID,
|
|
|
|
-1,
|
|
|
|
(error, lines, version, ranges) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error)
|
|
|
|
} else {
|
|
|
|
resolve({ lines, version, ranges })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
console.error('Done.')
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
})
|