Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Add "script/dbconsole"

#102

There's no handy database analog of "script/console"; this patch addresses this by adding Rake tasks for quickly connecting to each defined database with its standard console tool, e.g. psql, mysql etc.

This exists as a separate plugin (see http://www.sanityinc.com/article...), but a number of people have suggested I submit it for inclusion in the core, since it's a tiny patch, and provides a feature useful to almost every Rails developer.

Examples:

% rake db:console # Connect to your RAILS_ENV database

% rake db:console:production # Connect to the production database

% rake db:console:test # Connect to the test database

% rake db:console:some_other_db # Connect to some_other_db defined in database.yml

Reported by Steve Purcell · May 3rd, 2008 @ 08:33 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. alpinegizmo
    alpinegizmo

    This is one of those great little improvements that makes sense instantly, and you use several times a day. It's especially sweet when your project has lots of environments.

    May 3rd, 2008 @ 08:52 PM

  2. Gravis
  3. Ben Nolan
    Ben Nolan

    I use this regularly. If it's a small patch I'd love having it in core.

    May 3rd, 2008 @ 11:07 PM

  4. Jeremy Kemper
    Jeremy Kemper

    I'd use a script instead of a rake task. Here's the script/mysql I add to most apps:

    #!/usr/bin/env ruby
    require 'yaml'
    
    configs = YAML.load_file("#{File.dirname(__FILE__)}/../config/database.yml")
    rails_env = ARGV.shift || ENV['RAILS_ENV'] || 'development'
    
    unless configs.include?(rails_env)
      abort "Missing database.yml config for #{rails_env.inspect} environment."
    end
    
    db = configs[rails_env]
    command = "mysql #{db['database']}"
    
    { 'host'      => '--host',
      'port'      => '--port',
      'socket'    => '--socket',
      'username'  => '--user',
      'password'  => '--password'
    }.each do |key, opt|
      if value = db[key]
        command << " #{opt}=#{value}"
      end
    end
    
    puts "[#{rails_env}] #{command}"
    exec command
    

    May 4th, 2008 @ 12:46 AM

  5. Steve Purcell
    Steve Purcell

    Fair enough. I like the convenience of being able to execute the command from any working directory in my rails project, but nonetheless, here's a patch that adds the functionality as a script instead, using some cues from your script above.

    May 4th, 2008 @ 11:56 AM

  6. Stefan Kaes
    Stefan Kaes

    +1. I use this all the time.

    May 5th, 2008 @ 07:45 AM

  7. Steve Purcell
    Steve Purcell
    • Title changed from Add db:console tasks to [PATCH] Add "script/dbconsole"

    May 7th, 2008 @ 03:38 PM

  8. Jeremy Kemper
    Jeremy Kemper

    Is find_cmd needed? AFAIK Windows doesn't require the .exe suffix and PATH resolution is handled by the shell already.

    May 7th, 2008 @ 08:21 PM

  9. Steve Purcell
    Steve Purcell

    Yeah, good question - I think it's needed. Note that it's used both for choosing a preferred binary ("mysql5" vs "mysql") and for handling the .exe thing.

    The former case affected me personally (with either MacPorts or Debian; I forget which). The latter (.exe) case surprised me - Stefan Kaes provided a patch for an issue he'd seen. I think he uses Cygwin.

    Probably safer to leave find_cmd as-is, but by all means go with what makes the most sense to you.

    May 7th, 2008 @ 08:28 PM

  10. Stefan Kaes
    Stefan Kaes

    without the .exe magic the correct executable will sometimes not be found on windows. the problem is that some installations add a program.bat file alongside program.exe. invoking the .bat however, will not work.

    May 7th, 2008 @ 09:45 PM

  11. Repository
    Repository
    • State changed from new to resolved

    (from [2561732a08ae97fa44706a8eca4db147c4a7c286]) Some dbconsole tweaks. [#102 state:resolved]

    http://github.com/rails/rails/co...

    May 7th, 2008 @ 09:54 PM

  12. Steve Purcell
    Steve Purcell

    Awesome - thanks Jeremy.

    May 7th, 2008 @ 10:09 PM