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
Constant Summary collapse
- DEPARTURE_ADAPTERS =
%w[percona percona_trilogy].freeze
Instance Method Summary collapse
-
#departure_migrate(direction) ⇒ Object
Replaces the current connection adapter with the PerconaAdapter and patches LHM, then it continues with the regular migration process.
-
#include_foreigner ⇒ Object
Includes the Foreigner’s Mysql2Adapter implemention in DepartureAdapter to support foreign keys.
-
#migrate(direction) ⇒ Object
Migrate with or without Departure based on uses_departure class attribute.
-
#reconnect_with_percona ⇒ Object
Make all connections in the connection pool to use PerconaAdapter instead of the current adapter.
-
#reconnect_without_percona ⇒ Object
Reconnect without percona adapter when Departure is disabled but was enabled in a previous migration.
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.
47 48 49 50 51 52 53 |
# File 'lib/departure/migration.rb', line 47 def departure_migrate(direction) reconnect_with_percona include_foreigner if defined?(Foreigner) ::Lhm.migration = self active_record_migrate(direction) end |
#include_foreigner ⇒ Object
Includes the Foreigner’s Mysql2Adapter implemention in DepartureAdapter to support foreign keys
70 71 72 73 74 75 |
# File 'lib/departure/migration.rb', line 70 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.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/departure/migration.rb', line 57 def migrate(direction) with_restored_connection_specification_name do if uses_departure? departure_migrate(direction) else reconnect_without_percona active_record_migrate(direction) end end end |
#reconnect_with_percona ⇒ Object
Make all connections in the connection pool to use PerconaAdapter instead of the current adapter.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/departure/migration.rb', line 79 def reconnect_with_percona config = connection_config return if departure_adapter_config?(config) departure_adapter = Departure::RailsAdapter.for_current(db_connection_adapter: config[:adapter]) Departure::ConnectionBase.establish_connection( config.merge( adapter: departure_adapter.departure_adapter_name, departure_original_adapter: config[:adapter] ) ) end |
#reconnect_without_percona ⇒ Object
Reconnect without percona adapter when Departure is disabled but was enabled in a previous migration.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/departure/migration.rb', line 94 def reconnect_without_percona config = connection_config return unless departure_adapter_config?(config) Departure::OriginalAdapterConnection.establish_connection( config .except(:departure_original_adapter) .merge(adapter: config[:departure_original_adapter] || original_adapter) ) end |