Module: Departure::RailsPatches::ActiveRecordMigratorWithAdvisoryLockPatch

Defined in:
lib/departure/rails_patches/active_record_migrator_with_advisory_lock_patch.rb

Constant Summary collapse

RELEASE_LOCK_FAILED_MESSAGE =
'Failed to release advisory lock from ActiveRecordMigratorWithAdvisoryLockPatch'
.freeze

Instance Method Summary collapse

Instance Method Details

#with_advisory_lockObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/departure/rails_patches/active_record_migrator_with_advisory_lock_patch.rb', line 7

def with_advisory_lock
  return super if Departure.configuration.disable_rails_advisory_lock_patch

  lock_id = generate_migrator_advisory_lock_id
  @__original_connection = connection

  got_lock = @__original_connection.get_advisory_lock(lock_id)
  raise ActiveRecord::ConcurrentMigrationError unless got_lock

  load_migrated # reload schema_migrations to be sure it wasn't changed by another process before we got the lock
  yield
ensure
  if got_lock && !@__original_connection.release_advisory_lock(lock_id)
    raise ActiveRecord::ConcurrentMigrationError, RELEASE_LOCK_FAILED_MESSAGE
  end
end