dB'
dB'

Reputation: 8330

rake db:rollback not working?

I'm writing my first Rails app. I've run a few rails generate model ... and rake db:migrate commands, but I now want to change my data model and so need to undo a few migrations.

The docs say I can undo a migration with rake db:rollback, but this isn't working. When I run this in the console the computer thinks for a few seconds but doesn't make any changes to db/migrate/ or db/migrate/schema.rb. No output is printed to the console.

Is this behavior correct? Shouldn't db:rollback be changing my schema? If so, can anyone think why it might not be working?

I'm on Rails v. 3.2.6.

EDIT

At the moment rake db:migrate:status gives

database: db/development.sqlite3

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20120617191211  Create irs
   up     20120701154357  Create paths
   up     20120701154421  Create nodes
   up     20120702151447  ********** NO FILE **********
  down    20120702155140  Create venues
  down    20120703233833  Remove path from venues

Upvotes: 48

Views: 20115

Answers (5)

The best solution for me was to find the problematic versions in the schema_migrations table in the database and delete them. Then run the rollback command again.

Upvotes: 0

AmrataB
AmrataB

Reputation: 1136

This is what worked for me. Combine the steps given in this answer and comment by dB.

  1. run rake db:migrate:status
  2. If you have a ****NO FILE**** entry, just note the version number as noFileVersion. Note the version of the entry just above no file entry(stable_version).
  3. created a "dummy" file with name noFileVersion_create_nothing.rb, and body
class CreateNothing < ActiveRecord::Migration[6.0] 
  def change 
  end 
end
  1. run rake db:migrate VERSION=stable_version
  2. remove the noFileVersion_create_nothing.rb manually.
  3. run rake db:migrate.
  4. run rake db:migrate:status again to check if No file entry has disappeared.

Upvotes: 13

mau
mau

Reputation: 258

NOTE: Use the suggestions with caution, it is very dangerous for non-dev environments.


If

rake db:migrate:status

gives you a migration that says

up 20120702151447 ********** NO FILE **********

Then the best thing to do would be to do a (note that the following command will drop the database):

rake db:reset

to redo all migrations. If the last migration is the one missing, then schema.rb will have the last migration that rake db:migrate will look for:

ActiveRecord::Schema.define(:version => 20120702151447) do

Change that number to the last one in your migrate folder.

Upvotes: 1

krozaine
krozaine

Reputation: 1531

The following worked for me: rake db:migrate:down VERSION=20190304092208

Version number can be obtained by the following command: rake db:migrate:status

This was the last version number to rollback one last migration

Upvotes: 6

Peter
Peter

Reputation: 132157

Solution (see my comment): run

rake db:migrate:status

and correct problems you find there. In this case (per @MarkThomas' followup), you might want to check all files you need are in place.

Upvotes: 75

Related Questions