Ruby-on-rails Rails 迁移:self.up 和 self.down 与变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10365129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:19:36  来源:igfitidea点击:

Rails migrations: self.up and self.down versus change

ruby-on-railsmigration

提问by banditKing

Looks like the new rails version has "change" versus self.up and self.down methods.

看起来新的 rails 版本对 self.up 和 self.down 方法有“改变”。

So what happens when one has to roll back a migration how does it know what actions to perform. I have the following method that I need to implement based on an online tutorial:

那么当必须回滚迁移时会发生什么,它如何知道要执行哪些操作。我有以下方法需要根据在线教程实现:

class AddImageToUsers < ActiveRecord::Migration
  def self.up
    add_column :users, :image_file_name, :string
    add_column :users, :image_content_type, :string
    add_column :users, :image_file_size, :integer
    add_column :users, :image_updated_at, :datetime
  end

  def self.down
    remove_column :users, :image_file_name, :string
    remove_column :users, :image_content_type, :string
    remove_column :users, :image_file_size, :integer
    remove_column :users, :image_updated_at, :datetime
  end    
end

How can I do the same using the new change method?

如何使用新的更改方法执行相同的操作?

回答by Aldo 'xoen' Giambelluca

For many operations rails can guess what is the inverse operation(without problems). For example, in your case what is the reverse operation of add_columnto call when you rollback? Of course it's remove_column. What is the inverse of create_table? It's drop_table. So in these cases rails know how to rollback and define a downmethod is superfluous (you can see in the documentation the methods currently supported from the change method).

对于许多操作,rails 可以猜测逆操作是什么(没有问题)。例如,在您的情况下,add_column回滚时调用的反向操作是什么?当然是remove_column。的反义词是create_table什么?它是drop_table。所以在这些情况下,rails 知道如何回滚并定义一个down方法是多余的(你可以在文档中看到当前支持方法从 change 方法)。

But pay attention because for some kind of operation you still need to define the downmethod, for example if you change the precision of a decimal column how to guess the original precision on rollback? It's not possible, so you need to define the downmethod.

但是要注意,因为对于某种操作,您仍然需要定义down方法,例如如果您更改小数列的精度如何在回滚时猜测原始精度?这是不可能的,所以你需要定义down方法。

As said, I suggest you to read the Rails Migrations Guide.

如上所述,我建议您阅读Rails 迁移指南

回答by Kaleem Ullah

Better to use Up, Down, Change:

最好使用 Up、Down、Change:

On Rails 3 (Reversible):which should add new column on up and fill all records in table only on up, and only delete this column on down

On Rails 3(可逆):它应该在向上添加新列并仅在向上填充表中的所有记录,并且仅在向下时删除此列

def up
  add_column :users, :location, :string
  User.update_all(location: 'Minsk')
end

def down
  remove_column :users, :location
end

But:

但:

You had to avoid using change method which allows to save some time. For example, if you didn't need to update column value immediately after it's adding you would cut this code down to like this:

您必须避免使用可以节省一些时间的更改方法。例如,如果您不需要在添加列值后立即更新列值,您可以将此代码缩减为如下所示:

def change
  add_column :users, :location, :string
end

On up it will add column to table and remove it on down. Much less code and it's a profit.

向上它会将列添加到表并向下删除它。代码少得多,这是一种利润。

On Rails 4:one more useful way to write what we need in one place:

On Rails 4:一种更有用的方法,可以在一个地方编写我们需要的东西:

def change
  add_column :users, :location, :string
  reversible do |direction|
    direction.up { User.update_all(location: 'Minsk') }
  end
end

回答by nothing-special-here

class AddImageToUsers < ActiveRecord::Migration
  def change
    add_column :users, :image_file_name, :string
    add_column :users, :image_content_type, :string
    add_column :users, :image_file_size, :integer
    add_column :users, :image_updated_at, :datetime
  end
end