Skip to content

Commit

Permalink
Require replica set for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
GCHQDeveloper36 committed Jun 7, 2024
1 parent e34eec5 commit 92d8326
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions backend/src/services/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,21 @@ export async function _setModelCard(

const revision = new ModelCardRevisionModel({ ...newDocument, modelId, createdBy: user.dn })

if (await isReplicaSet()) {
await mongoose.connection
.transaction(async function executeUpdate(session) {
await revision.save({ session })
await ModelModel.updateOne({ id: modelId }, { $set: { card: newDocument } }, { session: session })
})
.catch((error) => {
const message = 'Unable to save model card revision'
log.error('Error when updating model card/revision. Transaction rolled back.', error)
throw InternalError(message, { modelId })
})
} else {
await revision.save()
await ModelModel.updateOne({ id: modelId }, { $set: { card: newDocument } })
if (!isReplicaSet()) {
throw InternalError('Database is not in replica set mode, cannot use transactions')
}

await mongoose.connection
.transaction(async function executeUpdate(session) {
await revision.save({ session })
await ModelModel.updateOne({ id: modelId }, { $set: { card: newDocument } }, { session: session })
})
.catch((error) => {
const message = 'Unable to save model card revision'
log.error('Error when updating model card/revision. Transaction rolled back.', error)
throw InternalError(message, { modelId })
})

return revision
}

Expand Down

0 comments on commit 92d8326

Please sign in to comment.