46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
const app = require('../app');
|
|
const { Client } = require('pg');
|
|
const fs = require('fs');
|
|
|
|
if (process.env.NODE_ENV !== 'production' || process.env.NODE_ENV !== 'testing') {
|
|
require('dotenv').config();
|
|
}
|
|
|
|
const client = new Client();
|
|
client.connect();
|
|
|
|
async function executeQuery(query, values = []) {
|
|
const result = await client.query({
|
|
rowMode: 'array',
|
|
text: query,
|
|
values: values
|
|
});
|
|
return result.rows;
|
|
}
|
|
|
|
async function Initialize() {
|
|
console.log("Initializing database...")
|
|
const sql = fs.readFileSync('database/init_database.sql').toString();
|
|
await executeQuery(sql);
|
|
console.log("Database initialized.")
|
|
}
|
|
|
|
|
|
async function checkForDatabaseInitialization() {
|
|
const scoresSchemaExistsQuery = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`;
|
|
let result = await executeQuery(scoresSchemaExistsQuery);
|
|
|
|
const scoresSchemaExists = result.length !== 0;
|
|
|
|
if(!scoresSchemaExists) {
|
|
await Initialize();
|
|
}
|
|
}
|
|
const initializationStatus = checkForDatabaseInitialization();
|
|
|
|
|
|
|
|
|
|
|
|
exports.executeQuery = executeQuery;
|
|
exports.initializationStatus = initializationStatus; |