Skip to content

Commit

Permalink
Fix errors in move relationships migration action.
Browse files Browse the repository at this point in the history
  • Loading branch information
ElJocko committed Jul 6, 2022
1 parent f0a731c commit 47de57b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions migrations/20220705205741-move-relationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ module.exports = {
const relationshipsCollection = await db.collection('relationships');

// Move relationship objects from the attackObjects collection to the relationships collection
const oldRelationships = await attackObjectsCollection.find({ '__t': 'relationship' }).toArray();
const oldRelationships = await attackObjectsCollection.find({ '__t': 'RelationshipModel' }).toArray();
console.log(`Found ${ oldRelationships.length } relationships in the attackObjects collection`);

for (const relationship of oldRelationships) {
// The relationship should not exist in the relationships collection
const newRelationship = await relationshipsCollection.find({ 'stix.id': relationship.stix.id, 'stix.modified': relationship.stix.modified });
const newRelationship = await relationshipsCollection.findOne({ 'stix.id': relationship.stix.id, 'stix.modified': relationship.stix.modified });
if (newRelationship) {
console.warning('Relationship already exists in relationships collection. Will delete from attackObjects collection without adding to relationships collection.');
console.log('Relationship already exists in relationships collection. Will delete from attackObjects collection without adding to relationships collection.');
attackObjectsCollection.findOneAndDelete({ 'stix.id': relationship.stix.id, 'stix.modified': relationship.stix.modified });
}
else {
// console.log(`Moving relationships ${ relationship.stix.id }/${ relationship.stix.modified }`);
// Add the relationship to the relationships collection
relationship._id = undefined;
relationship.__t = undefined;
relationshipsCollection.insert(relationship);
relationshipsCollection.insertOne(relationship);

// Remove the relationship from the attackObjects collection
attackObjectsCollection.findOneAndDelete({ 'stix.id': relationship.stix.id, 'stix.modified': relationship.stix.modified });
Expand All @@ -33,19 +35,20 @@ module.exports = {

// Move relationship objects from the relationship collection to the attackObjects collection
const newRelationships = await relationshipsCollection.find({ }).toArray();
console.log(`Found ${ newRelationships.length } relationships in the relationships collection`);

for (const relationship of newRelationships) {
// The relationship should not exist in the attackObjects collection
const oldRelationship = await attackObjectsCollection.find({ 'stix.id': relationship.stix.id, 'stix.modified': relationship.stix.modified });
const oldRelationship = await attackObjectsCollection.findOne({ 'stix.id': relationship.stix.id, 'stix.modified': relationship.stix.modified });
if (oldRelationship) {
console.warning('Relationship already exists in attackObjects collection. Will delete from attackObjects collection without adding to attackObjects collection.');
console.log('Relationship already exists in attackObjects collection. Will delete from attackObjects collection without adding to attackObjects collection.');
relationshipsCollection.findOneAndDelete({ 'stix.id': relationship.stix.id, 'stix.modified': relationship.stix.modified });
}
else {
// Add the relationship to the attackObjects collection
relationship._id = undefined;
relationship.__t = undefined;
attackObjectsCollection.insert(relationship);
relationship.__t = 'RelationshipModel';
attackObjectsCollection.insertOne(relationship);

// Remove the relationship from the relationships collection
relationshipsCollection.findOneAndDelete({ 'stix.id': relationship.stix.id, 'stix.modified': relationship.stix.modified });
Expand Down

0 comments on commit 47de57b

Please sign in to comment.