2021-11-15 17:39:03 +00:00
|
|
|
const app = require('../app');
|
|
|
|
const { Client } = require('pg');
|
2021-11-18 21:13:03 +00:00
|
|
|
const fs = require('fs');
|
2021-11-15 17:39:03 +00:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production' || process.env.NODE_ENV !== 'testing') {
|
|
|
|
require('dotenv').config();
|
|
|
|
}
|
|
|
|
|
|
|
|
const client = new Client();
|
2021-11-18 21:13:03 +00:00
|
|
|
client.connect();
|
|
|
|
|
|
|
|
async function executeQuery(query, values = []) {
|
|
|
|
const result = await client.query({
|
|
|
|
rowMode: 'array',
|
|
|
|
text: query,
|
|
|
|
values: values
|
|
|
|
});
|
|
|
|
return result.rows;
|
|
|
|
}
|
|
|
|
|
2021-11-20 23:51:49 +00:00
|
|
|
async function Initialize() {
|
2021-11-18 21:13:03 +00:00
|
|
|
console.log("Initializing database...")
|
|
|
|
const sql = fs.readFileSync('database/init_database.sql').toString();
|
|
|
|
await executeQuery(sql);
|
|
|
|
console.log("Database initialized.")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function checkForDatabaseInitialization() {
|
|
|
|
const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`;
|
|
|
|
let result = await executeQuery(query);
|
|
|
|
|
|
|
|
const scoresSchemaExists = result.length !== 0;
|
|
|
|
|
|
|
|
if(!scoresSchemaExists) {
|
2021-11-20 23:51:49 +00:00
|
|
|
Initialize();
|
2021-11-18 21:13:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-20 23:51:49 +00:00
|
|
|
checkForDatabaseInitialization();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.executeQuery = executeQuery;
|