Files
overleaf-cep/services/web/modules/server-ce-scripts/scripts/check-mongodb.mjs
Miguel Serrano c4edd2fffa Merge pull request #21154 from overleaf/msm-esm-ce-scrits
[web] Migrate `modules/server-ce-scripts` to ESM

GitOrigin-RevId: 8856f83b12d5d092e51e5765763737259c4b3985
2024-10-22 08:05:58 +00:00

65 lines
1.6 KiB
JavaScript

import mongodb from 'mongodb-legacy'
import { waitForDb, db } from '../../../app/src/infrastructure/mongodb.js'
import mongoose from '../../../app/src/infrastructure/Mongoose.js'
const { ObjectId } = mongodb
const { getMongoClient } = mongoose
const MIN_MONGO_VERSION = [5, 0]
async function main() {
try {
await waitForDb()
} catch (err) {
console.error('Cannot connect to mongodb')
throw err
}
await checkMongoVersion()
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 () => {
await db.users.findOne({ _id: new ObjectId() }, { session })
})
} finally {
await session.endSession()
}
}
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)
}
}
main()
.then(() => {
console.error('Mongodb is up.')
process.exit(0)
})
.catch(err => {
console.error(err)
process.exit(1)
})