Skip to content

Commit

Permalink
Only use transactions on replica sets
Browse files Browse the repository at this point in the history
  • Loading branch information
GCHQDeveloper36 committed Apr 30, 2024
1 parent 7de60c3 commit f9028a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
26 changes: 16 additions & 10 deletions backend/src/services/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ModelCardRevisionModel, { ModelCardRevisionDoc } from '../models/ModelCar
import { UserInterface } from '../models/User.js'
import { GetModelCardVersionOptions, GetModelCardVersionOptionsKeys, GetModelFiltersKeys } from '../types/enums.js'
import { isValidatorResultError } from '../types/ValidatorResultError.js'
import { isReplicaSet } from '../utils/database.js'
import { toEntity } from '../utils/entity.js'
import { BadReq, Forbidden, InternalError, NotFound } from '../utils/error.js'
import { convertStringToId } from '../utils/id.js'
Expand Down Expand Up @@ -224,16 +225,21 @@ export async function _setModelCard(

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

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

return revision
}
Expand Down
5 changes: 5 additions & 0 deletions backend/src/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export async function connectToMongoose() {
}
}

export async function isReplicaSet(): Promise<boolean> {
const options = mongoose.connection.getClient().options

Check failure on line 35 in backend/src/utils/database.ts

View workflow job for this annotation

GitHub Actions / unit_testing

test/services/model.spec.ts > services > model > _setModelCard > should save and update model card if user has write permission

TypeError: Cannot read properties of undefined (reading 'options') ❯ Module.isReplicaSet src/utils/database.ts:35:49 ❯ Module._setModelCard src/services/model.ts:228:13 ❯ test/services/model.spec.ts:216:20
return Object.prototype.hasOwnProperty.call(options, 'replicaSet') && options.replicaSet.length > 0
}

export async function disconnectFromMongoose() {
await mongoose.disconnect()
log.info({ log: false }, 'Disconnected from Mongoose')
Expand Down

0 comments on commit f9028a4

Please sign in to comment.