2022-06-22 04:50:24 -04:00
|
|
|
const VERBOSE_LOGGING = process.env.VERBOSE_LOGGING === 'true'
|
|
|
|
const DRY_RUN = process.env.DRY_RUN !== 'false'
|
|
|
|
process.env.MONGO_SOCKET_TIMEOUT =
|
|
|
|
parseInt(process.env.MONGO_SOCKET_TIMEOUT, 10) || 3600000
|
|
|
|
|
|
|
|
const PROJECT_ID = process.env.PROJECT_ID
|
|
|
|
|
2023-04-18 07:15:34 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
const {
|
|
|
|
db,
|
|
|
|
waitForDb,
|
|
|
|
READ_PREFERENCE_SECONDARY,
|
|
|
|
} = require('../../app/src/infrastructure/mongodb')
|
2022-06-22 04:50:24 -04:00
|
|
|
const ProjectHistoryHandler = require('../../app/src/Features/Project/ProjectHistoryHandler')
|
|
|
|
|
|
|
|
console.log({
|
|
|
|
DRY_RUN,
|
|
|
|
VERBOSE_LOGGING,
|
|
|
|
PROJECT_ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
let INTERRUPT = false
|
|
|
|
|
|
|
|
async function processProject(project) {
|
|
|
|
if (INTERRUPT) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!shouldPreserveHistory(project)) {
|
|
|
|
console.log(
|
|
|
|
`project ${project._id} does not have preserveHistory:true, skipping`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!DRY_RUN) {
|
|
|
|
await ProjectHistoryHandler.promises.downgradeHistory(project._id)
|
|
|
|
}
|
|
|
|
if (VERBOSE_LOGGING) {
|
2022-06-23 04:10:51 -04:00
|
|
|
console.log(`project ${project._id} downgraded to track-changes`)
|
2022-06-22 04:50:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function shouldPreserveHistory(project) {
|
|
|
|
return await db.projectHistoryMetaData.findOne(
|
|
|
|
{
|
|
|
|
$and: [
|
|
|
|
{ project_id: { $eq: project._id } },
|
|
|
|
{ preserveHistory: { $eq: true } },
|
|
|
|
],
|
|
|
|
},
|
2023-04-18 07:15:34 -04:00
|
|
|
{ readPreference: READ_PREFERENCE_SECONDARY }
|
2022-06-22 04:50:24 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
if (PROJECT_ID) {
|
|
|
|
await waitForDb()
|
|
|
|
const project = await db.projects.findOne({ _id: ObjectId(PROJECT_ID) })
|
|
|
|
await processProject(project)
|
|
|
|
} else {
|
|
|
|
console.log('PROJECT_ID environment value is needed.')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrading history is not atomic, if we quit out mid-initialisation
|
|
|
|
// then history could get into a broken state
|
|
|
|
// Instead, skip any unprocessed projects and exit() at end of the batch.
|
|
|
|
process.on('SIGINT', function () {
|
2022-06-23 04:10:51 -04:00
|
|
|
console.log('Caught SIGINT, waiting for in process downgrades to complete')
|
2022-06-22 04:50:24 -04:00
|
|
|
INTERRUPT = true
|
|
|
|
})
|
|
|
|
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
console.error('Done.')
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
})
|