Skip to content
Snippets Groups Projects
Commit e63dae39 authored by Federico Hurtado's avatar Federico Hurtado
Browse files

Move table init to DatabaseInitializer

parent 4206f367
No related branches found
No related tags found
1 merge request!11Basic functionality achieved of generalized search function. Need to define...
......@@ -23,46 +23,4 @@ console.log("Database Host: ", process.env.DB_HOST);
console.log("Database user: ", process.env.DB_USER);
console.log("Database name: ", process.env.DB_NAME);
// Function to execute the SQL file at initilization
const executeSQLFile = (filePath) => {
const sql = fs.readFileSync(filePath, "utf8");
// Split SQL into individual statements
const sqlStatements = sql.split(/;\s*$/m);
// connect to MySQL
pool.getConnection((err, connection) => {
if (err) {
console.error("Error connecting to MySQL:", err.message);
return;
}
// successfully connected to DB
console.log("connected to DB, executing schema...");
// Execute each SQL statement in the file one by one
sqlStatements.forEach((statement) => {
if (statement.trim()) {
// Ignore empty statements
connection.query(statement, (err, result) => {
if (err) {
console.error("Error executing SQL statement:", err.message);
// connection.release();
return;
}
});
}
console.log("Just added: ", statement);
});
// release connection after all statements
connection.release();
});
};
// Execute the SQL file during startup
const schemaFilePath = path.join(__dirname, "./schema.sql");
executeSQLFile(schemaFilePath);
module.exports = { pool: pool.promise() };
......@@ -16,6 +16,8 @@ const {
checkChapterTableExistence,
addChapterIfNotExists,
} = require("../../repository/chapterRepository");
const fs = require("fs").promises;
const path = require("path");
/*
Class responsible for populating the database
......@@ -27,6 +29,32 @@ class DatabaseInitializer {
this.pool = pool;
}
/*
Execute SQL schema file to create necessary tables on startup.
*/
async executeSQLFile(filePath) {
try {
const sql = await fs.readFile(filePath, "utf8");
const sqlStatements = sql.split(/;\s*$/m);
const connection = await this.pool.getConnection();
console.log("Connected to DB, executing schema...");
for (const statement of sqlStatements) {
if (statement.trim()) {
await connection.query(statement);
console.log("Executed statement:", statement);
}
}
connection.release();
} catch (err) {
console.error("Error executing SQL schema file:", err.message);
throw err;
}
}
/*
Method for ensuring database is accessible.
*/
......@@ -374,6 +402,10 @@ class DatabaseInitializer {
async initializeAll() {
await this.testDBConnection();
const schemaFilePath = path.join(__dirname, "./schema.sql");
await this.executeSQLFile(schemaFilePath);
await this.initializeRoles();
// await this.initializeTestUsers(); // no not initialize test users anymore
await this.initializeStates();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment