Rails 4.2 database.yml does not read rbenv-vars variables

My VPS has rbenv-vars installed , I found the .rbenv-vars file in the config directory inside the rails application. I pretend to load the database password inside it, but I'm doing something wrong, because rake db:create gives me an error without a password. A rake works when I write a 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 run all the rbenv vars variables

ssh> rbenv vars

 export DB_PASS='my_db_password' 
+5
source share
3 answers

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

You may need

 spring stop 

then try again. It worked for me.

+6
source

I have the same problem. I know my variables are set 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 because rbenv-vars hasn't loaded the variables into my session yet?

Interestingly, when I put explicit values ​​in config/database.yml , it does not initialize tiny_tds when loading the rails server, but when 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 after loading rails, but not during loading?

+1
source

I had a similar problem with setting SECRET_KEY_BASE in secrets.yml from an environment variable and was focused on a solution by rbenv developer . Here is the main explanation:

Typically, database.yml should have access to environment variables. But, for the sake of pure communication, do not forget that database.yml does not evaluate itself; rather, the process that Rails starts loads database.yml and evaluates the variables inside it. So my question for you is who / what starts this process and how?

How do you launch a Rails application and determine that database.yml broken? Are you using rails server or bundle exec or unicorn , or something else? For the command you are using, can you run which -a <command> and tell me the result?

In my case, I used Apache and Passenger, so this could be solved by adding the following to the Apache configuration for my application (e.g. /etc/apache2/sites-available/my_app.conf ):

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

0
source

All Articles