diff --git a/backend/src/main/database/DatabaseManager.js b/backend/src/main/database/DatabaseManager.js index 2cead150..97221fd6 100644 --- a/backend/src/main/database/DatabaseManager.js +++ b/backend/src/main/database/DatabaseManager.js @@ -8,7 +8,7 @@ class DatabaseManager { port: process.env.MYSQL_PORT || 3306, user: process.env.MYSQL_USER || 'root', password: process.env.MYSQL_PW || 'admin', - database: process.env.MYSQL_DB, + database: process.env.MYSQL_DB || 'carpal', multipleStatements: true }; } diff --git a/backend/src/main/database/RefreshDatabase.js b/backend/src/main/database/RefreshDatabase.js index 46f731e1..e7c9f9ee 100644 --- a/backend/src/main/database/RefreshDatabase.js +++ b/backend/src/main/database/RefreshDatabase.js @@ -1,8 +1,9 @@ const fs = require('fs'); const path = require('path'); +const DatabaseManager = require('./DatabaseManager'); +const createDB = '2018-08-04-1-create-database.sql'; const changeSet = [ - '2018-08-04-1-create-database.sql', '2018-08-04-2-create-location-table.sql', '2018-08-04-3-create-rides-table.sql', '2018-08-04-4-create-driver-table.sql', @@ -12,18 +13,27 @@ const changeSet = [ class RefreshDatabase { constructor(databaseManager) { + const dbConfig = Object.assign({}, databaseManager.databaseConfig); + + delete dbConfig.database; + this.databaseManagerNoDb = new DatabaseManager(dbConfig); this.databaseManager = databaseManager; } async executeAll() { - for (let fileName of changeSet) { - await this.execute(fileName); - } + this.execute(createDB, this.databaseManagerNoDb) + .then(() => { + let result = Promise.resolve(); + changeSet.forEach(fileName => { + result = result.then(this.execute(fileName, this.databaseManager)) + }); + return result; + }) } - async execute(fileName) { + execute(fileName, databaseManager) { let sql = fs.readFileSync(path.resolve(__dirname, './changes/' + fileName)).toString().trim(); - return this.databaseManager.query(sql); + return databaseManager.query(sql); } } diff --git a/backend/src/main/database/changes/2018-08-04-1-create-database.sql b/backend/src/main/database/changes/2018-08-04-1-create-database.sql index 95e1904f..62719596 100644 --- a/backend/src/main/database/changes/2018-08-04-1-create-database.sql +++ b/backend/src/main/database/changes/2018-08-04-1-create-database.sql @@ -1 +1 @@ -CREATE DATABASE IF NOT EXISTS carpal; \ No newline at end of file +CREATE SCHEMA IF NOT EXISTS carpal; \ No newline at end of file