From 3cb7bdf633d26f855d95833cf6e78996d61223aa Mon Sep 17 00:00:00 2001 From: Kael Shipman Date: Fri, 21 Jan 2022 14:34:21 -0600 Subject: [PATCH] Feat: Implemented default values for undefined env vars closes db-migrate/node-db-migrate#765 Signed-off-by: Kael Shipman --- lib/config.js | 31 ++++++++++++++++++++------- test/config_test.js | 37 +++++++++++++++++++++++++++++++++ test/database_with_env.json | 4 +++- test/database_with_env_url.json | 2 +- 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/lib/config.js b/lib/config.js index f27e641a..b0403c9c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -96,15 +96,26 @@ exports.loadFile = function (fileName, currentEnv, plugins) { function walkConfig (level) { for (var configEntry in level) { - if (level[configEntry] && level[configEntry].ENV) { - if (!process.env[level[configEntry].ENV]) { - log.verbose( - 'Environment variable ' + level[configEntry].ENV + ' is empty!' - ); + if (!level[configEntry]) { + continue; + // If we're asking to get the value from an env var + } else if (level[configEntry].ENV) { + // If the env var is set, use it + if (typeof process.env[level[configEntry].ENV] !== 'undefined') { + level[configEntry] = process.env[level[configEntry].ENV]; + } else { + // Otherwise, if a default is provided, use that + log.verbose('Environment variable ' + level[configEntry].ENV + ' is empty!'); + if (typeof level[configEntry].default !== 'undefined') { + log.verbose('Using default value provided.'); + level[configEntry] = level[configEntry].default; + } else { + // Otherwise, there's no var and there's no default - set it to undefined + level[configEntry] = undefined; + } } - - level[configEntry] = process.env[level[configEntry].ENV]; - } else if (level[configEntry] && typeof level[configEntry] === 'object') { + } else if (typeof level[configEntry] === 'object') { + // If we're NOT asking to get the value from an env var and the value is an object, recurse level[configEntry] = walkConfig(level[configEntry]); } } @@ -121,6 +132,10 @@ exports.loadObject = function (_config, currentEnv) { if (config[env].ENV) { if (!process.env[config[env].ENV]) { log.verbose('Environment variable ' + config[env].ENV + ' is empty!'); + if (typeof config[env].default !== 'undefined') { + log.verbose('Using default value provided.'); + out[env] = parseDatabaseUrl(config[env].default); + } } else { out[env] = parseDatabaseUrl(process.env[config[env].ENV]); } diff --git a/test/config_test.js b/test/config_test.js index a9b5f9ee..facaaa0f 100644 --- a/test/config_test.js +++ b/test/config_test.js @@ -90,6 +90,10 @@ lab.experiment('config', function () { function () { process.env.DB_MIGRATE_TEST_VAR = 'username_from_env'; + process.env.EMPTY_VAR = ''; + if (typeof process.env.NOT_SET !== 'undefined') { + delete process.env.NOT_SET; + } var configPath = path.join(__dirname, 'database_with_env.json'); var _config = config.load(configPath, 'prod'); @@ -98,6 +102,16 @@ lab.experiment('config', function () { Code.expect(_config.prod.username).to.equal('username_from_env'); } ); + lab.test( + 'should load default value from not set env var', () => { + Code.expect(_config.prod.password).to.equal('my-password'); + } + ); + lab.test( + 'should use value from env var even when empty, rather than default', () => { + Code.expect(_config.prod.host).to.equal(''); + } + ); } ); @@ -122,6 +136,29 @@ lab.experiment('config', function () { } ); + lab.experiment( + 'loading from a file from default when ENV URL is not set', + + function () { + if (typeof process.env.DB_MIGRATE_TEST_VAR !== "undefined") { + delete process.env.DB_MIGRATE_TEST_VAR; + } + var configPath = path.join(__dirname, 'database_with_env_url.json'); + var _config = config.load(configPath, 'prod'); + + lab.test( + 'should load the url from default when env var not set', () => { + var current = _config.getCurrent(); + Code.expect(current.settings.driver).to.equal('postgres'); + Code.expect(current.settings.user).to.equal('uname'); + Code.expect(current.settings.password).to.equal('pw'); + Code.expect(current.settings.host).to.equal('server.com'); + Code.expect(current.settings.database).to.equal('dbname'); + } + ); + } + ); + lab.experiment('loading from an URL', function () { var databaseUrl = 'postgres://uname:pw@server.com/dbname'; var _config = config.loadUrl(databaseUrl, 'dev'); diff --git a/test/database_with_env.json b/test/database_with_env.json index 6f2d5fc7..51c53683 100644 --- a/test/database_with_env.json +++ b/test/database_with_env.json @@ -2,6 +2,8 @@ "prod": { "driver": "sqlite3", "filename": "prod.db", - "username": {"ENV": "DB_MIGRATE_TEST_VAR"} + "username": {"ENV": "DB_MIGRATE_TEST_VAR"}, + "password": {"ENV": "NOT_SET", "default": "my-password"}, + "host": {"ENV": "EMPTY_VAR", "default": "my-host"} } } diff --git a/test/database_with_env_url.json b/test/database_with_env_url.json index cf993432..6eafb34d 100644 --- a/test/database_with_env_url.json +++ b/test/database_with_env_url.json @@ -1,3 +1,3 @@ { - "prod": {"ENV": "DB_MIGRATE_TEST_VAR"} + "prod": {"ENV": "DB_MIGRATE_TEST_VAR", "default": "postgres://uname:pw@server.com/dbname"} }