【发布时间】:2020-11-27 10:41:47
【问题描述】:
我正在尝试更改 PostgreSQL (Rails 6) 中的列类型。
迁移:
# frozen_string_literal: true
class ChangeUsersEmailToCitext < ActiveRecord::Migration[6.0]
def up
enable_extension('citext')
change_column :users, :email, :citext
end
def down
change_column :users, :email, :string
end
end
但是 Strong Migrations 抛出错误并显示如下:
=== Dangerous operation detected #strong_migrations ===
Changing the type of an existing column blocks reads and writes
while the entire table is rewritten. A safer approach is to:
1. Create a new column
2. Write to both columns
3. Backfill data from the old column to the new column
4. Move reads from the old column to the new column
5. Stop writing to the old column
6. Drop the old column
我了解数据库锁定的意义和危险......但我的问题很愚蠢,我还没有找到答案。从字面上看,我如何才能获得第 2、第 4 和第 5 分?如何强制我的 ActiveRecord 模型同时写入两列并在回填后切换到正确的列?
【问题讨论】:
标签: ruby-on-rails postgresql rails-activerecord rails-migrations