Rails 4 迁移以将列数据类型从字符串更改为整数并返回保留数据 (postgres)
Rails 4 migration to change columns data type from string to integer and back preserving data (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/models/system.rb
...
enum operation { start: 0, stop: 1 }
enum status { init: 0, working: 1, complete: 2 }
...
如何更新旧数据?
您可以在 2 个迁移步骤中完成
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
如果需要,请不要忘记编写回滚代码
经过一些研究,我发现这是一个合适的解决方案。
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
我需要将 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/models/system.rb
...
enum operation { start: 0, stop: 1 }
enum status { init: 0, working: 1, complete: 2 }
...
如何更新旧数据?
您可以在 2 个迁移步骤中完成
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
如果需要,请不要忘记编写回滚代码
经过一些研究,我发现这是一个合适的解决方案。
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