From 48873f0ec069fb8752a2266e0db932e8c80e5b69 Mon Sep 17 00:00:00 2001 From: Samuel Hassine Date: Tue, 2 Jul 2019 07:14:25 +0200 Subject: [PATCH] Migration for entities --- .../1562022797712-update-stix_id.js | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/opencti-graphql/migrations/1562022797712-update-stix_id.js b/opencti-graphql/migrations/1562022797712-update-stix_id.js index 10a2008a031a..8c61583258e8 100644 --- a/opencti-graphql/migrations/1562022797712-update-stix_id.js +++ b/opencti-graphql/migrations/1562022797712-update-stix_id.js @@ -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(); };