ruby - Running migrations on my Rails DB, adding a column quietly and randomly drops a table without a console error -
i able discouver problem walking through running migrations. last 2 days i've been trying somthing simple. remove 1 column verdict:text
simulations
table , add column: opinions:hash
.
doing caused variety of errors, such no method 'my_sym' , saying simulation table didn't exist.
i thought had resolved 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) |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 hash column worked out before run create_table :simulations |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 square 1 (all tests worked seemed in initial state had before started having problems), re-wrote 2 offending migrations thinking might problem, ran 2 migrations in order (i removed column added other one, opposite order of tried previous, thinking maybe order had 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
edit: further investigation 4th migration causing problem of dropping simulations table without error.
both these migrations worked without error. know doing else cause error (example running rspec), because looks problem 1 of these migration causes error schema.rb
file.
after running these last migrations 3&4 schema.rb
had both users , simulations tables looks this:
activerecord::schema.define(version: 20150807193122) # not dump table "simulations" because of following nomethoderror # undefined method `[]' nil:nilclass create_table "users", force: :cascade |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:
# not dump table "simulations" because of following nomethoderror # undefined method `[]' nil:nilclass
can shed light on whats happening here or how can fix it? i've been stuck on hours.
one thing maybe worth noting, when re-run rake db:schema:dump
need comment out factories reason, seem cause errors simulation table existing. not sure why they're being called thought info might help, here both are:
factorygirl.define factory :simulation |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 factory :user email "user_#{user.last.nil? ? 1 : user.last.id + 1}@home.com" password "password" end end
i think problem right here:
class addopiniontosimulations < activerecord::migration def change add_column :simulations, :opinion, :hash end end
there's no such thing hash
column type. sqlite will, unfortunately, let create columns type want (with unrecognized things being treated strings) activerecord won't know them. result migration works schema dump fails because activerecord can't say:
t.hash :opinion
in schema.rb
file.
if think want store hash in column, you'd create text
column:
add_column :simulations, :opinion, :text
and use serialize
in model:
serialize :opinion, hash
of course leaves opaque blob of yaml in database won't able (sanely) query should used if okay that.
a better solution normalize opinion
hashes store information in separate tables/models.
Comments
Post a Comment