Module: PerconaMigrator

Defined in:
lib/percona_migrator.rb,
lib/percona_migrator/dsn.rb,
lib/percona_migrator/errors.rb,
lib/percona_migrator/logger.rb,
lib/percona_migrator/option.rb,
lib/percona_migrator/runner.rb,
lib/percona_migrator/command.rb,
lib/percona_migrator/railtie.rb,
lib/percona_migrator/version.rb,
lib/percona_migrator/null_logger.rb,
lib/percona_migrator/user_options.rb,
lib/percona_migrator/cli_generator.rb,
lib/percona_migrator/configuration.rb,
lib/percona_migrator/alter_argument.rb,
lib/percona_migrator/logger_factory.rb,
lib/percona_migrator/connection_details.rb,
lib/percona_migrator/log_sanitizers/password_sanitizer.rb

Defined Under Namespace

Modules: LogSanitizers, LoggerFactory Classes: AlterArgument, ArgumentsNotSupported, CliGenerator, Command, CommandNotFoundError, Configuration, ConnectionDetails, DSN, Error, InvalidAlterStatement, Logger, NoStatusError, NullLogger, Option, Railtie, Runner, SignalError, UserOptions

Constant Summary collapse

VERSION =
'3.0.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



22
23
24
# File 'lib/percona_migrator.rb', line 22

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



25
26
27
28
# File 'lib/percona_migrator.rb', line 25

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.loadObject

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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