2

I'm on a git branch and trying to rollback two migrations, before destroying the branch. The most recent migration adds a column to a table that I want to keep (and which is part of master, not the branch to be dropped), so dropping the whole table is not a solution (unless I have to recreate it again). Anyways, I must have done something wrong, because when I tried to remove the apple_id column from the scores table, I got this abort error.

This is the migration that I'm trying to rollback

add_column :scores, :apple_id, :integer

However, the error message (see below) is referring to the indexes that I created with the original migration (part of master branch) that created the table

add_index :scores, [:user_id, :created_at, :funded, :started]

Can you suggest what I might do?

==  AddAppleidColumnToScores: reverting =======================================
-- remove_column("scores", :apple_id)
rake aborted!
An error has occurred, this and all later migrations canceled:

Index name 'temp_index_altered_scores_on_user_id_and_created_at_and_funded_and_started' on table 'altered_scores' is too long; the limit is 64 characters

Update: reading this SO question How do I handle too long index names in a Ruby on Rails migration with MySQL?, I got some more information about the source of the problem but don't know how to solve it. Both sql and postgres have 64 character limits

Index name 'index_studies_on_user_id_and_university_id_and_subject_\
           name_id_and_subject_type_id' on table 'studies' is too long; \
           the limit is 64 characters

The accepted answer for the question I refer to says to give the index a name, although I'm not sure how I could do this now that I'm trying to rollback.

add_index :studies, ["user_id", "university_id", \
          "subject_name_id", "subject_type_id"], 
          :unique => true, :name => 'my_index'

Update: in response to the comments, I'm using Rails 3.2.12. This is the migration that adds the column

class AddAppleidColumnToScores < ActiveRecord::Migration
  def change
    add_column :scores, :apple_id, :integer
  end
end

Furthermore, the reason why I didn't want to drop the table was that I was unsure about what problems it might cause in recreating it since a) the main part was created on branch master, while a column added on a branch and b) I was unsure about what to do with the migration file for the dropped table? since it was the fourth (of about 10) tables I created, I don't know how to run it and only it again.

Community
  • 1
  • 1
Leahcim
  • 40,649
  • 59
  • 195
  • 334
  • What version of Rails are you using? I've recreated a project from scratch with Rails v3.1.0 but the rollback is doing fine. I'm also using PG v9.0.1. When I create an index as `add_index :scores, [:user_id, :created_at, :funded, :started]`, the index name is `index_scores_on_user_id_and_created_at_and_funded_and_started`. – fro_oo Mar 15 '13 at 22:39
  • If you don't want to drop and recreate your database from scratch, I strongly advise you to remove the index manually, in the db console. And, can you paste here your full migration `AddAppleIdColumnToScores`and your schema.rb before rollback? – fro_oo Mar 15 '13 at 22:40
  • Did you try to remove the index with pure SQL? – fro_oo Mar 18 '13 at 09:24
  • @Fro_oo I ended up dropping the table. Seemed easiest. – Leahcim Mar 18 '13 at 15:05
  • Ok, happy you could find a way to solve your problem. – fro_oo Mar 20 '13 at 09:09

1 Answers1

0

Can you copy/paste your migration?

Here's mine:

class AddAppleIdColumnToScores < ActiveRecord::Migration

  def self.up
    add_column :scores, :apple_id, :integer
    add_index :scores, [:user_id, :created_at, :funded, :started]
  end

  def self.down

    # delete index MySql way
    # execute "DROP INDEX index_scores_on_user_id_and_created_at_and_funded_and_started ON scores"

    # delete index Postgresql way
    # execute "DROP INDEX index_scores_on_user_id_and_created_at_and_funded_and_started"

    # delete index Rails way / not necessary if you remove the column
    # remove_index :scores, [:user_id, :created_at, :funded, :started]

    remove_column :scores, :apple_id
  end
end
fro_oo
  • 1,610
  • 4
  • 24
  • 46
  • I updated the OP with the Rails v (3.2.12) and migration. I'm guessing I should just drop the table. How do I do that, and what's the best way to recreate it? Do I make a new migration or just migrate the migration that created that table? Does it matter that that migration is not the most recent? (i.e. it was about the 4th migration I created, and I've now done about 10, what happens with the last six if I drop table and try to recreate?) – Leahcim Mar 16 '13 at 00:08
  • I advise you to add a new migration to drop the table if needed. Maybe you should raise a `ActiveRecord::IrreversibleMigration` for down method (rollback). But, consider to recreate your full db in a clean way if you think you're messing too much with your migrations. – fro_oo Mar 18 '13 at 09:23