Skip to content

Commit 4858397

Browse files
committed
Fixes #1
now the deletion of the group is a more complex transaction also taking care to clean up the group attribute for all the entities that were in the group before deleting it.
1 parent fd58d41 commit 4858397

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

lib/entity-connection-pool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var db = null;
1+
var db;
22
var EntityStorage = require('./level-storage');
33
var console = require('./log');
44

lib/level-storage.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function updateSomething(action_type, pk, data, transaction, commit) {
147147
if (error)
148148
transaction.rollback(reject.bind(this, createError(500, 'cannot read ' + action_type + ' with pk ' + pkToString(pk) + ' after creating it')));
149149
else {
150-
console.log(action_type + 'executed successfully. Value ' + JSON.stringify(value));
150+
console.log(action_type + ' updated successfully. Value ' + JSON.stringify(value));
151151
if (commit && commit === true)
152152
transaction.commit(resolve.bind(this, value));
153153
else
@@ -291,9 +291,54 @@ LevelStorage.prototype.updateGroupPromise = function (group_name, owner, data) {
291291

292292
LevelStorage.prototype.deleteGroupPromise = function (group_name, owner) {
293293
console.log("arguments for deleteGroupPromise leveldb " + JSON.stringify(arguments));
294-
var pk = buildGroupPk(group_name, owner);
295-
return deleteSomething("groups", pk, transaction(this.groups), true);
296294

295+
function rollback(t1, t2, callback) {
296+
t1.rollback(t2.rollback(callback));
297+
}
298+
var that = this;
299+
var group, entity;
300+
return new Promise(function (resolve, reject) {
301+
var group_pk = buildGroupPk(group_name, owner);
302+
var t_groups = transaction(that.groups);
303+
var t_entities = transaction(that.entities);
304+
readSomething("groups", group_pk, t_groups, false)
305+
.then(function (group) {
306+
var entities = group.entities;
307+
var readEntities = [];
308+
if (entities && entities.length > 0) {
309+
entities.forEach(function (entity_pk) {
310+
readEntities.push(readSomething("entities", entity_pk, t_entities, false));
311+
});
312+
return Promise.all(readEntities);
313+
} else {
314+
return Promise.resolve([]); //return an empty set of entities so that the promise chain keeps going :)
315+
}
316+
}).then(function (entities) {
317+
var ps = [];
318+
entities.forEach(function (e) {
319+
ps.push(new Promise(function (re, rej) {
320+
e.groups = e.groups.filter(function (v) {
321+
return (v.group_name !== group_name || v.owner !== owner);
322+
});
323+
updateSomething("entities", getPk("entities", e), e, t_entities, false).then(re, rej);
324+
}));
325+
});
326+
return Promise.all(ps);
327+
}).then(function (res) {
328+
console.log("finished updating entities to remove group from their attributes");
329+
console.log("attempting to delete group " + JSON.stringify(group_pk));
330+
return deleteSomething("groups", group_pk, t_groups, false);
331+
}).then(function () {
332+
t_entities.commit(function () {
333+
t_groups.commit(function () {
334+
resolve();
335+
});
336+
});
337+
}).catch(function rej(reason) {
338+
console.log('level storage rejecting ' + reason);
339+
return rollback(t_entities, t_groups, reject.bind(this, reason));
340+
});
341+
});
297342
};
298343

299344
LevelStorage.prototype.addEntityToGroupPromise = function (group_name, owner, entity_id, entity_type) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "agile-idm-entity-storage",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "",
55
"main": "index.js",
66
"directories": {

tests/level-storage-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,45 @@ describe('LevelStorage', function () {
591591
});
592592
});
593593

594+
it('should remove group references in entities after deleting it', function (done) {
595+
var storage = createLevelStorage();
596+
var owner = "1";
597+
var entity_id = "2";
598+
var entity_type = "user";
599+
var data = {
600+
"name": "string",
601+
"token": "123"
602+
};
603+
var group;
604+
var group_name = "mygroup";
605+
storage.createGroupPromise(group_name, owner)
606+
.then(function (g) {
607+
group = g;
608+
return storage.createEntityPromise(entity_id, entity_type, owner, data)
609+
})
610+
.then(function (entity) {
611+
return storage.addEntityToGroupPromise(group.group_name, group.owner, entity_id, entity_type);
612+
}).then(function (result) {
613+
614+
return storage.deleteGroupPromise(group_name, owner);
615+
}).then(function (result) {
616+
return storage.readEntityPromise(entity_id, entity_type);
617+
}).then(function (entity) {
618+
if (entity.groups) {
619+
var r = entity.groups.filter(function (v) {
620+
return (v.group_name == group_name && v.owner === owner);
621+
});
622+
if (r.length === 0) {
623+
storage.cleanDb(done);
624+
} else {
625+
reject(new Error("group not removed! " + JSON.stringify(entity)));
626+
}
627+
}
628+
}).catch(function reject(error) {
629+
throw error;
630+
});
631+
});
632+
594633
it('should reject with 404 error when attempting to place an exitent entity in a group is not there', function (done) {
595634
var storage = createLevelStorage();
596635
var owner = "1";

0 commit comments

Comments
 (0)