Skip to content

Commit 108614b

Browse files
committed
Complete the porting script framework
- Generalized to make porting easier by changing only the transform.js file which is nothing but json.
1 parent 68c55df commit 108614b

File tree

3 files changed

+138
-35
lines changed

3 files changed

+138
-35
lines changed

tools/port_script/index.js

+109-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const mysql = require('mysql');
22
const config = require('./config.js');
33
const transform = require('./transform.js');
4+
const _ = require('lodash');
5+
6+
const DEBUG = 1;
47

58
// Connect to the source database
69
const source = mysql .createConnection({
@@ -27,43 +30,117 @@ dest.connect(function(err) {
2730
});
2831

2932

30-
for (var item in transform) {
31-
console.log(transform[item].table.old);
32-
source.query('SELECT * FROM ' + transform[item].table.old + ';',
33-
function(err, res, fields) {
34-
if (err) { throw err; }
35-
console.log('\nCopying table ' + transform[item].table.old);
36-
for (var i in res) {
37-
let query = 'INSERT INTO ';
38-
query += transform[item].table.new;
39-
query += ' (';
40-
41-
// Load fields in order
42-
let values = '';
43-
let count = 0;
44-
for (var newField in transform[item].fields) {
45-
if (count != 0) {
46-
query += ', ';
47-
values += ', ';
33+
_.forEach(transform, (rule) => {
34+
let select_query = null;
35+
if (rule.table.old.union) {
36+
console.log('union');
37+
select_query = 'SELECT * FROM ' + rule.table.old.union[0] + ' INNER JOIN (';
38+
39+
let count = 0;
40+
_.forEach(rule.table.old.union, (table) => {
41+
42+
if (count == 0) {
43+
console.log('hi');
44+
} else if (count == 1) {
45+
select_query += table;
46+
} else {
47+
select_query += ', ' + table;
48+
}
49+
count++;
50+
});
51+
52+
select_query += ') ON (' + rule.table.old.on + ')';
53+
} else {
54+
select_query = 'SELECT * FROM ' + rule.table.old;
55+
}
56+
DEBUG && (select_query += ' LIMIT 5');
57+
select_query += ';';
58+
console.log(select_query);
59+
60+
// Run the query
61+
source.query(select_query, function(err, tuples, fields) {
62+
63+
if (err) { throw err; }
64+
console.log('\nCopying table ' + rule.table.new);
65+
66+
_.forEach(tuples, (tuple) => {
67+
68+
let query = 'INSERT INTO ';
69+
query += rule.table.new;
70+
query += ' (';
71+
72+
// Load fields in order
73+
let values = '';
74+
let count = 0;
75+
_.forEach(rule.fields, (field, field_name) => {
76+
77+
// Add comma if not the first item
78+
if (count != 0) {
79+
query += ', ';
80+
values += ', ';
81+
}
82+
83+
// Add field name to query
84+
query += field_name;
85+
86+
// TODO: Add various data cleaning techniques here
87+
let attribute = field;
88+
89+
// check if there's a condition
90+
if (attribute.if) {
91+
92+
if (condition_eval(attribute.if.condition, tuple)) {
93+
values += '"' + attribute.if.pass.value + '"';
94+
} else {
95+
values += '"' + attribute.if.fail.value + '"';
4896
}
49-
query += newField;
5097

51-
// Add various data cleaning techniques here
52-
values += '"' + res[i][transform[item].fields[newField]] + '"';
98+
} else {
99+
values += '"' + tuple[field] + '"';
53100
count++;
54101
}
102+
}); // end forEach(fields)
55103

56-
query += ') VALUES (' + values;
57-
query += ');';
104+
query += ') VALUES (' + values;
105+
query += ');';
58106

59-
// Run the query
60-
dest.query(query, function(err, ans) {
61-
if (err) { throw err; }
62-
else {
63-
console.log(query);
64-
}
65-
});
66-
}
107+
// Run the query
108+
DEBUG && console.log(query + '\n');
109+
DEBUG || dest.query(query, function(err, ans) {
110+
if (err) { throw err; }
111+
else {
112+
console.log(query);
113+
}
114+
});
115+
116+
}); // end of forEach(res)
117+
});
118+
});
119+
120+
function condition_eval(condition, data) {
121+
if (condition.eval) {
122+
let first = data[condition.eval.first_attribute] || condition.eval.first_value || null;
123+
let second = data[condition.eval.second_attribute] || condition.eval.second_value || null;
124+
125+
switch (condition.eval.operator) {
126+
case '==':
127+
case '===':
128+
if (first == second) {
129+
return true;
130+
} else {
131+
return false;
132+
}
133+
break;
134+
default:
135+
throw 'Operator not supported';
67136
}
68-
);
137+
} else if (condition.or) {
138+
// TODO: make this variable
139+
return condition_eval(condition.or[0], data) || condition_eval(condition.or[1], data);
140+
} else if (condition.and) {
141+
// TODO: make this variable
142+
return condition_eval(condition.or[0], data) && condition_eval(condition.or[1], data);
143+
} else {
144+
throw 'Could not find eval or a condition';
145+
}
69146
}

tools/port_script/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"homepage": "https://gist.github.com/3677845cf74d5018c7706701bb0a851c",
2020
"dependencies": {
21+
"lodash": "^4.17.4",
2122
"mysql": "^2.14.1"
2223
}
2324
}

tools/port_script/transform.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
1-
21
module.exports = {
32
0 : {
43
table: {
5-
old: 'student_info',
4+
old: {
5+
union: ['student_info', 'cgpa'],
6+
on: 'student_info.login_id = cgpa.login_id'
7+
},
68
new: 'student'
79
},
810
fields: {
911
id: 'login_id',
1012
studentName: 'student_name',
11-
bitsId: 'id'
13+
bitsId: 'id',
14+
hostelPs: {
15+
if: {
16+
condition: {
17+
or: [
18+
{ eval: { operator: '===', first_attribute: 'hostel', second_value: 'PS2' }, },
19+
{ eval: { operator: '===', first_attribute: 'hostel', second_value: 'THESIS' } },
20+
]
21+
},
22+
pass: { value: 1 },
23+
fail: { value: 0 }
24+
},
25+
},
26+
gender: 'gender',
27+
bDay: 'bday',
28+
phone: 'phone',
29+
email: 'email',
30+
address: 'address',
31+
bloodGroup: 'bloodgroup',
32+
cgpa: 'cgpa',
33+
admit: 'admit',
34+
parentName: 'father_name',
35+
parentPhone: 'father_phone',
36+
parentEmail: 'father_email'
1237
}
1338
}
1439
}

0 commit comments

Comments
 (0)