Module: Switchman::ActiveRecord::Migrator

Defined in:
lib/switchman/active_record/migration.rb

Instance Method Summary collapse

Instance Method Details

#generate_migrator_advisory_lock_idObject

significant change: use the shard name instead of the database name in the lock id. Especially if you’re going through pgbouncer, the database name you’re accessing may not be consistent



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/switchman/active_record/migration.rb', line 28

def generate_migrator_advisory_lock_id
  db_name_hash = Zlib.crc32(Shard.current.name)
  shard_name_hash = ::ActiveRecord::Migrator::MIGRATOR_SALT * db_name_hash
  # Store in internalmetadata to allow other tools to be able to lock out migrations
  if ::Rails.version < "7.1"
    ::ActiveRecord::InternalMetadata[:migrator_advisory_lock_id] = shard_name_hash
  else
    ::ActiveRecord::InternalMetadata.new(connection)[:migrator_advisory_lock_id] = shard_name_hash
  end
  shard_name_hash
end

#with_advisory_lock_connectionObject

significant change: strip out prefer_secondary from config



41
42
43
44
45
46
47
48
49
# File 'lib/switchman/active_record/migration.rb', line 41

def with_advisory_lock_connection
  pool = ::ActiveRecord::ConnectionAdapters::ConnectionHandler.new.establish_connection(
    ::ActiveRecord::Base.connection_db_config.configuration_hash.except(:prefer_secondary)
  )

  pool.with_connection { |connection| yield(connection) } # rubocop:disable Style/ExplicitBlockArgument
ensure
  pool&.disconnect!
end