SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Wednesday, January 21, 2009

Running migrations in rails 2.2 & rails 2.3(edge version)

Rails provides a set of rake tasks to work with migrations.And some of them are related to the database
The very first migration related rake task you use will probably be db:migrate.

Note that running the db:migrate also invokes the db:schema:dump task, which will update your db/schema.rb file to match the structure of your database.

For migrating a particular version,we have to specify the version,Active Record will run the required migrations (up or down) until it has reached the specified version. The version is the numerical prefix on the migration’s filename. For example to migrate to version 20080906120000 run
rake db:migrate VERSION=20080906120000

If the version number is greater than the current version (i.e. it is migrating upwards) this will run the up method on all migrations up to and including 20080906120000, if migrating downwards this will run the down method on all the migrations down to, but not including, 20080906120000.

Rolling back


If you made a mistake in migration and wish to correct it,then no need to go for tracking down the version number associated with the previous migration you just run
rake db:rollback

This will run the down method from the latest migration. If you need to undo several migrations you can provide a STEP parameter:
rake db:rollback STEP=3

will run the down method from the last 3 migrations.

The db:migrate:redo task is a shortcut for doing a rollback and then migrating back up again. As with the db:rollback task you can use the STEP parameter if you need to go more than one version back, for example
rake db:migrate:redo STEP=3


These tasks couldn't be achieved with db:migrate, they are simply more convenient since you do not need to explicitly specify the version to migrate to.

Lastly, the db:reset task will drop the database, recreate it and load the current schema into it.

Note This is not the same as running all the migrations - see the section on schema.rb.

Being Specific



If you need to run a specific migration up or down the db:migrate:up and db:migrate:down tasks will do that. Just specify the appropriate version and the corresponding migration will have its up or down method invoked, for example

rake db:migrate:up VERSION=20080906120000

will run the up method from the 20080906120000 migration. These tasks check whether the migration has already run, so for example db:migrate:up VERSION=20080906120000 will do nothing if Active Record believes that 20080906120000 has already been run.