Skip to content

Commit acc5ee4

Browse files
Enable transaction management for updating model cards
1 parent 7c1aa70 commit acc5ee4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

backend/src/services/model.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Validator } from 'jsonschema'
2+
import mongoose from 'mongoose'
23

34
import authentication from '../connectors/authentication/index.js'
45
import { ModelAction, ModelActionKeys } from '../connectors/authorisation/actions.js'
@@ -10,8 +11,9 @@ import { UserInterface } from '../models/User.js'
1011
import { GetModelCardVersionOptions, GetModelCardVersionOptionsKeys, GetModelFiltersKeys } from '../types/enums.js'
1112
import { isValidatorResultError } from '../types/ValidatorResultError.js'
1213
import { toEntity } from '../utils/entity.js'
13-
import { BadReq, Forbidden, NotFound } from '../utils/error.js'
14+
import { BadReq, Forbidden, InternalError, NotFound } from '../utils/error.js'
1415
import { convertStringToId } from '../utils/id.js'
16+
import log from './log.js'
1517
import { findSchemaById } from './schema.js'
1618

1719
export type CreateModelParams = Pick<ModelInterface, 'name' | 'teamId' | 'description' | 'visibility'>
@@ -221,9 +223,17 @@ export async function _setModelCard(
221223
}
222224

223225
const revision = new ModelCardRevisionModel({ ...newDocument, modelId, createdBy: user.dn })
224-
await revision.save()
225226

226-
await ModelModel.updateOne({ id: modelId }, { $set: { card: newDocument } })
227+
const message = 'Unable to save model card revision'
228+
await mongoose.connection
229+
.transaction(async function executeUpdate(session) {
230+
await revision.save({ session })
231+
await ModelModel.updateOne({ id: modelId }, { $set: { card: newDocument } }, { session: session })
232+
})
233+
.catch((error) => {
234+
log.error('Error when updating model card/revision. Transaction rolled back.', error)
235+
throw InternalError(message, { modelId })
236+
})
227237

228238
return revision
229239
}

0 commit comments

Comments
 (0)