vendredi 7 août 2015

Running migrations on my Rails DB, adding a column quietly and randomly drops a table without a console error

I was only able to discouver this problem by slowly walking through running my migrations. For the last two days I've been trying to do somthing simple. Remove one column verdict:text from my simulations table and add another column: opinions:hash.

Doing this caused a variety of errors, such as no method 'my_sym' and saying the simulation table didn't exist.

I initially thought I had resolved this by doing:

rake db:drop
rake db:schema:dump
rake db:migrate VERSION="<Migration1>"
rake db:migrate VERSION="<Migration2>"
rake db:setup ENV="test"

Migration 1:

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              :null => false, :default => "", limit: 96
      t.string :encrypted_password, :null => false, :default => "", limit: 60

      t.timestamps

      t.index :email, unique: true
    end
  end
end

Migration 2:

class CreateSimulations < ActiveRecord::Migration
  def change
    # Needs the hash column worked out before this is run 
    create_table :simulations do |t|
        t.integer :x_size
        t.integer :y_size
        t.string :verdict
        t.string :arrangement 
    end

    add_reference :simulations, :user, index: true    
  end
end

This got me back to square one (all my tests worked everything seemed to be in my initial state I had before started having problems), so I re-wrote the two offending migrations thinking they might be the problem, ran the two migrations in this order (I removed the column then added the other one, the opposite order of what I tried previous, thinking maybe the order had some effect).

Migration 3:

class RemoveVerdictFromSimulation < ActiveRecord::Migration
  def change
    remove_column :simulations, :verdict, :string
  end
end

Migrations 4:

class AddOpinionToSimulations < ActiveRecord::Migration
  def change
    add_column :simulations, :opinion, :hash
  end
end

Both these migrations worked without error. However I know doing anything else will cause an error (example running rspec), because it looks like the problem is that one of these migration causes an error with the schema.rb file.

After running these last migrations 3&4 my schema.rb which previously had both users and simulations tables now looks like this:

ActiveRecord::Schema.define(version: 20150807193122) do

# Could not dump table "simulations" because of following NoMethodError
#   undefined method `[]' for nil:NilClass

  create_table "users", force: :cascade do |t|
    t.string   "email",              limit: 96, default: "", null: false
    t.string   "encrypted_password", limit: 60, default: "", null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true

end

The particular line of interest here being:

# Could not dump table "simulations" because of following NoMethodError
#   undefined method `[]' for nil:NilClass

Can anyone shed some light on whats happening here or how I can fix it? I've been stuck on this for hours.

One thing maybe worth noting, when I re-run the rake db:schema:dump I need too comment out my factories for some reason, as they seem to cause errors with simulation table existing. Not sure why they're being called just thought this info might help, here they both are:

FactoryGirl.define do 
    factory :simulation do |f|
        f.id (Simulation.last.nil? ? 1 : Simulation.last.id + 1)
        f.x_size 3
        f.y_size 3
        f.user_id 1
    end 
end 

--

FactoryGirl.define do
    factory :user do
        email "user_#{User.last.nil? ? 1 : User.last.id + 1}@home.com"
        password "password"
    end
end

Aucun commentaire:

Enregistrer un commentaire