如何回滚迁移?

时间:2015-01-09 20:19:12

标签: ruby-on-rails ruby

我已经成功完成了三次迁移。

20150109133619_add_items.rb:

class AddItems < ActiveRecord::Migration
  def change
    create_table :pros do |t|
      t.string :titlerr
      t.text :description
      t.string :image_url
      t.decimal :price

      t.timestamps
    end
  end
end

20150109134955_add_col.rb:

class AddCol < ActiveRecord::Migration
  def change
    add_column :pros, :fieldname, :string
  end
end

20150109162301_del_col.rb:

class DelCol < ActiveRecord::Migration
  def change
    remove_column :pros, :fieldname
  end
end

现在我需要在迁移20150109134955之前回滚状态。我做了以下事情:

rake db:rollback

结果是以下消息:

==  DelCol: reverting =========================================================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

remove_column is only reversible if given a type./home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/migration/command_recorder.rb:128:in `invert_remove_column'
/home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/migration/command_recorder.rb:66:in `inverse_of'
........
........
........
/home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:126:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)

我的第二次尝试是:

rake db:rollback VERSION=20150109134955

结果是以下消息:

==  DelCol: reverting =========================================================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

remove_column is only reversible if given a type./home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/migration/command_recorder.rb:128:in `invert_remove_column'
/home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/migration/command_recorder.rb:66:in `inverse_of'
.....
.....
.....
/home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:126:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)

1 个答案:

答案 0 :(得分:0)

将数据类型添加到20150109162301_del_col.rb

class DelCol < ActiveRecord::Migration
  def change
    remove_column :pros, :fieldname, :string
  end
end