-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
131 lines (126 loc) · 2.98 KB
/
index.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
const process = require('process');
const fs = require('fs');
const from = process.argv[2];
const to = process.argv[3];
const tableName = process.argv[4];
console.log(from, to);
const knex = require('knex');
let knexFrom;
if(from.indexOf('@') >= 0) {
const pos = from.lastIndexOf('@')
const [user, password] = from.substr(0, pos).split(':');
const [address, database] = from.substr(pos + 1).split('/');
const host = address.split(':')[0];
const port = address.split(':')[1] || 3306;
knexFrom = knex({
client: 'mysql',
connection: {
host,
port,
user,
password,
database,
charset: 'utf8',
collate: 'utf8_unicode_ci',
},
useNullAsDefault: true,
});
} else {
knexFrom = knex({
client: 'sqlite3',
connection: {
filename: from,
},
useNullAsDefault: true,
});
}
let knexTo;
if(to.indexOf('@') >= 0) {
const pos = to.lastIndexOf('@')
const [user, password] = to.substr(0, pos).split(':');
const [address, database] = to.substr(pos + 1).split('/');
const host = address.split(':')[0];
const port = address.split(':')[1] || 3306;
knexTo = knex({
client: 'mysql',
connection: {
host,
port,
user,
password,
database,
charset: 'utf8',
collate: 'utf8_unicode_ci',
},
useNullAsDefault: true,
});
} else {
knexTo = knex({
client: 'sqlite3',
connection: {
filename: to,
},
useNullAsDefault: true,
});
}
const transDb = name => {
const promises = [];
const table = require('./db/' + name);
return knexTo.schema.createTable(name, table)
.then(success => {
return knexFrom(name).count();
}).then(success => {
const count = success[0]['count(*)'];
const insert = number => {
let promise;
if(name === 'group') {
promise = knexFrom(name).select().where('id', '>', 0).limit(50).offset(number * 50);
} else {
promise = knexFrom(name).select().limit(50).offset(number * 50);
}
return promise
.then(data => {
return knexTo(name).insert(data);
});
};
let i = 0;
const promiseList = () => {
return insert(i).then(success => {
console.log(`${name} ${ i * 50 } - ${ i * 50 + 50 } success`);
i += 1;
if(i < Math.ceil(count/50)) {
return promiseList();
} else {
console.log(`${name} finish\n`);
return;
}
});
};
return promiseList();
});
};
if(tableName) {
transDb(tableName);
} else {
fs.readdir('./db', (err, files) => {
if(err) { return; }
const tables = files.filter(f => {
return f.match(/\.js$/);
}).map(f => {
return f.substr(0, f.length - 3);
});
let i = 0;
const tablePromise = () => {
transDb(tables[i]).then(success => {
i += 1;
if(i >= tables.length) {
console.log('all talbes finish');
return;
} else {
return tablePromise();
}
})
};
tablePromise();
});
}