question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mongodb not using env variable for DATABASE_URL

See original GitHub issue

I’m submitting a…

  • Bug report
  • Feature request
  • Question

Current behavior

We’re running mongodb and have a database json that looks like this: "defaultEnv": "dev", "dev": { "driver": "mongodb", "DATABASE_URL": {"ENV": "MONGO_URL"} }, It doesn’t seem to matter what ENV variable we set, we always get Error: database must be defined in database.json. If we manually set DATABASE_URL in our bash env, it works fine, but it doesn’t transform using MONGO_URL or really any env variable we set up. It seems like it’s just ignoring the environment transformation entirely.

Here’s the full error:

[ERROR] Error: database must be defined in database.json
    at Object.exports.connect (/Users/Janiuk/repos/graphql-server/node_modules/db-migrate-mongodb/index.js:496:11)
    at connect (/Users/Janiuk/repos/graphql-server/node_modules/db-migrate/lib/driver/index.js:93:12)
    at Object.exports.connect (/Users/Janiuk/repos/graphql-server/node_modules/db-migrate/lib/driver/index.js:134:5)
    at Object.exports.connect (/Users/Janiuk/repos/graphql-server/node_modules/db-migrate/connect.js:15:10)
    at /Users/Janiuk/repos/graphql-server/node_modules/db-migrate/lib/commands/up.js:15:13
    at tryCatcher (/Users/Janiuk/repos/graphql-server/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/Janiuk/repos/graphql-server/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/Janiuk/repos/graphql-server/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromiseCtx (/Users/Janiuk/repos/graphql-server/node_modules/bluebird/js/release/promise.js:606:10)
    at Async._drainQueue (/Users/Janiuk/repos/graphql-server/node_modules/bluebird/js/release/async.js:138:12)
    at Async._drainQueues (/Users/Janiuk/repos/graphql-server/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/Janiuk/repos/graphql-server/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)

Expected behavior

DATABASE_URL should be properly populated using the ENV setting.

Minimal reproduction of the problem with instructions

Create a project with mongodb and db-migrate. Create a database.json looking something a-like-so:

{
  "defaultEnv": "dev",
  "dev": {
    "driver": "mongodb",
    "DATABASE_URL": {"ENV": "MONGO_URL"}
  },
  "staging": {
    "driver": "mongodb",
    "DATABASE_URL": {"ENV": "STAGING_DATABASE_URL"}
  },
  "uat": {
    "driver": "mongodb",
    "DATABASE_URL": {"ENV": "UAT_DATABASE_URL"}
  },
  "demo": {
    "driver": "mongodb",
    "DATABASE_URL": {"ENV": "DEMO_DATABASE_URL"}
  },
  "prod": {
    "driver": "mongodb",
    "DATABASE_URL": {"ENV": "PROD_DATABASE_URL"}
  }
}

Run migrations using npx db-migrate up. Watch the error listed above occur.

What is the motivation / use case for changing the behavior?

We’d love to see A: the environment variable properly populated when specifying it above and B: Not have to duplicate our environment variables with both MONGO_URL and DATABASE_URL with the same information.

Environment


db-migrate version: 0.11.6
plugins with versions:
db-migrate-mongodb: 1.5.0
db-migrate driver with versions: mongodb 3.4.6

Additional information:
- Node version: 10.15.1
- Platform:  Mac

Others:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
janiukjfcommented, Jul 9, 2019

OK. So, I experimented this morning, and I got it working properly. I’m certain it was a mix of both the engineer that implemented this initially reading the docs wrong, and then afterwards…me, also reading the docs wrong after viewing what we had and getting confused. There’s a section in which user and password are passed in via env variables, and I think that’s where the confusion came from. This is the block from the docs:

{
  "prod": {
    "driver": "mysql",
    "user": {"ENV": "PRODUCTION_USERNAME"},
    "password": {"ENV": "PRODUCTION_PASSWORD"}
  },
}

After our conversation last week, this morning I replaced our database.json config removing the “DATABASE_URL” value that’s in the original post above and used basically exactly what you suggested a few days ago that I initially misread as the same thing we had. Once I did that and removed the environment variable of DATABASE_URL, it worked exactly as it’s supposed to. So this was a case of poorly reading the docs and then myself poorly following up on it.

Thanks for your patience working through this user error issue. It’s much appreciated.

0reactions
stale[bot]commented, Aug 8, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB Environment Variables Error, Unsure How to Proceed
First - make sure that your .env file is in the root directory of your project. Second - require dotenv as earliest as...
Read more >
Connection String URI Format — MongoDB Manual
Refer to the Drivers documentation. This connection string option is not available for the mongo shell. Use the command-line option instead. New in...
Read more >
MongoDB CA_CERT environment variable not being evaluated
I have an app spec that defines a database and a service, and the service defines two environment variables, DATABASE_URL and CA_CERT, ...
Read more >
Environment variables - Prisma
Learn how to use environment variables in your Prisma project. ... For example, if you specify a DATABASE_URL variable in two different .env...
Read more >
Variables | Railway Docs
Each database service provides the necessary environment variables needed to use it (such as DATABASE_URL for the PG plugin). You can also specify...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found