Skip to content

Commit

Permalink
Enable transaction management for updating model cards
Browse files Browse the repository at this point in the history
  • Loading branch information
GCHQDeveloper36 committed May 3, 2024
1 parent 7c1aa70 commit acc5ee4
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions backend/src/services/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Validator } from 'jsonschema'
import mongoose from 'mongoose'

import authentication from '../connectors/authentication/index.js'
import { ModelAction, ModelActionKeys } from '../connectors/authorisation/actions.js'
Expand All @@ -10,8 +11,9 @@ import { UserInterface } from '../models/User.js'
import { GetModelCardVersionOptions, GetModelCardVersionOptionsKeys, GetModelFiltersKeys } from '../types/enums.js'
import { isValidatorResultError } from '../types/ValidatorResultError.js'
import { toEntity } from '../utils/entity.js'
import { BadReq, Forbidden, NotFound } from '../utils/error.js'
import { BadReq, Forbidden, InternalError, NotFound } from '../utils/error.js'
import { convertStringToId } from '../utils/id.js'
import log from './log.js'
import { findSchemaById } from './schema.js'

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

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

await ModelModel.updateOne({ id: modelId }, { $set: { card: newDocument } })
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 })
})

return revision
}
Expand Down

0 comments on commit acc5ee4

Please sign in to comment.