1

I'm trying to run db-migrate for postgres on heroku but cannot seem to figure out why it doesn't have the right env variables. Hoping for some guidance if someone else has encountered a similar problem.

I created a next.js app. Locally, I have a .env file with DATABASE_URL, etc. The app and migrations work just fine.

I pushed my app to heroku. The env variables are all defined in the heroku app config vars, so the app is working as expected. However, when I try to run db-migrate up or down, I get "Could not find database config file '/app/database.json'". I looked at the source and this only happens when process.env.DATABASE_URL is not defined. However, it is in my heroku app config vars, so I am confused as to why the migration cannot find the database URL. I thought the config vars would be pushed into node's process.env.

I am working around this by creating a new .env file in heroku every time I deploy but would prefer to fix this properly.

davidatthepark
  • 1,235
  • 1
  • 13
  • 25
  • 1
    just checking, when you run `db-migrate` is it run by calling `heroku run db-migrate up/down --app app-name-here`? if you run `heroku run printenv --app app-name-here` does it look like the env is set correctly? – jmunsch Jul 31 '20 at 22:15
  • What I usually do is `heroku ps:exec` so I can attach to my app. Then I run an npm script `npm run db-migrate up` which just uses the `db-migrate` module. I ran `heroku run printenv --app appname` and I am actually not seeing my config variables I set in Heroku. The thing is, `pg` (node-postgres) seems to have no problem connecting to my database and I can see the config vars when I log `process.env` in `pool.connect().then...`. I think something is going over my head. – davidatthepark Jul 31 '20 at 22:22
  • 1
    yeah i'm not sure exactly what is going on but if you run `heroku config:edit --app appname` you can add/edit the config vars as well that way. might see that the config vars aren't set as expected? might be something wonky with DATABASE_URL, if the app still has a default database connected the DATABASE_URL will default to the whatever app db it was created with. I've had issues where i had to explicitly teardown the old db, or create a different env var `NEW_PROD_DATABASE_URL` and use this other env var instead of the special DATABASE_URL provided by heroku. – jmunsch Jul 31 '20 at 22:38
  • 1
    another thing to look into might be the order in which stuff get's created, if the app is being created fresh every time, it might boot up a db with the app if DATABASE_URL is empty, if so when it gets time to running `heroku run db-migrate up/down` it might be the case that the env gets overwrittern, especially if it's part of a heroku pipeline where you have the pipeline config vars set to template out to each app that gets created. just some stuff to look into. hope it helps. – jmunsch Jul 31 '20 at 22:47
  • thanks a lot! will try out those suggestions – davidatthepark Aug 01 '20 at 00:20

0 Answers0