Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Migrations MySQL algorithm: :concurrently alternative

I have this rails migration, I'm adding an index to a very large table and am aware of the fact that introducing a migration that would lock the table and potentially block build processing on Semaphore is quite risky. So I used the safe route, and triggered a concurrent index build instead

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

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

but after migrating, it turns out to be unsuccessful here's the error:

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

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

Im using rails 5.2.5

How can I replicate the functionality algorithm: :concurrently has with PostGres with MYSQL.

like image 565
Muhammad Ushay Avatar asked Dec 13 '25 02:12

Muhammad Ushay


1 Answers

To make sure you don't have any locks the option you want is

LOCK=NONE

Sadly I do not believe rails migrations have support for this option. One possible solution is to manually build the SQL and run it with execute.

An example can be seen below:

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
like image 141
Gino Avatar answered Dec 14 '25 17:12

Gino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!