47

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
dB'
  • 7,838
  • 15
  • 58
  • 101
  • 2
    Perhaps give us the output of `rake db:migrate:status`. – Peter Jul 04 '12 at 00:00
  • ok, done. NO FILE looks kinda ominous... is that part of the problem? – dB' Jul 04 '12 at 00:09
  • 3
    Yes. `db:rollback` runs the down method in the latest applied migration, which in this case seems to be one where the file is missing. – Mark Thomas Jul 04 '12 at 00:12
  • So... am I understanding correctly that the rollback is failing because rake can't find a particular migration file whose name begins with `20120702151447`? And that the solution to my problem is to find this file and drop it into `/db/migrate/`? – dB' Jul 04 '12 at 00:51
  • Why `rake db:rollback` output an error in this case? Very frustrating! – Jason Axelson Apr 27 '16 at 01:58

4 Answers4

74

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.

Peter
  • 127,331
  • 53
  • 180
  • 211
  • 13
    Thanks, this worked. Strangely, though, I was never able to find a file with the migration ID that rails was looking for (20120702151447). In the end I created a "dummy" file with name `20120702151447_create_nothing.rb`, and body `class CreateNothing < ActiveRecord::Migration def change end end`. It's not an elegant solution, but it got the job done. – dB' Jul 04 '12 at 01:06
  • 12
    I had some missing migration files due to removing a gem manually. Rather than creating the dummy file, I deleted the records from the `schema_migrations` table, like: `delete from schema_migrations where version in ('20150424220749', '20150424220748');` That fixed it for me. – Derek Apr 27 '15 at 16:13
  • @Derek that should be an answer – Jonathan Allard Sep 16 '16 at 00:10
  • Just a note that you may encounter this state if you've migrated your database on a git branch other than your current working branch. – dzuc Jul 10 '18 at 21:05
11

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.
AmrataB
  • 1,066
  • 2
  • 10
  • 19
  • After getting this error. Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for I added this to the dummy file: `class CreateNothing < ActiveRecord::Migration[6.0] def change end end` – Ashley Mar 13 '21 at 07:18
6

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

krozaine
  • 1,481
  • 15
  • 22
1

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.

Shiva
  • 11,485
  • 2
  • 67
  • 84
mau
  • 258
  • 3
  • 10
  • If you have users & authentication, then make sure you take provisions not to lock yourself out. For instance, add your user in db/seed.rb. – iconoclast Aug 07 '13 at 23:17
  • 2
    Fwiw, when I had this issue it was because I had run a migration and then, needing to insert a quick bug fix before continuing work on the migration-related feature, `git stash`-ed my local changes without first doing a rollback. With that in mind, the best approach might be to first unstash, rollback, and then stash again. – clozach Nov 20 '14 at 00:39
  • 14
    !! Remember that rake db:reset will drop your current database and you will lose all your data. Be sure that's what you want to do. – user1515295 Sep 03 '15 at 02:14
  • 3
    The answer is very dangerous – dtelaroli Jan 04 '17 at 18:55
  • According to this, you'd have to run `rake db:migrate:reset`, to actually run all the migrations and not just load the schema again: https://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaload – Magne Jun 07 '17 at 13:26