-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (70 loc) · 2.35 KB
/
Copy pathindex.js
File metadata and controls
84 lines (70 loc) · 2.35 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
const sql = require('mysql2/promise');
const { readFile, writeFile } = require('node:fs').promises;
const path = require('path');
require('dotenv').config();
const JSON_LIST_FILE_NAME = process.env.JSON_LIST_FILE_NAME || null;
const DB_CONFIG = {
"host": process.env.DB_HOST,
"user": process.env.DB_USER,
"password": process.env.DB_PASSWORD,
"port": process.env.DB_PORT,
"database": process.env.DB_NAME
};
async function main() {
if (process.env === undefined) {
console.error('No .env file found.');
process.exit(2);
}
if (JSON_LIST_FILE_NAME === null) {
console.error('JSON_LIST_FILE_NAME not set in .env file.');
process.exit(3);
}
const connection = await sql.createConnection(DB_CONFIG).catch((error) => {
console.error(error);
process.exit(4);
});
const parsedFileList = [];
try {
const filePath = JSON_LIST_FILE_NAME.startsWith('abs:')
? JSON_LIST_FILE_NAME.slice(4)
: path.join(__dirname, JSON_LIST_FILE_NAME);
const file = await readFile(filePath, 'utf-8');
parsedFileList.push(...JSON.parse(file));
console.log('File read successfully');
} catch (error) {
console.error('Error reading input file');
console.error(error);
process.exit(5);
}
const errors = [];
let i = 0;
console.log('Executing statements...');
for (const statement of parsedFileList) {
try {
await connection.query(statement);
console.log('Statement ' + i + ' executed successfully.');
} catch (error) {
console.error('Statement ' + i + ' failed.');
console.error(error);
errors.push(error);
}
i++;
}
if (errors.length === 0) {
console.log('\n\nNo errors detected\n\n');
process.exit(0);
}
try {
const errorFileName = JSON_LIST_FILE_NAME.sta
await writeFile(join(__dirname, errorFileName), JSON.stringify(errors), 'utf-8');
console.log('Errors written to file');
process.exit(1);
} catch (error) {
console.error('Error writing errors to file, printing to console instead! \n\n Error:');
console.error(error);
console.log('\n\nSQL Statements that failed:');
console.log(errors);
process.exit(6);
}
}
main();