Jesper Rønn-Jensen
Jesper Rønn-Jensen

Reputation: 111716

How to handle failing migrations on reset because of deleted models/methods

An old one of my ruby on rails migrations contains both the actual migration but also an action to modify data:

class AddTypeFlagToGroup < ActiveRecord::Migration
  def self.up
    add_column :groups, :selection_type, :string

    Group.reset_column_information
    Group.transaction do
    Group.all.each do |group|
      group.selection_type = group.calculate_selection_type
      group.save
    end
  end
  end

  def self.down
    remove_column :groups, :selection_type
  end
end

In this migration there are the usual add_column and remove_column migration statements. But there are also some model specific method calls.

I wrote this a couple of weeks ago. Since then, I have removed my Group model, which gives an error when I do a full migration with :reset.

rake db:migrate:reset
(in /Users/jesper/src/pet_project)

[...]

==  AddTypeFlagToGroup: migrating =============================================
-- add_column(:groups, :selection_type, :string)
   -> 0.0012s
rake aborted!
An error has occurred, this and all later migrations canceled:

uninitialized constant AddTypeFlagToGroup::Group

The thing is that in the current revision of my code, Group does not exist. How should I handle this "the rails way"??

I am thinking I could modify the migration by commenting out the Group.xxx stuff, but is this a wise way to go?

Upvotes: 1

Views: 187

Answers (1)

srboisvert
srboisvert

Reputation: 12759

There is no value in leaving the group stuff in your migration now that it is gone from your project. I'd just edit the migration, drop everything from the db and migrate from scratch. There isn't even a reason to comment it out (you are using version control right?)

Also, I believe the "rails way" with migrations is spelled "Arrrrgh!"

Upvotes: 1

Related Questions