美文网首页
active_record 往已有表添加jsonb column

active_record 往已有表添加jsonb column

作者: ifree321 | 来源:发表于2019-09-29 21:05 被阅读0次

https://blog.bitwrangler.com/2016/11/30/performant-postgres-migrations.html

slow

class AddInfoColumnToCaseDocketReport < ActiveRecord::Migration
  def change
    add_column :case_docket_reports, :info, '{}'
  end
end

faster

Add columns without default values

class AddInfoColumnToCaseDocketReport < ActiveRecord::Migration
  def change
    add_column :case_docket_reports, :info, :jsonb
    change_column_default :case_docket_reports, :info,  '{}'
  end
end

并行往postgres添加index的问题解决,快且不阻塞

class AddIndexToAsksActive < ActiveRecord::Migration
  disable_ddl_transaction!

  def change
    add_index :asks, :active, algorithm: :concurrently
  end
end

相关文章

网友评论

      本文标题:active_record 往已有表添加jsonb column

      本文链接:https://www.haomeiwen.com/subject/ccboectx.html