Rake only one migration

I am trying to start only one migration from a whole group in my rails application. How can i do this? I do not want to run any migrations before or after it. Thank.

+70
database ruby-on-rails migration rake
Aug 25 '09 at 22:25
source share
11 answers

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

+137
Aug 26 '09 at 3:04
source share
 rake db:migrate:up VERSION=1234567890 

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

+55
Aug 26 '09 at 9:03
source share

I had to complete one migration, which has changed, and it needs to be re-run independently of all other migrations. Launch the console and do the following:

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

More useful, this could be included in the rake task, etc.

+23
Sep 13 '09 at 8:37
source share
 rake db:migrate VERSION=20098252345 

give a try.

+7
Aug 25 '09 at 22:30
source share

rake db:migrate:up VERSION=version_no

Migrate (add) a specific migration script

rake db:migrate:down VERSION=version_no

Remove specific migration script

+5
Jun 02 '13 at
source share

Turning around the answer above, require didn't work for me, but load did. To be specific, for a migration file:

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

in input console

  > 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 (version 2013-11-22 version 43786) [x86_64-linux] and Rails 3.2.13.

+3
May 20 '14 at 13:31
source share
 rake db:migrate:redo version='xxxx' 

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

You can check the timestamps (migration ID) for previous migrations you made using

 rake db:migrate:status 
+2
Nov 24 '14 at 3:08
source share

I have a useful method that makes this very easy to develop. I find that this helps me to avoid creating too many migrations - usually I change the migration until they are deployed.

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

+1
Jun 15 2018-11-11T00:
source share

Adding my 2 ยข to this because I ran into the same problem:

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

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

And the rails will โ€œforgetโ€ that he made the transition to 20150105181157. Now that you run db: migrate, it will start it again.

This is almost always a bad idea. One instance where this might make sense is if you have a development branch and you have not yet defined your migration and want to add some things to it during the development process. But even then, you better make your transition to 2 paths so that you can roll back and try again.

+1
Jan 08 '15 at 18:30
source share

There should be a way to run the migration class through the console. I can't seem to recognize the migration code.

However, as the comments indicate, he prefers to migrate in order. Using:

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

Copy and paste your code when switching to script / console?

0
Aug 26 '09 at 2:54
source share

I use this technique in development when I change the migration to a significant amount, and I do not want to transfer a ton and lose any data along the way (especially when I import outdated data, a long time that I do not want to re-import).

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

  • Move the migration you want to restart from your directory to a temporary location
  • Generate another migration with the same name
  • Copy / paste the migration source code into the newly created migration file
  • Start a new migration
  • Delete newly created migration file
  • Edit the schema migration to remove the last value
  • Restore old migration file
0
Oct 06 '16 at 20:26
source share



All Articles