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

To think I also created my own rake tasks for these :)
RAILS_ENV=production rake db:drop ... oops
Why the hackiness for the Postgres create?
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!@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
_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
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—traceAny ideas?
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!
V && Chamstar. Are you sure you are on Edge? Which changeset? What did you setup in your database.yml file?
-Matt
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.