No such file 'config/database.yml' on Heroku - ruby

My app is a Ruby rack app. When my Heroku app starts it breaks because
/app/config.ru:8:in `read': No such file or directory - config/database.yml (Errno::ENOENT)
Why does this happen? I understood Heroku is meant to create this file https://devcenter.heroku.com/articles/cedar-migration
The database credentials will still be configured automatically: at slug compile time, a config/database.yml that parses the DATABASE_URL from the environment will be written into the app.
Frustratingly the doc at https://devcenter.heroku.com/articles/ruby doesn't explain about database.yml

Ok, the first thing - heroku does not use database.yml file. By default rails app loading it from config/ directory automatically. And it's no need to load it manually in config.ru. If you want to use in heroku PosgreSQL - just add add-on. Heroku wil do all other things to link your app and db. If you want to use external MySQL server you should use Amazon RDS add-on
heroku addons:add amazon_rds url=mysql2://user:pass#dbhost/dbname
By this you can use any db. I use GoDaddy mysql bases through the Amazon RDS add-on.
Any way, the problem in your config.ru 8th line something like
read 'config/database.yml'
Delete it and look other ways that not conflicted with heroku
Good luck

It appears Heroku only creates its config/database.yml if you have a folder config under source control. Not explained in docs.

Related

Tell heroku which config file for Play application to load

In Play, you can use multiple config files (application.conf, prod.conf...). Usually you would have a default conf file, i.e. application.conf, and let the other files import it and overload specific values.
One case is for example when you have a production database and wand to overwrite access configuration values set by developers and use credentials only known to the production personnel.
Here is a manual on this topic that say that the wanted config is to be specified as a parameter when running the application
I am deploying my application onto Heroku, which takes care of running the application. The only peace missing here and I can't find is how to tell Heroku which config file to load?
I solved this by using a Procfile with the contents:
web: target/universal/stage/bin/my_app -Dhttp.port=$PORT -Dconfig.resource=my-special.conf
You can define environment variables for your Heroku app, e.g. using the heroku config CLI command:
heroku config:set PLAY_CONFIG_FILE=application.conf
See Heroku config vars.

How can I get Heroku's database information in a ruby script?

I created a rails app that is hosted on Heroku. Now I want to upload a ruby script that will populate my database.
This script already exists and is using
PGconn.connect( :hostaddr=>"127.0.0.1", :port=>5432, :dbname=>"myDB", :user=>"MyUser", :password=>'MyPass');
This will obviously not work on Heroku.
I tried to get it using:
Rails.configuration.database_configuration
But I get
uninitialized constant Rails
How can I get the information so I can connect to my Heroku DB? Is there any way to make the same code wotk both on my localhost and on Heroku?
(ps: I am using foreman and the .ENV file, if it helps)
Edit:
The solution I took:
require 'yaml'
require 'erb'
config = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)
In config I have all the information I need to perform the DB access.
Why would you not just access ENV['DATABASE_URL'] and break it up into the constituent parts? To see how this is done, if you do heroku run bash and do cat config\database.yml you will see how Heroku does this itself.
To ensure that locally it works as well if you execute via foreman run <Scriptname> then it will be executed and the contents of your .env will be loaded.

Will Heroku ignore the .env file?

Our team is using foreman for development and .env files to preassign development ports to each piece of a service oriented application. It dramatically simplifies things for this file to just live with the repository as we are not doing any specific per-machine local configurations even though multiple docs seem to think this is a bad idea.
Does anybody know if Heroku will ignore these .env files automatically? What if they were added to .slugignore?
I setup a test app to try this out including a PORT=5005 in the .env file and then committing/deploying to Heroku. Heroku didn't seem to notice it was even there and no new config vars appeared when I checked heroku config.
You answered your own question, but just for confirmation: .env is entirely a Foreman construct, while Foreman and Heroku will make use of Procfile.
We actually wanted to be able to ensure consistent environments between local and Heroku deployments, so I wrote a python script to export .env up to Heroku.
In case others want to export .env to Heroku:
https://github.com/FinalsClub/karmaworld/blob/68f0f0340d7b6420e263cab648ff7de1ea851a0e/export_env_to_heroku.py

Heroku is switching my play framework 2 config file

I have a Play! application which is on Heroku.
My config file is different between my local application and the same on Heroku. Especially for the URL of my MongoDB base.
On localhost my base address is 127.0.0.1 and on heroku it's on MongoHQ. So when I push my application to Heroku I modify my config file.
But some times, like this morning Heroku change the config file. I pushed my application correctly configured on Heroku this morning and everything worked until now.
When I watch the logs I see that Heroku changed my config and try to connect to my local MongoDB base.
Is someone knowing what ? I hope I'm clear :)
Thanks everybody !
If there are differences in your application in different environments (e.g. local vs production), you should be using assigning the values with environment variables. For Play apps, you can use environment variables in your application.conf file, like this:
`mongo.url=${MONGO_URL}`
Then, on Heroku you can set the environment variables with config vars, like this (note, this may already be assigned for you by the add-on provider):
$ heroku config:add MONGO_URL=...
Locally, you can use Foreman to run your application with the environment variables stored in an .env file in your project root.

heroku rails "load error expected app/app/model" instead just "app/model"

I have a Rails 2 application running well on my local machine at the moment. But fails on Heroku, here's the log: http://pastie.org/1957572
I can't figure out why the path is "app/app/model/.." instead of just "app/model...", is there anything I need to config my Rails app for Heroku?
Thanks!
You probably figured this out, but it's because /app is the root folder for apps on Heroku
It's like if you had it on your own VPS and it was in /var/www, then the error would read like "/var/www/app..."

Resources