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
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
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 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
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 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
# 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
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