module SimpleDataMigrations::ConcurrentRun

Constants

MIGRATOR_SALT

Public Class Methods

with_advisory_lock() { || ... } click to toggle source
# File lib/simple_data_migrations/concurrent_run.rb, line 10
def self.with_advisory_lock
  with_advisory_lock_connection do |connection|
    lock_id = MIGRATOR_SALT * Zlib.crc32(connection.current_database)

    got_lock = connection.get_advisory_lock(lock_id)
    raise Error unless got_lock

    yield
  ensure
    got_lock && connection.release_advisory_lock(lock_id)
  end
end

Private Class Methods

with_advisory_lock_connection() { |connection| ... } click to toggle source

Caused by multi db specific connection flow. More on github.com/rails/rails/pull/38235

# File lib/simple_data_migrations/concurrent_run.rb, line 24
def self.with_advisory_lock_connection
  pool = ActiveRecord::ConnectionAdapters::ConnectionHandler.new.establish_connection(
    ActiveRecord::Base.connection_db_config
  )

  pool.with_connection { |connection| yield(connection) }
end