What's New in Edge Rails: New Database Rake Tasks

Posted by ryan
at 7:04 PM on Tuesday, May 29, 2007

Not two days after I just implemented these tasks myself for a client project comes the addition of some useful rake tasks that handle the provisioning of your database.

Want to create your database without having to drop into that pesky mysql command prompt? Then I believe rake db:create is your man. Don’t need that database anymore? Give rake db:drop a try. Want to tear down your database and re-create it, migrations and all? rake db:reset is your jack-of-all-trades then.

And all the options you’re used to when raking the database still apply:

1
2
3
4
5
# Drop the test database
RAILS_ENV=test rake db:drop

# Recreate the staging database, but only migrate it to version 3
RAILS_ENV=staging rake db:reset VERSION=3

If you haven’t made rake a part of your life yet, it’s time you do so.

tags: ruby, rubyonrails

Comments

Leave a response

  1. aizattoMay 30, 2007 @ 01:19 AM

    To think I also created my own rake tasks for these :)

  2. RyanMay 30, 2007 @ 10:29 AM

    RAILS_ENV=production rake db:drop ... oops

  3. DavidMay 31, 2007 @ 01:02 PM

    Why the hackiness for the Postgres create?

  4. fozJune 01, 2007 @ 07:46 AM

    These new tasks look great… I just posted something on my blog related to migrations and making sure they all work.

    However, there’s a patch that showed up a couple of weeks ago that seems awesome: http://dev.rubyonrails.org/ticket/8389 - the idea is that you can set config.active_record.schema_format = :migrations in environment.rb - and your db will be created from migrations instead of schema.rb. Hope that gets committed!

  5. Matt AimonettiJune 05, 2007 @ 04:13 AM

    @david I’m not a postgresql expert, as a matter of fact I don’t even use it in production. When I worked on the rake tasks, it was the only way I found to create and drop tables. If you know of a better way, please submit a patch or let me know so I can patch my code.

    Anyway, I’m glad these tasks got added to Edge, but for your information, when using db:create your databases will be created in utf8 by default. I also extended the mysql adapter to support other charsets and collations hence the new rake db:collation and rake db:charset tasks used to retrieve your db collation and character set info.

    Matt

  6. LøµisxsJuly 02, 2007 @ 04:39 AM

    _Hi Everybody Im new in this World(rails) and i have a question !How can i make my DB so that i can translate to my svn.version. there are any commads to write in Eclipse.

    thansk Løµisxs

  7. ChamStarJuly 09, 2007 @ 06:20 AM

    I copied the databases.rake file in to my lib/tasks folder but when I try and run the task I get an error:

    [code] /code/tarmac$ RAILS_ENV=development rake db:create—trace
    • Invoke db:create (first_time)
    • Invoke environment (first_time)
    • Execute environment
    • Execute db:create rake aborted! wrong number of arguments (2 for 1) /code/tarmac/config/../lib/tasks/databases.rake:16:in `create_database’ /code/tarmac/config/../lib/tasks/databases.rake:16 /code/tarmac/config/../lib/tasks/databases.rake:5:in `each_value’ /code/tarmac/config/../lib/tasks/databases.rake:5 /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `call’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `each’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in `invoke’ /usr/lib/ruby/1.8/thread.rb:135:in `synchronize’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `each’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:in `top_level’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1711:in `run’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:in `run’ /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/bin/rake:7 /usr/bin/rake:16:in `load’ /usr/bin/rake:16 [/code]

    Any ideas?

  8. VJuly 10, 2007 @ 04:52 PM

    I got the same problem. I tried changing the following line in datbases.rake ActiveRecord::Base.connection.create_database(config[‘database’], {:charset => @charset, :collation => @collation}) to ActiveRecord::Base.connection.create_database(config[‘database’])

    That seemed to work!

  9. Matt AimonettiJuly 28, 2007 @ 09:35 PM

    V && Chamstar. Are you sure you are on Edge? Which changeset? What did you setup in your database.yml file?

    -Matt

  10. Matt AimonettiJuly 28, 2007 @ 09:38 PM

    For Rails 1.2.x I wrote a plugin (which was the base for the rails patch) to handle the db creation. I didn’t use it in a very long time but check it out: http://svn.aimonetti.net/public/plugins/db_tasks/ and you can even get the former plugin there: http://svn.integrumtech.com/public/plugins/rake_tasks/ http://svn.integrumtech.com/public/plugins/rake_tasks/tasks/database.rake will be compatible with Rails 1.2 and Sake.