views:

392

answers:

5

Hi, I'm trying to run just one migration out of a whole bunch in my rails app. How can I do this? I don't want to run any of the migrations before or after it. Thanks.

A: 
rake db:migrate VERSION=20098252345

give that a try.

Joseph Silvashy
I think this will run any migrations up to the one you specified.
Ken Liu
close, but that also runs any migrations before the specific migration.
William
I don't think you should/want to run only one migration without considering the ones before it. A migration is a representation of the databases structure as it relates to the code at a given point in time , and thus the migrations before it are necessary. If you only want to run _one_ migration it's likely you didn't write the proper up/down operations to keep the migrations functional... it's a bad habit to only write your up migrations.
Joseph Silvashy
A: 

There's got to be a way to run the migration class via the console. I can't seem to get the migrations code to be recognizable.

However, as the comments indicate, it's preferred to run the migrations in order. Use:

rake db:migrate VERSION=##########

Copy and paste your code in the migration to script/console?

Terry Lorber
+1  A: 

rake db:migrate:redo VERSION=xxxxxxx, but that will run the down and then the up step. You could do this in conjunction with commenting out the down step temporarily.

Ryan Bigg
Hmm, http://blog.stonean.com/2007/12/18/rake-dbmigrateredo/, ::redo doesn't seem to take a VERSION argument.
Terry Lorber
It works, I tested it.Note that the article is from TWO THOUSAND AND SEVEN. Rails changes a lot during that time.
Ryan Bigg
A: 
rake db:migrate:up VERSION=1234567890

similarly rake db:migrate:down to take a specific migration down. You can get a list of available rake tasks with rake -T.

Shadwell
+1  A: 

I've had to run a single migration that changed and needed to be re-run independently of all other migrations. Fire up the console and do this:

>> require 'db/migrate/your_migrations.rb'
=> ["YourMigrations"]
>> YourMigrations.up
=> etc... as the migration runs
>> YourMigration.down

More usefully this could be put into a rake task etc.

korch