Skip to content

Commit 4fa958d

Browse files
committed
feat: admin endpoints to migrate data from DB to ES (metadata)
At the moment it only supports "metadata" index.
1 parent 6d5f9a6 commit 4fa958d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Admin endpoint migrate data from DB to ES index.
3+
*
4+
* Waits until the operation is completed and returns result.
5+
*/
6+
import _ from 'lodash';
7+
import config from 'config';
8+
import { middleware as tcMiddleware } from 'tc-core-library-js';
9+
import esUtils from '../../utils/es';
10+
11+
const ES_METADATA_INDEX = config.get('elasticsearchConfig.metadataIndexName');
12+
13+
const permissions = tcMiddleware.permissions;
14+
15+
module.exports = [
16+
permissions('project.admin'),
17+
(req, res, next) => {
18+
try {
19+
const logger = req.log;
20+
logger.debug('Entered Admin#migrateFromDb');
21+
22+
const indexName = _.get(req, 'body.indexName');
23+
logger.debug('indexName', indexName);
24+
25+
if (!indexName) {
26+
const apiErr = new Error('"indexName" is required.');
27+
apiErr.status = 400;
28+
throw apiErr;
29+
}
30+
31+
if (indexName !== ES_METADATA_INDEX) {
32+
const apiErr = new Error(`Only "indexName" === "${ES_METADATA_INDEX}" is supported for now.`);
33+
apiErr.status = 400;
34+
throw apiErr;
35+
}
36+
37+
esUtils.indexMetadata()
38+
.then(() => {
39+
res.status(200).json({ message: 'Data has been successfully migrated.' });
40+
})
41+
.catch(next);
42+
} catch (err) {
43+
next(err);
44+
}
45+
},
46+
];

src/routes/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ router.route('/v5/projects/admin/es/createIndex')
8585
.post(require('./admin/es-create-index'));
8686
router.route('/v5/projects/admin/es/deleteIndex')
8787
.delete(require('./admin/es-delete-index'));
88+
router.route('/v5/projects/admin/es/migrateFromDb')
89+
.patch(require('./admin/es-migrate-from-db'));
8890
router.route('/v5/projects/admin/es/project/index')
8991
.post(require('./admin/project-index-create'));
9092
router.route('/v5/projects/admin/es/project/remove')

0 commit comments

Comments
 (0)