Rails 4迁移将列的数据类型从字符串更改为整数,并保留数据(postgres)



我需要将string字段转换为integer并使用enum代替。在不丢失数据的情况下做到这一点的最佳方法是什么?

当前迁移:

class CreateSystems < ActiveRecord::Migration
  def change
    create_table :systems do |t|
      t.string :operation
      t.string :status
      t.timestamps null: false
    end
  end
end

然后我改变字段的类型,像这样:

class ChangeColumnsForSystems < ActiveRecord::Migration
  def change
    change_column :systems, :operation, :integer
    change_column :systems, :status, :integer
  end
end

并更新模型文件。

/app/模型/system.rb

...
enum operation { start: 0, stop: 1 }
enum status { init: 0, working: 1, complete: 2 }
...

如何更新旧数据?

经过一番研究,我发现这是一个合适的解决方案。

class ChangeColumnsForSystems < ActiveRecord::Migration
  def change
    change_column :systems, :operation, "integer USING (CASE operation WHEN 'start' THEN '0'::integer ELSE '1'::integer END)", null: false
    change_column :systems, :status, "integer USING (CASE status WHEN 'init' THEN '0'::integer WHEN 'working' THEN '1'::integer ELSE '2'::integer END)", null: false
  end
end

更新:在某些情况下,您必须在更改类型之前删除默认值。以下是带有回滚的版本。

class ChangeColumnsForSystems < ActiveRecord::Migration
  def up
    change_column_default :systems, :status, nil
    change_column :systems, :operation, "integer USING (CASE operation WHEN 'start' THEN '0'::integer ELSE '1'::integer END)", null: false
    change_column :systems, :status, "integer USING (CASE status WHEN 'init' THEN '0'::integer WHEN 'working' THEN '1'::integer ELSE '2'::integer END)", null: false, default: 0
  end
  def down
    change_column_default :systems, :status, nil
    change_column :systems, :operation, "varchar USING (CASE operation WHEN '0' THEN 'start'::varchar ELSE 'stop'::varchar END)", null: false
    change_column :systems, :status, "varchar USING (CASE status WHEN '0' THEN 'init'::varchar WHEN '1' THEN 'working'::varchar ELSE 'complete'::varchar END)", null: false, default: 'init'
  end
end

可以分两个步骤完成

1。重命名当前的operation列,并添加新的必要类型

def up
    rename_column :systems, :operation, :operation_str
    add_column :systems, :operation, ... # your options
end

2。将值从旧列移动到新列并删除旧列

def up
    System.all.each do |sys|
        sys.operation = sys.operation_str.to_i # replace it with your converter
    end
    remove_column :systems, :operation
end

如果有必要,不要忘记写回滚代码

相关内容