diff --git a/.openshift/postgresql-template-persistent.json b/.openshift/postgresql-template-persistent.json index 34f0a215..32b601ea 100644 --- a/.openshift/postgresql-template-persistent.json +++ b/.openshift/postgresql-template-persistent.json @@ -406,7 +406,7 @@ "volumeMounts": [ { "name": "${DATABASE_SERVICE_NAME}-data", - "mountPath": "/var/lib/postgresql/data" + "mountPath": "/var/lib/pgsql/data" } ] } diff --git a/lib/db/index.js b/lib/db/index.js index 4cc285c7..c1cd3087 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -2,8 +2,8 @@ const { Pool } = require('pg'); const serviceHost = process.env.MY_DATABASE_SERVICE_HOST || process.env.POSTGRESQL_SERVICE_HOST || 'localhost'; -const user = process.env.DB_USERNAME || process.env.POSTGRESQL_USER || 'user'; -const password = process.env.DB_PASSWORD || process.env.POSTGRESQL_PASSWORD || 'password'; +const user = process.env.DB_USERNAME || process.env.POSTGRESQL_USER || 'luke'; +const password = process.env.DB_PASSWORD || process.env.POSTGRESQL_PASSWORD || 'secret'; const databaseName = process.env.POSTGRESQL_DATABASE || 'my_data'; const connectionString = `postgresql://${user}:${password}@${serviceHost}:5432/${databaseName}`; @@ -11,7 +11,17 @@ const pool = new Pool({ connectionString }); -let didInitHappen = false; +async function didInitHappen () { + const query = 'select * from products'; + + try { + await pool.query(query); + console.log('Database Already Created'); + return true; + } catch (err) { + return false; + } +} // -- Create the products table if not present const initScript = `CREATE TABLE IF NOT EXISTS products ( @@ -28,17 +38,19 @@ INSERT INTO products (name, stock) values ('Pear', 10);`; async function query (text, parameters) { // Check that we have initialized the DB on each Query request - if (!didInitHappen) { + const initHappened = await didInitHappen(); + if (!initHappened) { await init(); } return pool.query(text, parameters); } -function init () { - return pool.query(initScript).then(() => { - didInitHappen = true; - }); +async function init () { + const initHappened = await didInitHappen(); + if (!initHappened) { + return pool.query(initScript); + } } module.exports = {