Skip to content

Commit

Permalink
Merge pull request #74 from bcgov/bug/no-db
Browse files Browse the repository at this point in the history
Fix meta/tag updates for 'no-db' mode and with non-versioned objects
  • Loading branch information
kamorel authored Sep 14, 2022
2 parents dfdd598 + ea286d1 commit f23d7a1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/src/controllers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,10 @@ const controller = {
const objectVersionId = s3Response.VersionId;
// delete version in DB
await versionService.delete(objId, objectVersionId);
// prune tags amd metadata
await metadataService.pruneOrphanedMetadata();
await tagService.pruneOrphanedTags();
// if other versions in DB, delete object record
// TODO: synch with versions in S3
const remainingVersions = await versionService.list(objId);
if (remainingVersions.length === 0) await objectService.delete(objId);
} else { // else deleting the object
Expand All @@ -371,6 +373,9 @@ const controller = {
} else { // else object in bucket is not versioned
// delete object record from DB
await objectService.delete(objId);
// prune tags amd metadata
await metadataService.pruneOrphanedMetadata();
await tagService.pruneOrphanedTags();
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/db/models/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Model } = require('objection');
const config = require('config');

const utils = {
/**
Expand Down Expand Up @@ -60,6 +61,7 @@ const utils = {
* @throws The error encountered upon db transaction failure
*/
async trxWrapper(callback) {
if(!config.has('db.enabled')) return Promise.resolve(true);
const trx = await Model.startTransaction();
try {
const result = await callback(trx);
Expand Down
5 changes: 2 additions & 3 deletions app/src/services/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ const service = {
// get DB records of all input metadata
const dbMetadata = await service.createMetadata(metadata, trx);

// already joined
// for non-versioned objects we are updating metadata joins for an existing version
const associatedMetadata = await VersionMetadata.query(trx)
.modify('filterVersionId', versionId);

// remove existing joins for metadata that is not in incomming set
if (associatedMetadata.length) {
const dissociateMetadata = associatedMetadata.filter(({ metadataId }) => !dbMetadata.some(({ id }) => id === metadataId));
if (dissociateMetadata.length) {
await VersionMetadata.query(trx)
.whereIn('id', dissociateMetadata.map(meta => meta.id))
.whereIn('metadataId', dissociateMetadata.map(vm => vm.metadataId))
.modify('filterVersionId', versionId)
.delete();

Expand Down
10 changes: 3 additions & 7 deletions app/src/services/version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { v4: uuidv4, NIL: SYSTEM_USER } = require('uuid');
const metadataService = require('./metadata');
const { Version } = require('../db/models');

/**
Expand Down Expand Up @@ -112,8 +111,6 @@ const service = {
.returning('*')
.throwIfNotFound();

// TODO: prune metadata and tags

if (!etrx) await trx.commit();
return Promise.resolve(response);
} catch (err) {
Expand Down Expand Up @@ -189,7 +186,6 @@ const service = {
* Updates a version of an object.
* Typically happens when updating the 'null-version' created for an object
* on a non-versioned or version-suspnded bucket.
* Replaces metadata/tags that already exists on this version by default
* @param {object[]} data array of object attributes
* @param {string} userId uuid of the current user
* @param {object} [etrx=undefined] An optional Objection Transaction object
Expand All @@ -202,7 +198,7 @@ const service = {
trx = etrx ? etrx : await Version.startTransaction();
// update version record
const versionId = data.versionId ? data.versionId : null;
const response = await Version.query(trx)
const version = await Version.query(trx)
.where({ objectId: data.id, versionId: versionId })
.patch({
objectId: data.id,
Expand All @@ -212,10 +208,10 @@ const service = {
.first()
.returning('id');

if (data.metadata) await metadataService.updateMetadata(response.id, data.metadata, data.userId, trx);
// TODO: consider updating metadata here instead of the controller

if (!etrx) await trx.commit();
return Promise.resolve(response);
return Promise.resolve(version);
} catch (err) {
if (!etrx && trx) await trx.rollback();
throw err;
Expand Down

0 comments on commit f23d7a1

Please sign in to comment.