2

I need to rollback migration on my ruby on rails project

I accidently deleted my migration file, [ID:20180214033313].

my migration status

migration output

I tried doing bundle exec rake:db rollback but it didn't help and I also tried bundle exec rake db:migrate:down VERSION=20180214033313 but it gave me the error below,

ActiveRecord::UnknownMigrationVersionError: 
No migration with version number 20180214033313> 

Any suggestions on how can I fix this?

hhh_
  • 310
  • 2
  • 5
  • 17
Menphis
  • 35
  • 2
  • 5
  • What do you mean "how can I do this?" You want to roll back to a migration that you deleted. How is rake supposed to find the file you specified when you explicitly nuked it? Are you actually trying to roll back to the version *before* your deleted migration? – MarsAtomic Feb 14 '18 at 04:53
  • Does this answer your question? [rake db:rollback not working?](https://stackoverflow.com/questions/11320756/rake-dbrollback-not-working) – Rajkaran Mishra Aug 06 '20 at 15:26

5 Answers5

2

Aaah, today I was confronted with this same problem following the deletion of a migration. which is probably your case...

To reinstall existing migrations, do a migration reset:

rails db:migrate:reset

And to have a good knowledge about migrations, my little 12 points guide:

  • db:migrate runs (single) migrations that have not run yet.

  • db:create creates the database

  • db:drop deletes the database

  • db:schema:load creates tables and columns within the existing database following schema.rb. This will delete existing data.

  • db:setup does db:create, db:schema:load, db:seed

  • db:reset does db:drop, db:setup

  • db:migrate:reset does db:drop, db:create, db:migrate

  • db:migrate:redo runs (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration

  • db:schema:dump dumps the current env's schema (and seems to create the db as well)

  • db:forward advances the current schema version to the next one

  • db:seed (only) runs the db/seed.rb file

  • db:migrate:down rolls back one specific migration

1
  1. If you are using git then, You can use git to recover that file, In git status (or git gui) where you will find the list of changed files, There you will see

    deleted: db/seeds.rb

    something like this for deleted files. Just use,

    git checkout filename

    And it will restore file.

  2. If you are not using git then, use rake db:migrate:status and check the timestamp and create a file with exact same timestamp of deleted file and the name which you had given earlier. And add the same code like before. And do migrate.

Hope this will help

Ruturaj Bisure
  • 151
  • 1
  • 13
  • I like the idea with naming the file with the missing timestamp. This can be used when we want to keep the database with all the data, but still fix the issue. – user2992971 Feb 14 '23 at 16:44
0

Better Solutions for your situation.Assuming you have git initialised with your application,

If you deleted your migration in current commit

Execute git stash so that you can terminate/delete the changes you made. Now You can rollback the migrations.

If you deleted your migration in previous commit

You can drop your database by using the command DROP DATABASE databasename and you can make fresh migration.

Thananjaya S
  • 1,451
  • 4
  • 18
  • 31
0

If you're using the github app for desktop, you can simply right click and discard the changes you've made.

I've done the same thing before it's a real simple rollback

user3349825
  • 67
  • 1
  • 11
0

You can do a rails db:reset, if you deleted accidentally your files with rails delete model MyModel.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77