Class: ActiveRecord::PtOscMigration

Inherits:
Migration
  • Object
show all
Defined in:
lib/active_record/pt_osc_migration.rb

Constant Summary collapse

PERCONA_FLAGS =
{
  'defaults-file' => {
    mutator: :make_path_absolute,
  },
  'recursion-method' => {
    version: '>= 2.1',
  },
  'execute' => {
    default: false,
  },
  'check-alter' => {
    boolean: true,
    default: true,
    mutator: :execute_only,
    version: '>= 2.1',
  },
  'user' => {
    mutator: :get_from_config,
    arguments: {
      key_name: 'username',
    },
    default: nil,
  },
  'password' => {
    mutator: :get_from_config,
    default: nil,
  },
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object

:nodoc:



103
104
105
106
# File 'lib/active_record/pt_osc_migration.rb', line 103

def method_missing(method, *arguments, &block) # :nodoc:
  # Putting this here ensures that pt_osc_migration shows up in the caller trace
  super
end

Class Method Details

.percona_flagsObject



49
50
51
# File 'lib/active_record/pt_osc_migration.rb', line 49

def self.percona_flags
  PERCONA_FLAGS
end

Instance Method Details

#migrate(direction) ⇒ Object

Raises:

  • (ArgumentError)


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_record/pt_osc_migration.rb', line 53

def migrate(direction)
  return unless respond_to?(direction)

  run_mode = percona_config[:run_mode] || 'print'
  raise ArgumentError.new('Invalid run_mode specified in database config') unless run_mode.in? %w(print execute)

  case direction
    when :up   then announce 'migrating'
    when :down then announce 'reverting'
  end

  time   = nil
  ActiveRecord::Base.connection_pool.with_connection do |conn|
    @connection = conn
    if respond_to?(:change)
      if direction == :down
        recorder = CommandRecorder.new(@connection)
        suppress_messages do
          @connection = recorder
          change
        end
        @connection = conn
        time = Benchmark.measure {
          self.revert {
            recorder.inverse.each do |cmd, args|
              send(cmd, *args)
            end
          }
        }
      else
        time = Benchmark.measure { change }
      end
    else
      time = Benchmark.measure { send(direction) }
    end

    case run_mode
      when 'execute' then time += Benchmark.measure { execute_pt_osc }
      when 'print' then print_pt_osc
    end

    @connection = nil
  end

  case direction
    when :up   then announce 'migrated (%.4fs)' % time.real; write
    when :down then announce 'reverted (%.4fs)' % time.real; write
  end
end