Friday, April 29, 2011

Merb & DataMapper - accessing database connection info?

I'm using Merb and DataMapper with a MySQL db. I want to access the database name, user, and password from a Rake task for my Merb app. I guess I could YAML.load() the the database.yml, but that seems ugly. Any ideas?

From stackoverflow
  • desc "outputs database connection parameters"
    task :db_conn => :merb_env do |t|
      puts "Username: #{DataMapper.repository.adapter.uri.user}"
      puts "Password: #{DataMapper.repository.adapter.uri.password}"
      puts "Database: #{DataMapper.repository.adapter.uri.path.split('/').last}"
    end
    

    The interesting part there is the => :merb_env bit. That ensures that the "merb_env" task has executed before your task does. This simply loads up the Merb environment, at which point you can proceed to inspect its configuration.

0 comments:

Post a Comment