-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrator.js
More file actions
121 lines (98 loc) · 3.93 KB
/
orchestrator.js
File metadata and controls
121 lines (98 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
const connection = require('./connection').connection;
const preparation = require('./preparation');
const uuidChecks = require('./uuid-checks');
const integrityChecks = require('./integrity-checks');
const prepare = preparation.prepare;
const insertSource = preparation.insertSource;
const movePersonsUsersAndAssociatedTables = require('./person-users');
const locationsMover = require('./location');
const patientsMover = require('./patient');
const programsMover = require('./patient-programs');
const providersMover = require('./provider');
const visitsMover = require('./visit');
const encounterMover = require('./encounter');
const obsMover = require('./obs');
const gaacModuleTablesMover = require('./gaac');
const utils = require('./utils');
const logTime = utils.logTime;
const config = require('./config');
async function orchestration() {
let persist = config.persist || false;
let startTime = Date.now();
let initialErrors = [];
let dryRun = process.argv.some(arg => (arg === '--dry-run'));
if (config.source.location === undefined) {
initialErrors.push('Error: source.location not specified in config.json file');
}
if (config.generateNewUuids === undefined) {
let msg = 'Error: generateNewUuids option must be explicitly set to true/false ' +
'in config.json file';
initialErrors.push(msg);
}
if (initialErrors.length > 0) {
initialErrors.forEach(error => {
utils.logError(error);
});
utils.logInfo('Aborting...');
process.exit(1);
}
let srcConn = null;
let destConn = null;
try {
srcConn = await connection(config.source);
destConn = await connection(config.destination);
// Check for UUID collisions
if(!dryRun) {
utils.logInfo(logTime(), ': Preparing destination database...');
await prepare(srcConn,destConn, config);
}
utils.logInfo(logTime(), ': Checking for Orphaned Records');
await integrityChecks(srcConn, config.source.openmrsDb);
utils.logInfo(logTime(), ': Ensuring uniqueness of UUIDs');
await uuidChecks(srcConn, destConn, dryRun, true);
utils.logInfo(logTime(), ': Starting data migration ...');
destConn.query('START TRANSACTION');
await movePersonsUsersAndAssociatedTables(srcConn, destConn);
utils.logInfo('Consolidating locations...');
let movedLocations = await locationsMover(srcConn, destConn);
utils.logOk(`Ok...${movedLocations} locations moved.`);
//patients & identifiers
await patientsMover(srcConn, destConn);
//programs
await programsMover(srcConn, destConn);
//providers & provider attributes
await providersMover(srcConn, destConn);
//visits & visit types
await visitsMover(srcConn, destConn);
//encounters, encounter_types, encounter_roles & encounter_providers
await encounterMover(srcConn, destConn);
//obs
await obsMover(srcConn, destConn);
//gaac tables
await gaacModuleTablesMover(srcConn, destConn);
if (!persist) {
await insertSource(destConn, config.source.location);
}
if(dryRun) {
destConn.query('ROLLBACK');
utils.logOk(`Done...No database changes have been made!`)
}
else {
destConn.query('COMMIT');
utils.logOk(`Done...All Data from ${config.source.location} copied.`);
}
} catch (ex) {
destConn.query('ROLLBACK');
utils.logError(ex);
utils.logInfo('Aborting...Rolled back, no data has been moved');
} finally {
if (srcConn) srcConn.end();
if (destConn) destConn.end();
let timeElapsed = (Date.now() - startTime);
utils.logInfo(`Time elapsed: ${timeElapsed} ms`);
}
}
module.exports = orchestration; // In case one needs to require it.
// Run
orchestration();