module PerconaMigrator

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/percona_migrator.rb, line 25
def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end
load() click to toggle source

Hooks Percona Migrator into Rails migrations by replacing the configured database adapter

# File lib/percona_migrator.rb, line 32
def self.load
  ActiveRecord::Migrator.instance_eval do
    class << self
      alias_method(:original_migrate, :migrate)
    end

    # Checks whether arguments are being passed through PERCONA_ARGS when running
    # the db:migrate rake task
    #
    # @raise [ArgumentsNotSupported] if PERCONA_ARGS has any value
    def migrate(migrations_paths, target_version = nil, &block)
      raise ArgumentsNotSupported if ENV['PERCONA_ARGS'].present?
      original_migrate(migrations_paths, target_version, &block)
    end
  end

  ActiveRecord::Migration.class_eval do
    alias_method :original_migrate, :migrate

    # 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
    def migrate(direction)
      reconnect_with_percona
      include_foreigner if defined?(Foreigner)

      ::Lhm.migration = self
      original_migrate(direction)
    end

    # Includes the Foreigner's Mysql2Adapter implemention in
    # PerconaMigratorAdapter to support foreign keys
    def include_foreigner
      Foreigner::Adapter.safe_include(
        :PerconaMigratorAdapter,
        Foreigner::ConnectionAdapters::Mysql2Adapter
      )
    end

    # Make all connections in the connection pool to use PerconaAdapter
    # instead of the current adapter.
    def reconnect_with_percona
      connection_config = ActiveRecord::Base
        .connection_config.merge(adapter: 'percona')
      ActiveRecord::Base.establish_connection(connection_config)
    end
  end
end

Public Instance Methods

include_foreigner() click to toggle source

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

# File lib/percona_migrator.rb, line 65
def include_foreigner
  Foreigner::Adapter.safe_include(
    :PerconaMigratorAdapter,
    Foreigner::ConnectionAdapters::Mysql2Adapter
  )
end
migrate(migrations_paths, target_version = nil, &block) click to toggle source

Checks whether arguments are being passed through PERCONA_ARGS when running the db:migrate rake task

@raise [ArgumentsNotSupported] if PERCONA_ARGS has any value

# File lib/percona_migrator.rb, line 42
def migrate(migrations_paths, target_version = nil, &block)
  raise ArgumentsNotSupported if ENV['PERCONA_ARGS'].present?
  original_migrate(migrations_paths, target_version, &block)
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/percona_migrator.rb, line 74
def reconnect_with_percona
  connection_config = ActiveRecord::Base
    .connection_config.merge(adapter: 'percona')
  ActiveRecord::Base.establish_connection(connection_config)
end