module Departure::Migration

Hooks Departure into Rails migrations by replacing the configured database adapter.

It also patches ActiveRecord's migrate method so that it patches LHM first. This will make migrations written with LHM to go through the regular Rails Migration DSL.

Public Instance Methods

departure_migrate(direction) click to toggle source

Replaces the current connection adapter with the PerconaAdapter and patches LHM, then it continues with the regular migration process.

@param direction [Symbol] :up or :down

# File lib/departure/migration.rb, line 45
def departure_migrate(direction)
  reconnect_with_percona
  include_foreigner if defined?(Foreigner)

  ::Lhm.migration = self
  active_record_migrate(direction)
end
include_foreigner() click to toggle source

Includes the Foreigner's Mysql2Adapter implemention in DepartureAdapter to support foreign keys

# File lib/departure/migration.rb, line 66
def include_foreigner
  Foreigner::Adapter.safe_include(
    :DepartureAdapter,
    Foreigner::ConnectionAdapters::Mysql2Adapter
  )
end
migrate(direction) click to toggle source

Migrate with or without Departure based on uses_departure class attribute.

# File lib/departure/migration.rb, line 55
def migrate(direction)
  if uses_departure?
    departure_migrate(direction)
  else
    reconnect_without_percona
    active_record_migrate(direction)
  end
end
reconnect_with_percona() click to toggle source

Make all connections in the connection pool to use PerconaAdapter instead of the current adapter.

# File lib/departure/migration.rb, line 75
def reconnect_with_percona
  return if connection_config[:adapter] == 'percona'
  Departure::ConnectionBase.establish_connection(connection_config.merge(adapter: 'percona'))
end
reconnect_without_percona() click to toggle source

Reconnect without percona adapter when Departure is disabled but was enabled in a previous migration.

# File lib/departure/migration.rb, line 82
def reconnect_without_percona
  return unless connection_config[:adapter] == 'percona'
  Departure::ConnectionBase.establish_connection(connection_config.merge(adapter: original_adapter))
end

Private Instance Methods

configuration_hash() click to toggle source
# File lib/departure/migration.rb, line 96
        def configuration_hash
  if ActiveRecord::VERSION::STRING >= '6.1'
    ActiveRecord::Base.connection_db_config.configuration_hash
  else
    ActiveRecord::Base.connection_config
  end
end
connection_config() click to toggle source

Capture the type of the adapter configured by the app if not already set.

# File lib/departure/migration.rb, line 90
def connection_config
  configuration_hash.tap do |config|
    self.class.original_adapter ||= config[:adapter]
  end
end