Rake just one migration

Ruby on-RailsDatabaseMigrationRake

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

rake db:migrate:up VERSION=version_no

Will migrate( add) specific migration script

rake db:migrate:down VERSION=version_no

Will delete specific migration script

Solution 5 - Ruby on-Rails

rake db:migrate VERSION=20098252345

give that a try.

Solution 6 - Ruby on-Rails

rake db:migrate:redo version='xxxx'   

Remember to put the quotation mark around xxxx, xxxx is the timestamp (or Migration ID) for your migration.

You may check the timestamps (Migration ID) for the previous migrations you've done by using

rake db:migrate:status    

Solution 7 - Ruby on-Rails

Expanding on the answer by korch above, require did not work for me, but load did. To be concrete, for the migration file:

    class ChangeMinQuantityToRaces < ActiveRecord::Migration
      def change
        change_column :races, :min_quantity, :integer, :default => 0
      end
    end

in the console typing

    > load 'db/migrate/30130925110821_change_min_quantity_to_races.rb'
    > ChangeMinQuantityToRaces.new.change

worked for me.

    > Race.new.min_quantity # => 0 

This was for ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux] and Rails 3.2.13.

Solution 8 - Ruby on-Rails

Adding my 2¢ to this because I ran into this same issue:

If you absolutely want to run a migration over again without creating a new one, you can do the following:

rails dbconsole -p devdb=# delete from public.schema_migrations where version = '20150105181157';

And rails will "forget" that it ran the migration for 20150105181157. Now when you run db:migrate it will run it again.

This is almost always a bad idea though. The one instance where it could make sense is if you have a development branch and you haven't fleshed out your migration yet and want to add some things to it in development. But even then it's better to make your migration 2-way so you can properly rollback and retry repeatedly.

Solution 9 - Ruby on-Rails

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?

Solution 10 - Ruby on-Rails

I have a utility method that makes this very easy in development. I find that it helps me avoid creating too many migrations--normally I modify migrations until they have been deployed.

http://fullware.net/index.php/2011/05/26/easily-load-rails-migrations-for-console-execution/

Solution 11 - Ruby on-Rails

I use this technique in development when I change a migration a significant amount, and I don't want to migrate down a ton and lose any data in those along the way (especially when I'm importing legacy data that takes a long time that I don't want to have to re-import again).

This is 100% hackish and I would definitely not recommend doing this in production, but it will do the trick:

  1. Move migration that you want to re-run out of its directory to a temporary place
  2. Generate another migration with the same name
  3. Copy/paste the original migration code into the newly generated migration file
  4. Run the new migration
  5. Delete the newly generated migration file
  6. Edit your schema migrations to remove the most recent value
  7. Restore the old migration file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnonView Question on Stackoverflow
Solution 1 - Ruby on-RailsRyan BiggView Answer on Stackoverflow
Solution 2 - Ruby on-RailsShadwellView Answer on Stackoverflow
Solution 3 - Ruby on-Railsuser46519View Answer on Stackoverflow
Solution 4 - Ruby on-RailsreshmaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsJP SilvashyView Answer on Stackoverflow
Solution 6 - Ruby on-RailshexinpeterView Answer on Stackoverflow
Solution 7 - Ruby on-RailsbhfailorView Answer on Stackoverflow
Solution 8 - Ruby on-RailsKen SimonView Answer on Stackoverflow
Solution 9 - Ruby on-RailsTerry G LorberView Answer on Stackoverflow
Solution 10 - Ruby on-RailsaceofspadesView Answer on Stackoverflow
Solution 11 - Ruby on-RailsGreg BlassView Answer on Stackoverflow