-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-mysql.js
117 lines (105 loc) · 2.78 KB
/
mysql-mysql.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
// require table structure in target before dump the data
const mariadb = require("mariadb");
const limit = 300;
const start_row = 0;
const limit_break = 500000;
const db_name = "db_name";
const skipTable = [];
const selectedTable = [];
const connect_target = async () => {
const pool = mariadb.createPool({
host: "127.0.0.1",
user: "user",
password: "xxx",
port: "3306",
database: db_name,
});
return await pool.getConnection();
};
const connect_source = async () => {
const pool = mariadb.createPool({
host: "127.0.0.1",
user: "user",
password: "xxx",
port: "3306",
database: db_name,
});
return await pool.getConnection();
};
const getFields = (row, primary) => {
const fields = [];
const values = [];
let key;
for (var field in row) {
fields.push(field);
values.push(row[field]);
if (field === primary) {
key = row[field];
}
}
return { fields, values };
};
const dump = async (source, target, table, primary, offset) => {
try {
const select = `SELECT * FROM ${table} order by \`${primary}\` desc limit ${limit} offset ${offset}`;
const rows = await source.query(select);
let fields;
const values = [];
for (var i = 0; i < rows.length; i++) {
const data = getFields(rows[i], primary);
fields = data.fields;
values.push(data.values);
}
target.batch(
`INSERT IGNORE ${table} (${fields.join(",")}) VALUE (${fields
.map((i) => "?")
.join(",")})`,
values
);
await target.commit();
console.log("commit data", table, rows.length, "/", offset);
return rows.length === limit;
} catch (e) {
console.log("error", e.message);
return false;
}
};
const getPrimary = (keys) => {
const obj = keys.find((key) => {
return key.Key_name === "PRIMARY";
});
return obj.Column_name;
};
const main = async () => {
const source = await connect_source();
console.log("connected source");
const target = await connect_target();
console.log("connected target");
const tables = await source.query("show tables");
for (var i = 0; i < tables.length; i++) {
const table = tables[i][`Tables_in_${db_name}`];
if (skipTable.includes(table)) {
continue;
}
if (selectedTable.length > 0 && !selectedTable.includes(table)) {
continue;
}
console.log("start table: ", table);
const keys = await source.query(
`SHOW KEYS FROM ${table} WHERE Key_name = "PRIMARY"`
);
const primary = getPrimary(keys);
console.log("table ", table, "has ", primary);
let offset = start_row;
let more;
do {
more = await dump(source, target, table, primary, offset);
offset += limit;
} while (offset < limit_break && more);
}
source.end();
target.end();
};
(async () => {
await main();
})();