Ruby on Rails migration stuck -
i'm new ruby on rails , grateful migrations. version 4
first migration
class createrooms < activerecord::migration def change create_table :rooms |t| t.integer :legacy_id, null: true t.string :tag, null: false t.string :name_en, null: false t.string :name_et, null: false t.string :name_ru, null: false t.string :color, default: 'black' t.timestamp :enabled_from, default: 0 t.timestamp :enabled_to, default: 0 t.timestamps null: false end add_index :rooms, :tag, unique: true add_index :rooms, :name_en, unique: true add_index :rooms, :name_et, unique: true add_index :rooms, :name_ru, unique: true end end
next one
class adddefaultvaluestoroom < activerecord::migration def change add_column :rooms, :priority, :integer, null: false change_column :rooms, :enabled_from, :datetime, default: '2008-08-15' change_column :rooms, :enabled_to, :datetime, default: '2050-12-31' add_index :rooms, :priority, unique: true end end
strangely enough not reflected in schema
create_table "rooms", force: :cascade |t| t.integer "legacy_id" t.string "tag", null: false t.string "name_en", null: false t.string "name_et", null: false t.string "name_ru", null: false t.string "color", default: "black" t.datetime "enabled_from", default: '2008-08-15 00:00:00' t.datetime "enabled_to", default: '2050-12-31 00:00:00' t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "rooms", ["name_en"], name: "index_rooms_on_name_en", unique: true add_index "rooms", ["name_et"], name: "index_rooms_on_name_et", unique: true add_index "rooms", ["name_ru"], name: "index_rooms_on_name_ru", unique: true add_index "rooms", ["tag"], name: "index_rooms_on_tag", unique: true
rake db:rollback crashes
rake db:rollback step=2 --trace ** invoke db:rollback (first_time) ** invoke environment (first_time) ** execute environment ** invoke db:load_config (first_time) ** execute db:load_config ** execute db:rollback activerecord::schemamigration load (0.2ms) select "schema_migrations".* "schema_migrations" activerecord::schemamigration load (0.2ms) select "schema_migrations".* "schema_migrations" migrating adddefaultvaluestoroom (20160127121254) (0.1ms) begin transaction == 20160127121254 adddefaultvaluestoroom: reverting =========================== (0.1ms) rollback transaction rake aborted! standarderror: error has occurred, , later migrations canceled: activerecord::irreversiblemigration/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/migration/command_recorder.rb:65:in `inverse_of'
now i'm in fix, me out ?
tia pavel
change_column
creates irreversible change database, means can't roll back. see guide: http://guides.rubyonrails.org/active_record_migrations.html#changing-columns
the idea here reverting data result in data loss, rails prevents doing destructive default , throws error stop process.
change last migration utilize reversible
or up
/down
methods, in order explicitly tell rails how reverse change (note lose data stored in new column priority
). see: http://guides.rubyonrails.org/active_record_migrations.html#using-reversible
here's example using up
/down
methods:
class adddefaultvaluestoroom < activerecord::migration def add_column :rooms, :priority, :integer, null: false change_column :rooms, :enabled_from, :datetime, default: '2008-08-15' change_column :rooms, :enabled_to, :datetime, default: '2050-12-31' add_index :rooms, :priority, unique: true end def down remove_column :rooms, :priority change_column :rooms, :enabled_from, :datetime, default: 0 change_column :rooms, :enabled_to, :datetime, default: 0 end end
Comments
Post a Comment