Merge pull request #22580 from overleaf/ar-migrate-server-ce-scripts

Convert server-ce-scripts module to ES modules

GitOrigin-RevId: 516247b25b5bdbfd89fee4b99a88431097c827de
This commit is contained in:
Andrew Rumble
2025-01-08 16:35:00 +00:00
committed by Copybot
parent d6e763bc57
commit 813946a693
15 changed files with 153 additions and 118 deletions
@@ -0,0 +1,64 @@
import mongodb from 'mongodb-legacy'
import {
connectionPromise,
db,
} from '../../../app/src/infrastructure/mongodb.js'
const { ObjectId } = mongodb
const MIN_MONGO_VERSION = [5, 0]
async function main() {
let mongoClient
try {
mongoClient = await connectionPromise
} catch (err) {
console.error('Cannot connect to mongodb')
throw err
}
await checkMongoVersion(mongoClient)
try {
await testTransactions(mongoClient)
} catch (err) {
console.error("Mongo instance doesn't support transactions")
throw err
}
}
async function testTransactions(mongoClient) {
const session = mongoClient.startSession()
try {
await session.withTransaction(async () => {
await db.users.findOne({ _id: new ObjectId() }, { session })
})
} finally {
await session.endSession()
}
}
async function checkMongoVersion(mongoClient) {
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)
})