helper function in database.yml not working from rake

190 views
Skip to first unread message

ruud

unread,
Nov 17, 2015, 5:20:02 AM11/17/15
to Ruby on Rails: Talk
hello group,

in order not to have passwords stored in my database.yml, I had inserted a function that read the password from a file that is not under version control.

The entry in database.yml looks like this:

e:
  adapter: postgresql
  host:     localhost
  template: template0
  database: archive_e
  encoding: utf8
  username: archive_e
  password: <%= read_pwd() %>
  pool:     5               # not mandatory
  timeout: 500


In config/environment.rb the function was defined:

# Load the rails application
require File.expand_path('../application', __FILE__)

def read_pwd ()
        File.open( File.join( File.dirname( __FILE__), '.pwd'), 'r') do |fh|
                fh.read
        end
end

# Initialize the rails application
Varch::Application.initialize!


This seemed to be a nice solution for quite some time, until I today had to rebuild my database. When I now try a db:setup or db:migrate, I get this error:

$ rake db:setup RAILS_ENV=e
/home/ruud/.rvm/gems/ruby-1.9.3-p551@rails32/gems/rspec-core-3.3.2/lib/rspec/core/shell_escape.rb:30: warning: already initialized constant SHELLS_ALLOWING_UNQUOTED_IDS
rake aborted!
NoMethodError: undefined method `read_pwd' for main:Object
(erb):8:in `<main>'
/home/ruud/.rvm/gems/ruby-1.9.3-p551@rails32/gems/railties-3.2.17/lib/rails/application/configuration.rb:115:in `database_configuration'
/home/ruud/.rvm/gems/ruby-1.9.3-p551@rails32/gems/activerecord-3.2.17/lib/active_record/railties/databases.rake:25:in `block (2 levels) in <top (required)>'
/home/ruud/.rvm/gems/ruby-1.9.3-p551@rails32/bin/ruby_executable_hooks:15:in `eval'
/home/ruud/.rvm/gems/ruby-1.9.3-p551@rails32/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:setup => db:schema:load_if_ruby => db:create => db:load_config
(See full trace by running task with --trace)
[1]    19621 exit 1     rake db:setup RAILS_ENV=e

It must be very simple to solve this, but I would greatfully accept any help; I am out of ideas at the moment. I don't know how to instruct rake to include the file in which read_pwd is defined.

thanks in advance, Ruud
           

BuyzLots

unread,
Nov 17, 2015, 10:29:39 AM11/17/15
to rubyonra...@googlegroups.com
Why not just use an environment variable.  For example, <%=  ENV[‘MY_DB_PASSWORD’] %>.   Then you can export that from your specific environment or fire up your task with the environment var set, ex: `MY_DB_PASSWORD=letsparty rails s`
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1e01fabb-1b47-415e-84c2-bc78442fddfb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rahoul Baruah

unread,
Nov 17, 2015, 2:51:29 PM11/17/15
to Ruby on Rails: Talk


On Tuesday, 17 November 2015 15:29:39 UTC, matt wrote:
Why not just use an environment variable.  For example, <%=  ENV[‘MY_DB_PASSWORD’] %>.   Then you can export that from your specific environment or fire up your task with the environment var set, ex: `MY_DB_PASSWORD=letsparty rails s`


I second that - support for environment variables is already built into the loader of database.yml 

ruud

unread,
Nov 18, 2015, 4:48:38 AM11/18/15
to Ruby on Rails: Talk


On Tuesday, November 17, 2015 at 4:29:39 PM UTC+1, matt wrote:
Why not just use an environment variable.  For example, <%=  ENV[‘MY_DB_PASSWORD’] %>.   Then you can export that from your specific environment or fire up your task with the environment var set, ex: `MY_DB_PASSWORD=letsparty rails s`


Hi Matt,
thanks for your advice. Maybe this is an alternative in this case, but I would like to know how to tackle this for custom functions in general.
I found out that if I copy the function to config/application, rake finds it. If it is the proper solution for yml-helper functions I don't know. Maybe someone has advice on this.

Ruud

Rahoul Baruah

unread,
Nov 18, 2015, 4:57:22 AM11/18/15
to Ruby on Rails: Talk


On Wednesday, 18 November 2015 09:48:38 UTC, ruud wrote:
 
Hi Matt,
thanks for your advice. Maybe this is an alternative in this case, but I would like to know how to tackle this for custom functions in general.
I found out that if I copy the function to config/application, rake finds it. If it is the proper solution for yml-helper functions I don't know. Maybe someone has advice on this.


Not exactly the same thing but I have a config/storage.yml file for holding my S3 configuration - and I load it in config/initializers/storage.rb - load the YAML as a text file, use ERB to parse it and then apply the file.  So any functions I had defined within config/initializers/storage.rb should be available to the ERB parser.

contents = File.open(File.join(Rails.root, 'config', 'storage.yml')) { | f | f.read } 
contents = ERB.new(contents).result
yaml = YAML.load(contents)

AqrReports::Application.config.image_options = yaml['images'][Rails.env]
AqrReports::Application.config.file_options = yaml['files'][Rails.env]
 

ruud

unread,
Nov 18, 2015, 10:56:53 AM11/18/15
to Ruby on Rails: Talk


On Wednesday, November 18, 2015 at 10:57:22 AM UTC+1, Rahoul Baruah wrote:

 So any functions I had defined within config/initializers/storage.rb should be available to the ERB parser.


Hi Rahoul,
I am going to try that. Thanks for your help.

regards, Ruud

Tamara Temple

unread,
Nov 18, 2015, 1:46:07 PM11/18/15
to rubyonra...@googlegroups.com
This is for Rahoul, not the OP:

Rails 4.1 introduced the #config_for method on the Rails application,
which makes all the reading, erbing, and yamling of config files really
super simple. You can change your above initializer to:

STORAGE_CONFIG = Rails.application.config_for(:storage)
AqrReports::Application.config.image_options = STORAGE_CONFIG['images']
AqrReports::Application.config.file_options = STORAGE_CONFIG['files']

From the looks of things, you may need to rearrange your storage.yml to
be in the same structure as database.yml.


--
Tamara Temple
tam...@gmail.com
http://www.tamouse.org

Rahoul Baruah

unread,
Nov 19, 2015, 6:49:31 AM11/19/15
to Ruby on Rails: Talk


On Wednesday, 18 November 2015 18:46:07 UTC, tamouse wrote:
 

This is for Rahoul, not the OP:

Rails 4.1 introduced the #config_for method on the Rails application,
which makes all the reading, erbing, and yamling of config files really
super simple. You can change your above initializer to:

Thanks Tamara - I must have completely missed that.  It looks awesome.

I guess the OP would still need to do the ERB stuff by hand to get custom functions in there though.

Cheers

Rahoul
Reply all
Reply to author
Forward
0 new messages