Skip to content

Indicating an ENV variable to use in a specific database.json environment throws an error if not set in current environment #411

@fklingler

Description

@fklingler

Following discussion on #391.

With this config file:

{
  "defaultEnv": "development",

  "development": "mongodb://localhost/myproject-dev",
  "production": {"ENV": "MONGO_URL"}
}

If I'm on the development environment, and if the process.env.MONGO_URL variable is not set, it will throw this error:

$ node node_modules/db-migrate/bin/db-migrate up -e dev --config "./database.json" -v
[INFO] Detected and using the projects local version of db-migrate. '/.../node_modules/db-migrate/index.js'
[ERROR] TypeError: Parameter "url" must be a string, not undefined
    at Url.parse (url.js:87:11)
    at Object.urlParse [as parse] (url.js:81:5)
    at module.exports (/.../node_modules/parse-database-url/lib/parse-database-url.js:16:23)
    at Object.exports.loadObject (/.../node_modules/db-migrate/lib/config.js:67:18)
    at Object.exports.loadFile (/.../node_modules/db-migrate/lib/config.js:59:18)
    at loadConfig (/.../node_modules/db-migrate/api.js:555:18)
    at Object.dbmigrate (/.../node_modules/db-migrate/api.js:67:17)
    at Object.module.exports.getInstance (/.../node_modules/db-migrate/index.js:56:10)
    at /.../node_modules/db-migrate/bin/db-migrate:34:23
    at /.../node_modules/resolve/lib/async.js:44:21
    at ondir (/.../node_modules/resolve/lib/async.js:187:31)
    at onex (/.../node_modules/resolve/lib/async.js:93:22)
    at /.../node_modules/resolve/lib/async.js:24:18
    at FSReqWrap.oncomplete (fs.js:123:15)

Apparently, db-migrate iterates over everything (even the non-current environment) in the database.json file, and if there is something in the form of {"ENV": "XXXX"}, it will try to parse process.env.XXXX using parse-database-url.
Since process.env.XXXX is undefined in some cases (potentially all the other environments that are not using {"ENV": "XXXX"} in the config) and parse-database-url doesn't accept undefined, it throws this error.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions