forked from kurt213/mongodb-to-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_data.js
435 lines (387 loc) · 15.7 KB
/
migrate_data.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
let async = require('async')
let moment = require('moment')
let waterfall = require('async-waterfall')
let report_models = require('./data_model.js')
let MongoConnection = require('./mongo_db.js');
var PostgresConnection = require('./postgres_db.js');
let db
// Data migration method and module. Logic for migrating data from MongoDB to PostgreSQL
// -------------------------------------
let MigrationIsRunning = false
module.exports = function (complete) {
if (MigrationIsRunning) {
console.log('Migration script is still running')
MigrationIsRunning = true
return
}
MigrationIsRunning = true
console.log('start')
// Extract all models from data_model.js
let models = Object.getOwnPropertyNames(report_models.all_models)
let now = moment().format('')
let models_index = []
for (let rm in models) {
models_index.push(rm)
}
// Declared variables
let table_name = ''
let columns = []
let latest_update_date = ''
let latest_create_date = ''
let columns_string = ''
let pg_columns_String = ''
let pgUpdateStatement = ''
let pgInsertStatement = ''
let no_data_flag = ''
let pg_columns = []
let updated_at_flag = false
let created_at_flag = false
let model_name = ''
let model_data_type = []
let pg_update_res
let pg_create_res
let model_transform = async function (base_model, cb) {
// Get table name
let base_model_input = report_models.all_models[models[base_model]]
let model_input = base_model_input.slice()
model_data_type = model_input.map(function (x) {
return x.split(/\s+/)[1]
})
table_name = model_input.shift()
model_name = table_name.replace(/s$/, '')
model_data_type.shift()
// remove data type information from model
columns = []
let index = 0
for (let m = 0; m < model_input.length; m++) {
index = model_input[m].indexOf(' ')
columns.push(model_input[m].substring(0, index))
}
// check if created at or updated at fields exist
created_at_flag = false
updated_at_flag = false
pg_columns = []
pg_columns.push(columns[0])
if (columns.includes('created_at')) {
pg_columns.push('created_at')
created_at_flag = true
}
if (columns.includes('updated_at')) {
pg_columns.push('updated_at')
updated_at_flag = true
}
latest_update_date = ''
latest_create_date = ''
columns_string = columns.join(', ')
pg_columns_String = pg_columns.join(', ')
pgUpdateStatement = ''
pgInsertStatement = ''
no_data_flag = ''
cb()
}
function json_key(object, key, k) {
let out_array = [];
if (typeof(object) === 'undefined') {
return null
}
let json = JSON.parse(JSON.stringify(object))
if (object === null) {
return null
} else {
if (
JSON.stringify(json[key]) === 'null' ||
typeof json[key] === 'undefined'
) {
return null
} else if (
JSON.stringify(json[key]).replace(/(\r\n|\n|\r)/gm, '').length > 0
) {
// logic for different data types, i.e. date - moment, integer - convert number if needed
if (model_data_type[k].indexOf('JSONB') !== -1) {
return JSON.stringify(json[key])
}
else if (model_data_type[k].indexOf('_') !== -1) {
for (j in (json[key]) ) {
out_array.push(json[key][j])
}
return out_array
}
else if (model_data_type[k].indexOf('TIMESTAMP') !== -1) {
if (json[key] === null) {
print('this time is null')
}
return moment(
JSON.stringify(json[key])
.replace(/(\r\n|\n|\r)/gm, '')
.replace(/"/gm, '')
).format()
} else if (
model_data_type[k].indexOf('INT') !== -1 ||
model_data_type[k].indexOf('NUMERIC') !== -1
) {
return Number(
JSON.stringify(json[key])
.replace(/(\r\n|\n|\r)/gm, '')
.replace(/"/g, '')
)
} else {
return JSON.stringify(json[key])
.replace(/(\r\n|\n|\r)/gm, '')
.replace(/"/gm, '')
}
} else {
return key + ' is an empty string'
}
}
}
function pgUpdateQuery(cols) {
// Setup static beginning of query
let query = ['UPDATE ' + table_name]
query.push('SET')
// Create an array of the columns to update - skip first column which is for the WHERE
let set = []
for (let col = 0; col < cols.length - 1; col++) {
set.push(cols[col + 1] + ' = ($' + (col + 2) + ')')
}
query.push(set.join(', '))
// Add the WHERE statement to look up by id
query.push('WHERE ' + pg_columns[0] + ' = $1')
// Return a complete query string
return query.join(' ')
}
let pgGenerate = async function (cb) {
// create $1, $2, $3 for PG INSERT statement
console.log('pgGenerate')
let insert_values_array = Array.from({ length: columns.length }, (v, k) =>
String('$' + (k + 1))
)
let insert_values_string = insert_values_array.join(', ')
pgInsertStatement =
'INSERT into ' +
table_name +
' (' +
columns_string +
') VALUES (' +
insert_values_string +
')' + ' RETURNING *'
// create $1, $2, $3 etc. for PG Update Statement
pgUpdateStatement = pgUpdateQuery(columns)
cb()
}
const productInsert = () => {
return 'INSERT into products (name) VALUES ($1) RETURNING *'
}
const productSelect = () => {
return 'SELECT * FROM products WHERE name = $1'
}
const productRInsert = () => {
return 'INSERT into accouts_products (product_id, account_id) VALUES ($1, $2) RETURNING *'
}
// Extraction of currently existing data in Postgres
let pgExtract = async function (cb) {
console.log('pgExtract')
let pgUpdateText =
'SELECT ' +
pg_columns_String +
' from ' +
table_name +
' WHERE updated_at IS NOT NULL ORDER BY updated_at DESC LIMIT 5'
let pgCreateText =
'SELECT ' +
pg_columns_String +
' from ' +
table_name +
' WHERE created_at IS NOT NULL ORDER BY created_at DESC LIMIT 5'
let pgAllText =
'SELECT ' +
pg_columns_String +
' from ' +
table_name +
' LIMIT 5'
if (updated_at_flag) {
pg_update_res = await PostgresConnection().query(pgUpdateText)
}
if (created_at_flag) {
pg_create_res = await PostgresConnection().query(pgCreateText)
}
pg_all_res = await PostgresConnection().query(pgAllText)
// console.log(pg_update_res)
// console.log(pg_create_res)
if (typeof pg_all_res.rows[0] === 'undefined') {
no_data_flag = 'yes'
console.log('no postgres data found')
} else {
if (updated_at_flag) {
latest_update_date = moment(pg_update_res.rows[0].updated_at)
.add(1, 'seconds')
.toISOString()
console.log('latest updated_at date: ' + latest_update_date)
}
if (created_at_flag) {
latest_create_date = moment(pg_create_res.rows[0].created_at)
.add(1, 'seconds')
.toISOString()
console.log('latest created_at date: ' + latest_create_date)
}
}
cb()
}
async function mongoConnect(cb) {
MongoConnection.connectToServer(function (err, client) {
console.log('mongo connect')
if (err) console.log(err);
db = MongoConnection.getDb();
cb()
})
}
// -------------------------------------------------------
async function startMongoExtract(queryType, cMessage, cb) {
console.log(queryType)
if ((no_data_flag == 'yes' && queryType == 'all_data') || (no_data_flag != 'yes' && ((queryType == 'existing_data' && updated_at_flag) || (queryType == 'new_data' && created_at_flag) ))) {
let count = null
let found = null
let limit = 250
let max = 10000
while ((found === null || found == limit) && count < max) {
console.log(found, limit)
console.log(table_name)
console.log(queryType)
mongo_data = await new Promise((resolve, reject) => {
if (queryType == 'new_data' && created_at_flag) {
db.collection(table_name)
.find({ created_at: { $gte: new Date(latest_create_date) } })
.sort({ created_at: 1 })
.skip(count === null ? 0 : count)
.limit(limit)
.toArray((err, items) => {
resolve(items)
});
} else if (queryType == 'existing_data' && updated_at_flag) {
db.collection(table_name)
.find({ updated_at: { $gte: new Date(latest_update_date) } })
.sort({ updated_at: 1 })
.skip(count === null ? 0 : count)
.limit(limit)
.toArray((err, items) => {
resolve(items)
});
} else if (queryType == 'all_data') {
db.collection(table_name)
.find({})
// .sort({ created_at: 1 })
.skip(count === null ? 0 : count)
.limit(limit)
.toArray((err, items) => {
resolve(items)
});
} else {
reject('error')
}
})
let rows = []
for (let md in mongo_data) {
try {
let data_row = mongo_data[md]
// console.log(md)
var insert_row = []
for (let j = 0; j < columns.length; j++) {
// custom rules applied in this switch statement if needed, otherwise default will be used
// -------------------------------------------------------
// console.log('-------------------------------------------------------')
// console.log(data_row);
// console.log('-------------------------------------------------------')
switch (columns[j]) {
// custom rule for extracting value from child level i.e. 'common' that is stored in the 'name' object
case 'products':
insert_row.push(
json_key(data_row.name, 'common', j)
)
break
default:
// console.log('-------------------------------------------------------')
// console.log(insert_row);
// console.log(columns[j])
// console.log('-------------------------------------------------------')
insert_row.push(
json_key(data_row, columns[j].replace(/"/g, ''), j)
)
}
// -------------------------------------------------------
}
const { products } = data_row;
if ( products ) {
//console.log(data_row.products)
for (let index = 0; index < products.length; index++) {
const element = products[index];
const data = await PostgresConnection().query(productSelect(), [element])
// console.log(data)
if (!data.rows[0]) {
const r = await PostgresConnection().query(productInsert(), [element])
// console.log(r)
}
}
}
} catch (e) {
console.log(e)
}
rows.push(insert_row)
}
found = mongo_data.length
count += found
console.log('COUNT:' + count)
console.log('FOUND:' + found)
console.log('ROWS:' + rows.length)
for (r in rows) {
try {
let values = rows[r]
if ( (queryType == 'new_data' && created_at_flag) || queryType == 'all_data') {
const data = await PostgresConnection().query(pgInsertStatement, values)
// console.log(data.rows[0]._id)
} else if (queryType == 'existing_data' && updated_at_flag) {
const data = await PostgresConnection().query(pgUpdateStatement, values)
// console.log(data.rows)
} else {
console.log('No query type')
}
} catch (err) {
console.log(err.stack)
}
}
console.log(table_name + ' data copied successfully')
}
console.log(cMessage)
}
cb()
//})
}
// -----------------------------
async.forEachLimit(
models_index,
1,
function (m, modelcb) {
waterfall([
async.apply(model_transform, m),
pgGenerate,
pgExtract,
mongoConnect,
async.apply(startMongoExtract, 'all_data', 'All Data Inserted'),
async.apply(startMongoExtract, 'new_data', 'New Data Inserted'),
async.apply(startMongoExtract, 'existing_data', 'Updated Data Inserted'),
function (cb) {
cb()
}
],
function (err, result) {
console.log('model ' + m + ' complete')
modelcb()
}
)
},
function (err) {
console.log(err)
console.log('complete')
MigrationIsRunning = false
if (typeof complete === 'function') setTimeout(complete, 1000)
}
)
}