Skip to content

Commit

Permalink
Migration for entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Hassine committed Jul 2, 2019
1 parent fea6362 commit 48873f0
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions opencti-graphql/migrations/1562022797712-update-stix_id.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import { queryMultiple, updateAttribute } from '../src/database/grakn';
import { index } from '../src/database/elasticSearch';
import { logger } from '../src/config/conf';

module.exports.up = async next => {
['sector', 'organization', 'user', 'region', 'country', 'city'].map(
async entityType => {
const query = `match $x isa entity; $x has stix_id $sid; $sid contains "${entityType}"; get $x;`;
const entities = await queryMultiple(query, ['x']);
entities.map(entity => {
return updateAttribute(entity.x.id, {
key: 'stix_id',
value: [entity.x.stix_id.replace(entityType, 'identity')]
}).then(stixDomainEntity => {
index('stix-domain-entities', 'stix_domain_entity', stixDomainEntity);
const resultPromise = Promise.all(
['sector', 'organization', 'user', 'region', 'country', 'city'].map(
async entityType => {
const query = `match $x isa entity; $x has stix_id $sid; $sid contains "${entityType}"; get $x;`;
const entities = await queryMultiple(query, ['x']);
logger.info('Entities loaded');
const updatePromise = Promise.all(
entities.map(entity => {
return updateAttribute(entity.x.id, {
key: 'stix_id',
value: [entity.x.stix_id.replace(entityType, 'identity')]
}).then(stixDomainEntity => {
index(
'stix-domain-entities',
'stix_domain_entity',
stixDomainEntity
);
});
})
);
await Promise.resolve(updatePromise).then(() => {
logger.info('Entities updated');
return Promise.resolve(true);
});
});
}
}
)
);
await Promise.resolve(resultPromise).then(() => {
logger.info('Migration complete');
});
next();
};

Expand Down

0 comments on commit 48873f0

Please sign in to comment.