2024-10-21 11:56:13 -04:00
|
|
|
const { ObjectId } = require('mongodb-legacy')
|
|
|
|
const { waitForDb, db } = require('../../../app/src/infrastructure/mongodb')
|
|
|
|
const { getMongoClient } = require('../../../app/src/infrastructure/Mongoose')
|
2021-07-14 06:55:08 -04:00
|
|
|
|
2024-02-28 05:19:03 -05:00
|
|
|
const MIN_MONGO_VERSION = [5, 0]
|
|
|
|
|
2023-05-11 07:50:53 -04:00
|
|
|
async function main() {
|
|
|
|
try {
|
|
|
|
await waitForDb()
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Cannot connect to mongodb')
|
|
|
|
throw err
|
|
|
|
}
|
2024-02-28 05:19:03 -05:00
|
|
|
|
|
|
|
await checkMongoVersion()
|
|
|
|
|
2023-05-11 07:50:53 -04:00
|
|
|
try {
|
|
|
|
await testTransactions()
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Mongo instance doesn't support transactions")
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testTransactions() {
|
|
|
|
const mongoClient = await getMongoClient()
|
|
|
|
const session = mongoClient.startSession()
|
|
|
|
try {
|
|
|
|
await session.withTransaction(async () => {
|
2023-12-18 05:54:01 -05:00
|
|
|
await db.users.findOne({ _id: new ObjectId() }, { session })
|
2023-05-11 07:50:53 -04:00
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
await session.endSession()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 05:19:03 -05:00
|
|
|
async function checkMongoVersion() {
|
|
|
|
const mongoClient = await getMongoClient()
|
|
|
|
const buildInfo = await mongoClient.db().admin().buildInfo()
|
|
|
|
const [major, minor] = buildInfo.versionArray
|
|
|
|
const [minMajor, minMinor] = MIN_MONGO_VERSION
|
|
|
|
|
|
|
|
if (major < minMajor || (major === minMajor && minor < minMinor)) {
|
|
|
|
const version = buildInfo.version
|
|
|
|
const minVersion = MIN_MONGO_VERSION.join('.')
|
|
|
|
console.error(
|
|
|
|
`The MongoDB server has version ${version}, but Overleaf requires at least version ${minVersion}. Aborting.`
|
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:53 -04:00
|
|
|
main()
|
2021-07-14 06:55:08 -04:00
|
|
|
.then(() => {
|
|
|
|
console.error('Mongodb is up.')
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|