From 50b72c7f03ebf47a78ed303c7d0d41bf278dedba Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Sun, 11 Dec 2016 12:07:46 +0800 Subject: [PATCH 1/3] Fix heroku production db setting --- bin/heroku | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/bin/heroku b/bin/heroku index f30b2b2..b0d0797 100755 --- a/bin/heroku +++ b/bin/heroku @@ -18,8 +18,38 @@ module.exports = { } EOF + regex='postgres:\/\/([^:]+):([^@]+)@([^:]+):([^\/]+)\/(.+)' - cat << EOF > config.json + if [[ $DATABASE_URL =~ $regex ]]; then + + username="${BASH_REMATCH[1]}" + password="${BASH_REMATCH[2]}" + host="${BASH_REMATCH[3]}" + port="${BASH_REMATCH[4]}" + database="${BASH_REMATCH[5]}" + + cat << EOF > config.json + +{ + "production": { + "db": { + "dialect": "postgres", + "host": "${host}", + "username": "${username}", + "password": "${password}", + "database": "${database}", + "port": "${port}", + "dialectOptions": { + "ssl": true + } + } + } +} + +EOF + else + + cat << EOF > config.json { "production": { @@ -28,6 +58,9 @@ EOF EOF + fi + + cp public/js/config.js.example public/js/config.js # build app From 74c1da4536285593f3172f8be6a22ad300f73348 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Mon, 12 Dec 2016 10:18:12 +0800 Subject: [PATCH 2/3] Simplify output with sequelize database argument --- bin/heroku | 31 ++----------------------------- lib/models/index.js | 10 ++++++++-- 2 files changed, 10 insertions(+), 31 deletions(-) diff --git a/bin/heroku b/bin/heroku index b0d0797..0375e9e 100755 --- a/bin/heroku +++ b/bin/heroku @@ -18,27 +18,13 @@ module.exports = { } EOF - regex='postgres:\/\/([^:]+):([^@]+)@([^:]+):([^\/]+)\/(.+)' - if [[ $DATABASE_URL =~ $regex ]]; then - - username="${BASH_REMATCH[1]}" - password="${BASH_REMATCH[2]}" - host="${BASH_REMATCH[3]}" - port="${BASH_REMATCH[4]}" - database="${BASH_REMATCH[5]}" - - cat << EOF > config.json + cat << EOF > config.json { "production": { "db": { - "dialect": "postgres", - "host": "${host}", - "username": "${username}", - "password": "${password}", - "database": "${database}", - "port": "${port}", + "database": "${DATABASE_URL}", "dialectOptions": { "ssl": true } @@ -47,19 +33,6 @@ EOF } EOF - else - - cat << EOF > config.json - -{ - "production": { - } -} - -EOF - - fi - cp public/js/config.js.example public/js/config.js diff --git a/lib/models/index.js b/lib/models/index.js index 3b49d45..60eedf6 100644 --- a/lib/models/index.js +++ b/lib/models/index.js @@ -11,7 +11,13 @@ var logger = require("../logger.js"); var dbconfig = config.db; dbconfig.logging = config.debug ? logger.info : false; -var sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig); + +var sequelize; +if (typeof dbconfig.username === 'undefined' && typeof dbconfig.password === 'undefined') { + sequelize = new Sequelize(dbconfig.database, dbconfig); +} else { + sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig); +} var db = {}; @@ -34,4 +40,4 @@ Object.keys(db).forEach(function (modelName) { db.sequelize = sequelize; db.Sequelize = Sequelize; -module.exports = db; \ No newline at end of file +module.exports = db; From 5282bf491e299e50f65484d987558e7bfc031c4b Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Mon, 12 Dec 2016 11:12:59 +0800 Subject: [PATCH 3/3] Update sequelize init condition --- lib/models/index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/models/index.js b/lib/models/index.js index 60eedf6..d52f550 100644 --- a/lib/models/index.js +++ b/lib/models/index.js @@ -12,12 +12,11 @@ var logger = require("../logger.js"); var dbconfig = config.db; dbconfig.logging = config.debug ? logger.info : false; -var sequelize; -if (typeof dbconfig.username === 'undefined' && typeof dbconfig.password === 'undefined') { - sequelize = new Sequelize(dbconfig.database, dbconfig); -} else { +var sequelize = null; +if (dbconfig.hasOwnProperty('username') || dbconfig.hasOwnProperty('password')) sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig); -} +else + sequelize = new Sequelize(dbconfig.database, dbconfig); var db = {};