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

Merge remote-tracking branch 'origin/main' into feature/update-person

parents c4860d1f 084cf810
No related branches found
No related tags found
1 merge request!8Feature/activity logger
...@@ -52,6 +52,8 @@ const executeSQLFile = (filePath) => { ...@@ -52,6 +52,8 @@ const executeSQLFile = (filePath) => {
} }
}); });
} }
console.log("Just added: ", statement);
}); });
// release connection after all statements // release connection after all statements
......
...@@ -2,6 +2,10 @@ const { addUser, findByUsername } = require("../../repository/userRepository"); ...@@ -2,6 +2,10 @@ const { addUser, findByUsername } = require("../../repository/userRepository");
const { const {
addRoleIfNotExists, addRoleIfNotExists,
} = require("../../repository/rolesPermissionRepository"); } = require("../../repository/rolesPermissionRepository");
const {
addStateIfNotExists,
checkTableExistence,
} = require("../../repository/stateRepository");
/* /*
Class responsible for populating the database Class responsible for populating the database
...@@ -66,10 +70,74 @@ class DatabaseInitializer { ...@@ -66,10 +70,74 @@ class DatabaseInitializer {
} }
} }
/*
Creates all the states
*/
async initializeStates() {
checkTableExistence();
const states = [
"AL",
"AK",
"AZ",
"AR",
"CA",
"CO",
"CT",
"DE",
"FL",
"GA",
"HI",
"ID",
"IL",
"IN",
"IA",
"KS",
"KY",
"LA",
"ME",
"MD",
"MA",
"MI",
"MN",
"MS",
"MO",
"MT",
"NE",
"NV",
"NH",
"NJ",
"NM",
"NY",
"NC",
"ND",
"OH",
"OK",
"OR",
"PA",
"RI",
"SC",
"SD",
"TN",
"TX",
"UT",
"VT",
"VA",
"WA",
"WV",
"WI",
"WY",
];
for (const state of states) {
await addStateIfNotExists(state);
}
}
async initializeAll() { async initializeAll() {
await this.testDBConnection(); await this.testDBConnection();
await this.initializeRoles(); await this.initializeRoles();
await this.initializeTestUsers(); await this.initializeTestUsers();
await this.initializeStates();
console.log("database init completed."); console.log("database init completed.");
} }
......
...@@ -119,9 +119,6 @@ CREATE TABLE IF NOT EXISTS peopleXInvolvement ( ...@@ -119,9 +119,6 @@ CREATE TABLE IF NOT EXISTS peopleXInvolvement (
FOREIGN KEY (involvementId) REFERENCES involvementLookup(involvementId) FOREIGN KEY (involvementId) REFERENCES involvementLookup(involvementId)
); );
INSERT INTO stateLookup (stateId, name)
VALUES (1, 'Virginia');
INSERT INTO countryLookup (countryId, name) INSERT INTO countryLookup (countryId, name)
VALUES (1, 'United States'); VALUES (1, 'United States');
......
const { pool } = require("../config/database/database.config");
/*
Class responsible for making queries to the
stateLookup database.
*/
/*
Function to add a state to the database
state must be a string and
*/
async function addStateIfNotExists(state) {
console.log("adding state: ", state);
// don't add state if it exists
const existingState = await getState(state);
if (existingState) {
console.log("State already exists: ", state);
return;
}
// insert SQL query
const query = `INSERT INTO stateLookup (name) VALUES (?)`;
// attempt to insert state into table
try {
await pool.query(query, [state]);
console.log(`Successfully added state: ${state}`);
} catch (error) {
console.error(`Error adding state: ${state}`, error);
throw error;
}
}
async function getState(name) {
// SQL query to get a certain state
const query = "SELECT * FROM stateLookup WHERE name = ?";
try {
console.log("Getting state: ", name);
const [rows] = await pool.query(query, [name]);
if (rows.length > 0) {
console.log("Found state: ", name);
return rows[0];
} else {
console.log("State not found: ", name);
return null;
}
} catch (error) {
console.error(`Error fetching state with ID ${name}:`, error);
throw error;
}
}
async function checkTableExistence() {
try {
const [rows] = await pool.query("SHOW TABLES LIKE 'stateLookup'");
if (rows.length > 0) {
console.log("Table `stateLookup` exists");
} else {
console.log("Table `stateLookup` does not exist");
}
} catch (error) {
console.error("Error checking table existence:", error);
}
}
module.exports = { checkTableExistence, addStateIfNotExists, getState };
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