Database-Related Rake Tasks

The following rake tasks are included by default in boilerplate Rails projects.

db:create and db:create:all
Create the database defined in config/database.yml for the current Rails.env (Or create
all of the local databases defined in config/database.yml in the case of db:create:all.)
db:drop and db:drop:all
Drops the database for the current RAILS_ENV. (Or drops all of the local databases
defined in config/database.yml in the case of db:drop:all.)
db:forward and db:rollback
The db:rollback task moves your database schema back one version. Similarly, the
db:forward task moves your database schema forward one version and is typically used
after rolling back.
db:migrate
Applies all pending migrations. If a VERSION environment variable is provided, then
db:migrate will apply pending migrations through the migration specified, but no
further. The VERSION is specified as the timestamp portion of the migration file name.

If the VERSION provided is older than the current version of the schema, then this task
will actually rollback the newer migrations.

db:migrate:down
Invoked without a VERSION, this task will migrate all the way down the version list to
an empty database, assuming that all your migrations are working correctly.
With a VERSION, this task will invoke the down method of the specified migration
only. The VERSION is specified as the timestamp portion of the migration file name.

$ rake db:migrate:down VERSION=20100124181315
== AddClientIdToUser: reverting =========================================
— remove_column(:users, :client_id)
-> 0.0367s
== AddClientIdToUser: reverted (0.0370s) ================================

db:migrate:up
Invoked without a VERSION, this task will migrate up the version list, behaving the same
as db:migrate.
With a VERSION, this task will invoke the up method of the specified migration
only. The VERSION is specified as the timestamp portion of the migration file name.

$ rake db:migrate:up VERSION=20100124181315
== AddClientIdToUser: migrating =========================================
— add_column(:users, :client_id, :integer)
-> 0.0383s
== AddClientIdToUser: migrated (0.0385s) ================================

db:migrate:redo
Executes the down method of the latest migration file, immediately followed by its up
method. This task is typically used right after correcting a mistake in the up method or
to test that a migration is working correctly.

$ rake db:migrate:redo
== AddTimesheetsUpdatedAtToUsers: reverting =============================
— remove_column(:users, :timesheets_updated_at)
-> 0.0853s
== AddTimesheetsUpdatedAtToUsers: reverted (0.0861s) ====================
== AddTimesheetsUpdatedAtToUsers: migrating =============================
— add_column(:users, :timesheets_updated_at, :datetime)
-> 0.3577s
== AddTimesheetsUpdatedAtToUsers: migrated (0.3579s) ====================

 

 

db:migrate:reset
Resets your database for the current environment using your migrations (as opposed to
using schema.rb).
db:reset and db:setup
The db:setup creates the database for the current environment, loads the schema from
db/schema.rb, then loads the seed data. It’s used when you’re setting up an existing
project for the first time on a development workstation. The similar db:reset task does
the same thing except that it drops and recreates the database first.
db:schema:dump
Create a db/schema.rb file that can be portably used against any DB supported by
Active Record. Note that creation (or updating) of schema.rb happens automatically
any time you migrate.
db:schema:load
Loads schema.rb file into the database for the current environment.
db:seed
Load the seed data from db/seeds.rb as described in this chapter’s section Database
Seeding.
db:structure:dump
Dump the database structure to a SQL file containing raw DDL code in a format
corresponding to the database driver specified in database.yml for your current envi-
ronment.

$ rake db:structure:dump
$ cat db/development_structure.sql
CREATE TABLE ‘avatars’ (
‘id’ int(11) NOT NULL AUTO_INCREMENT,
‘user_id’ int(11) DEFAULT NULL,
‘url’ varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (‘id’)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

 


I’ve rarely needed to use this task. It’s possible that some Rails teams working in con-
junction with DBAs that exercise strict control over their application’s database schemas
will need this task on a regular basis.

 

db:test:prepare
Check for pending migrations and load the test schema by doing a db:schema:dump
followed by a db:schema:load.
This task gets used very often during active development whenever you’re run-
ning specs or tests without using Rake. (Standard spec-related Rake tasks run
db:test:prepare automatically for you.)
db:version
Returns the timestamp of the latest migration file that has been run. Works even if
your database has been created from db/schema.rb, since it contains the latest version
timestamp in it:

ActiveRecord::Schema.define(:version => 20100122011531)

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s