-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewritesql.js
More file actions
61 lines (51 loc) · 1.8 KB
/
Copy pathrewritesql.js
File metadata and controls
61 lines (51 loc) · 1.8 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
const fs = require('fs');
const readline = require('readline');
const tableNamesToSkip = ['_pantheon_heartbeat', 'wp_gf_entry', 'wp_gf_entry_meta', 'wp_2_gf_entry', 'wp_2_gf_entry_meta'];
async function rewriteSql(inputFilePath, outputFilePath) {
console.log('Starting NU database sync...');
// Delete the output file if it already exists
if (fs.existsSync(outputFilePath)) {
fs.unlinkSync(outputFilePath);
console.log(`Deleted existing file ${outputFilePath}`);
}
const input = fs.createReadStream(inputFilePath);
const output = fs.createWriteStream(outputFilePath);
const lineReader = readline.createInterface({
input,
crlfDelay: Infinity,
});
let skipNextLines = false;
let tableNameToDelete = '';
let skipTable = false;
for await (const line of lineReader) {
if (skipTable) {
if (/^UNLOCK TABLES;$/.test(line)) {
skipTable = false;
}
continue;
}
if (skipNextLines) {
if (/^UNLOCK TABLES;$/.test(line)) {
skipNextLines = false;
console.log(`Deleted rows in table ${tableNameToDelete}`);
}
} else if (/^LOCK TABLES `.+` WRITE;$/.test(line)) {
for (const tableName of tableNamesToSkip) {
if (line.includes(tableName)) {
skipNextLines = true;
tableNameToDelete = tableName;
break;
}
}
} else if (/^DROP TABLE IF EXISTS `_pantheon_heartbeat`;$/.test(line) || /^CREATE TABLE `_pantheon_heartbeat`/.test(line)) {
skipTable = true;
} else if (/^INSERT INTO `_pantheon_heartbeat`/.test(line)) {
// Skip insert statements for _pantheon_heartbeat table
console.log("Dropping _pantheon_heartbeat table...");
} else {
output.write(`${line}\n`);
}
}
console.log('NU database sync completed.');
}
module.exports = { rewriteSql };