我试图改变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
但强迁移抛出错误,并说如下:
=== 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
我明白db锁定的意义和危险。。但我的问题很傻,我还没有找到答案。我怎么能做到第二、第四和第五点?如何强制ActiveRecord模型同时写入两个列,并在回填后切换到正确的列?
2条答案
按热度按时间3lxsmp7m1#
假设旧列为
length
,新列为length_advanced
。2 -写入两列
4 -将读取从旧列移动到新列
5 -停止向旧栏中写入内容
更多关于https://api.rubyonrails.org/classes/ActiveRecord/Base.html
qqrboqgw2#
在你的情况下,这应该起作用: