Module: Departure::Migration

Extended by:
ActiveSupport::Concern
Defined in:
lib/departure/migration.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#departure_migrate(direction) ⇒ Object

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



45
46
47
48
49
50
51
# 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_foreignerObject

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



66
67
68
69
70
71
# File 'lib/departure/migration.rb', line 66

def include_foreigner
  Foreigner::Adapter.safe_include(
    :DepartureAdapter,
    Foreigner::ConnectionAdapters::Mysql2Adapter
  )
end

#migrate(direction) ⇒ Object

Migrate with or without Departure based on uses_departure class attribute.



55
56
57
58
59
60
61
62
# 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_perconaObject

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



75
76
77
78
# 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_perconaObject

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



82
83
84
85
# File 'lib/departure/migration.rb', line 82

def reconnect_without_percona
  return unless connection_config[:adapter] == 'percona'
  Departure::OriginalAdapterConnection.establish_connection(connection_config.merge(adapter: original_adapter))
end