Rails迁移MySQL算法: :concurrently的替代方案

3

我有一个 Rails 迁移文件,要向一个非常大的表添加索引,并意识到引入一项会锁定该表且可能会阻塞 Semaphore 构建处理的迁移操作风险很大。因此,我选择了安全的方式,触发了并发索引构建。

class AddIndexToEventsTable < ActiveRecord::Migration[5.2]
disable_ddl_transaction!

def change
  add_index :events, [:status, :created_at], algorithm: :concurrently
end
end

但是迁移后,发现不成功,在这里出现了错误:

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Algorithm must be one of the following: :default, :copy, :inplace

我正在使用 rails 5.2.5

我该如何在MYSQL上实现类似于algorithm: :concurrently在PostGres中的功能。


你的Rails版本不如mysql版本重要。https://dev59.com/3G855IYBdhLWcg3wnFpO。根据版本,你可能实际上并没有问题。 - Joel Blum
:concurrently更改为 :default - Rick James
1个回答

1

为了确保您没有任何锁定,您想要的选项是

LOCK=NONE

很遗憾,我不认为Rails迁移支持此选项。一个可能的解决方案是手动构建SQL并使用execute运行它。

下面可以看到一个示例:

class AddIndexToEventsTable < ActiveRecord::Migration[5.2]
disable_ddl_transaction!

 def change
    execute <<~SQL
      ALTER TABLE events
      ADD INDEX index_events_on_status_created_at(status, created_at),
      ALGORITHM=DEFAULT,
      LOCK=NONE;
    SQL
 end
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接