Ricardo Castañeda
Ricardo Castañeda

Reputation: 5812

Rails 4.2 database.yml not reading rbenv-vars variables

My VPS has rbenv-vars installed, I've located .rbenv-vars file within config directory inside rails app. I pretend to load database password inside of it, but I'm doing something wrong, because rake db:create gives me a no-password error. Rake works when I write the password as a string.

config/.rbenv-vars

DB_PASS=my_db_password

config/database.yml

  password: $DB_PASS                # Doesn't work
  #password: <%= ENV['DB_PASS'] %>  # Doesn't work
  # password: my_db_password        # Works


I get all variables running rbenv vars

ssh > rbenv vars

export DB_PASS='my_db_password'

Upvotes: 8

Views: 2319

Answers (3)

Dan
Dan

Reputation: 915

I had a similar problem setting SECRET_KEY_BASE in secrets.yml from an environment variable and was guided to a solution by an rbenv developer. Here's the basic explanation:

Generally, database.yml should have access to environment variables. But, for the sake of clear communication, let's remember that database.yml doesn't evaluate itself; rather, the process that starts Rails loads database.yml and evaluates variables within it. So, my question for you is, who/what starts that process and how?

How are you starting your Rails app and determining that database.yml is broken? Are you using rails server, or bundle exec, or unicorn, or something else? For the command that you're using, can you run which -a <command> and tell me the result?

In my case I was using Apache and Passenger, so this could be resolved by adding the following to the Apache config for my application (i.e /etc/apache2/sites-available/my_app.conf):

PassengerRuby /home/my_username/.rbenv/shims/ruby

Upvotes: 0

Jay
Jay

Reputation: 156

via https://github.com/rails/rails/issues/19256#issuecomment-102980786

You may need to do a

spring stop

and then try it again. This worked for me.

Upvotes: 9

daveharris
daveharris

Reputation: 395

I have the same problem. I know my variables are setup correctly

$ rbenv vars
# /Users/dave/code/project/.rbenv-vars
export SQL_SERVER_HOST='10.0.0.1'
export SQL_SERVER_PORT='1433'
export SQL_SERVER_USERNAME='project'
export SQL_SERVER_PASSWORD='password'
export SQL_SERVER_DATABASE='project-development'

$ cat config/database.yml
development:
  adapter:  sqlserver
  host:     <%= ENV['SQL_SERVER_HOST'] %>
  port:     <%= ENV['SQL_SERVER_PORT'] %>
  username: <%= ENV['SQL_SERVER_USERNAME'] %>
  password: <%= ENV['SQL_SERVER_PASSWORD'] %>
  database: <%= ENV['SQL_SERVER_DATABASE'] %>
  mode:     dblib

Results in:

$ rails c
Inside TinyTds#initialize with {:dataserver=>nil, :host=>nil, :port=>nil, :username=>nil, :password=>nil, :database=>nil, :tds_version=>nil, :appname=>"project", :login_timeout=>nil, :timeout=>nil, :encoding=>nil, :azure=>nil}
/Users/dave/code/project/.gems/bundler/gems/tiny_tds-afef8218c3c0/lib/tiny_tds/client.rb:65:in `initialize': missing :host option if no :dataserver given (ArgumentError)
...

Maybe it's because rbenv-vars hasn't loaded the variables into my session yet?

Interestingly, when I put explicit values into the config/database.yml, it doesn't initialize tiny_tds on rails server boot, but on loading the first model:

$ rails c
Loading development environment (Rails 4.2.2)
[1] pry(main)> # No TinyTds#initialize!!!
[2] pry(main)> User.first
Inside TinyTds#initialize with {:dataserver=>nil, :host=>"10.0.0.1", :port=>1433, :username=>"project", :password=>"password", :database=>"project-development", :tds_version=>nil, :appname=>"project", :login_timeout=>nil, :timeout=>nil, :encoding=>nil, :azure=>nil}
  SQL (0.6ms)  USE [project-development]
  User Load (3.7ms)  EXEC sp_executesql N'SELECT  [users].* FROM [users]  ORDER BY [users].[id] ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY'
=> #<User:0x007fb8a0929fe0
 id: 1,
 ...

So rbenv-vars seems to work fine once rails has been booted, but not during the boot?

Upvotes: 1

Related Questions