I messed up while generating my migration. How can I undo it?

Posted over 5 years ago by Patty Whitman
Posted over 5 years ago by Alex Yang

You need to be careful when manually fiddling with migration files. Rails uses migration files as a record of all the changes you’ve ever made to your database. Manually deleting a migration file that Rails thinks should exist is like rewriting history. And if you’ve watched enough movies, you know that rewriting history is never a good idea. Here’s what you can do to fix the situation safely.

If you’ve created the migration file but haven't run the migration yet (‘rake db:migrate’), you can just delete that specific migration file under your db/migrate folder and generate another one.

On the other hand, if you’ve already run the migration, you should first run 'rake db:migrate:status' to see a list of all your migrations. As long as the migration you want to modify is the most recent one and it shows as 'up', you can run 'rake db:rollback'. That will un-run the migration so it becomes 'down' (you can confirm with 'rake db:migrate:status' again). Once it's down, you can delete the migration file under your db/migrate folder safely, and then generate a new one.

Posted about 5 years ago by Harry Lee

Hi Alex,
So luckily, the migration i want to un-run was the most recent one. However, I've got the following response from gitbash:

SQLite3::SQLException: duplicate column name: name: ALTER TABLE "users" ADD "nam
e" varchar(255)c:in `migrate'
Tasks: TOP => db:rollback

and has failed to un-run previous migration.

Is there a reason for this?

Posted about 5 years ago by Alex Yang

Can you tell me which commands you ran as well as what your migration file contains? You'll be able to find this under db/migrations

Posted about 5 years ago by Harry Lee

I did
removename from users

Posted about 5 years ago by Harry Lee

This are my rake db:migrate:status
database: c:/Sites/etsydemo/db/development.sqlite3

Status Migration ID Migration Name
up 20140618223709 Create listings
up 20140619215549 Add attachment image to listings
up 20140627190736 Devise create users
up 20140627214225 Add name to users
up 20140627223922 Add user id to listings
up 20140628094745 Add university to listings
up 20140628095310 Add features to listings
up 20140705100221 Create tutorial requests
up 20140705103211 Add listing id to tutorial requests
up 20140705104440 Add fields to tutorial requests
up 20140726185156 Create reviews
up 20140730150950 Add user id to reviews
up 20140730163643 Add listing id to reviews
up 20140731153741 Add fields to users
up 20140731160708 Remove name from users

then as you have said i did rake db:rollback

remove_name_from_users.rb contains:
class RemoveNameFromUsers < ActiveRecord::Migration
def change
remove_column :users, :name, :string

Posted about 5 years ago by Alex Yang

The problem is that you edited the migration file manually after it was already run. Now, when Rails tries to undo it, it's trying to undo a change that was never made in the first place. Instead, you need to manually change the migration back to what it was before, then run 'rake db:rollback', and then delete the migration file manually.

Posted about 5 years ago by Harry Lee

perfect! got it to work thanks!:)