Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: how to rollback a botched migration

I'm an idiot...screwed up a migration in Rails:

thinking migrations would work like model generators (using references:modelname) I did the following:

$ rails g migration add_event_to_photos references:event

which created the migration

class AddEventToPhotos < ActiveRecord::Migration
  def change
    add_column :photos, :references, :event
  end
end

And now my development database (SQLite3) has a references column of type event in the photos table.

And my schema.rb has a line in the middle saying:

# Could not dump table "photos" because of following StandardError
#   Unknown type 'event' for column 'references'

rake db:rollback is powerless against this:

$ rake db:rollback
==  AddEventToPhotos: reverting ===============================================
-- remove_column("photos", :references)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `to_sym' for nil:NilClass

So, how to roll back and maintain my development data in the database? I'd even be happy trashing the photos table if that's my only choice..but don't want to have to rebuild the whole thing. What to do?


btw- for anyone reading this about to make same stupid mistake...don't! Use the correct migration generator:

$ rails g migration add_event_to_photos event_id:integer
like image 358
Meltemi Avatar asked Aug 10 '11 22:08

Meltemi


1 Answers

The easiest way I found to do this was to recreate the table in the schema.rb file in /db/. Afterwards I ran a rake db:reset (if it says you have pending migrations, just delete them and try again).

This took care of the problem.

like image 75
Mohamad Avatar answered Sep 30 '22 02:09

Mohamad