-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdebug_import.js
More file actions
86 lines (75 loc) · 2.77 KB
/
debug_import.js
File metadata and controls
86 lines (75 loc) · 2.77 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
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
require('dotenv').config();
const mysql = require('mysql2');
const fs = require('fs');
const path = require('path');
const config = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
ssl: process.env.DB_SSL === 'true' ?
(process.env.DB_SSL_CA_PATH ? { ca: fs.readFileSync(process.env.DB_SSL_CA_PATH) } : { rejectUnauthorized: false })
: undefined,
multipleStatements: true
};
const conn = mysql.createConnection(config);
async function run() {
conn.connect(async (err) => {
if (err) {
console.error('Connection failed:', err);
return;
}
console.log('Connected to Aiven.');
// Check data count
conn.query("SELECT COUNT(*) as count FROM category", (err, results) => {
if (err) {
// Table might not exist
console.log('Check failed (table might be missing):', err.message);
importData();
} else {
const count = results[0].count;
console.log(`Current 'category' count: ${count}`);
if (count === 0) {
console.log('Data appears missing. Re-importing...');
importData();
} else {
console.log('Data exists! The database seems fine.');
conn.end();
}
}
});
});
}
function importData() {
const sqlPath = path.join(__dirname, 'sql', 'keeppley-shop.sql');
const sql = fs.readFileSync(sqlPath, 'utf8');
// Split statements to execute one by one for better error handling
// This is a naive split, but works for standard dumps
const statements = sql.split(/;\s*[\r\n]+/).filter(s => s.trim().length > 0);
console.log(`Found ${statements.length} SQL statements. Executing...`);
executeStatements(statements, 0);
}
function executeStatements(statements, index) {
if (index >= statements.length) {
console.log('All statements executed successfully!');
conn.end();
return;
}
conn.query(statements[index], (err) => {
if (err) {
console.error('Error executing statement:', statements[index].substring(0, 100) + '...');
console.error('Error details:', err.message);
// Continue despite error? Depending on error.
// E.g. "Table exists" - ignore.
if (err.code === 'ER_TABLE_EXISTS_ERROR') {
console.log('Table exists, skipping...');
} else {
// Stop on critical error
}
}
// Proceed anyway for now to try to get data in
executeStatements(statements, index + 1);
});
}
run();