Merge pull request #13045 from overleaf/em-check-mongo-transactions

Stop ServerPro/CE from booting if Mongo doesn't support transactions

GitOrigin-RevId: b38c4f4ea8e74a80fe732ef5f3fe6fa703b55af1
This commit is contained in:
Eric Mc Sween 2023-05-11 07:50:53 -04:00 committed by Copybot
parent cc47299f62
commit b313b99276
2 changed files with 37 additions and 3 deletions

View file

@ -54,11 +54,17 @@ mongoose.plugin(schema => {
mongoose.Promise = global.Promise mongoose.Promise = global.Promise
async function getMongoClient() {
const mongooseInstance = await connectionPromise
return mongooseInstance.connection.getClient()
}
async function getNativeDb() { async function getNativeDb() {
const mongooseInstance = await connectionPromise const mongooseInstance = await connectionPromise
return mongooseInstance.connection.db return mongooseInstance.connection.db
} }
mongoose.getMongoClient = getMongoClient
mongoose.getNativeDb = getNativeDb mongoose.getNativeDb = getNativeDb
mongoose.connectionPromise = connectionPromise mongoose.connectionPromise = connectionPromise

View file

@ -1,12 +1,40 @@
const { waitForDb } = require('../../../app/src/infrastructure/mongodb') const { ObjectId } = require('mongodb')
const { waitForDb, db } = require('../../../app/src/infrastructure/mongodb')
const { getMongoClient } = require('../../../app/src/infrastructure/Mongoose')
waitForDb() async function main() {
try {
await waitForDb()
} catch (err) {
console.error('Cannot connect to mongodb')
throw err
}
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: ObjectId() }, { session })
})
} finally {
await session.endSession()
}
}
main()
.then(() => { .then(() => {
console.error('Mongodb is up.') console.error('Mongodb is up.')
process.exit(0) process.exit(0)
}) })
.catch(err => { .catch(err => {
console.error('Cannot connect to mongodb.')
console.error(err) console.error(err)
process.exit(1) process.exit(1)
}) })