Class: Lhm::AtomicSwitcher

Inherits:
Object
  • Object
show all
Includes:
Command, SqlHelper
Defined in:
lib/lhm/atomic_switcher.rb

Overview

Switches origin with destination table using an atomic rename.

It should only be used if the MySQL server version is not affected by the bin log affecting bug #39675. This can be verified using Lhm::SqlHelper.supports_atomic_switch?.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlHelper

#annotation, #idx_name, #idx_spec, #sql, #table?, #update, #version_string

Methods included from Command

#run

Constructor Details

#initialize(migration, connection = nil) ⇒ AtomicSwitcher

Returns a new instance of AtomicSwitcher.



20
21
22
23
24
25
# File 'lib/lhm/atomic_switcher.rb', line 20

def initialize(migration, connection = nil)
  @migration = migration
  @connection = connection
  @origin = migration.origin
  @destination = migration.destination
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



18
19
20
# File 'lib/lhm/atomic_switcher.rb', line 18

def connection
  @connection
end

Instance Method Details

#atomic_switchObject



31
32
33
34
35
36
# File 'lib/lhm/atomic_switcher.rb', line 31

def atomic_switch
  [
    "rename table `#{ @origin.name }` to `#{ @migration.archive_name }`, " +
    "`#{ @destination.name }` to `#{ @origin.name }`"
  ]
end

#statementsObject



27
28
29
# File 'lib/lhm/atomic_switcher.rb', line 27

def statements
  atomic_switch
end

#validateObject



38
39
40
41
42
# File 'lib/lhm/atomic_switcher.rb', line 38

def validate
  unless table?(@origin.name) && table?(@destination.name)
    error "`#{ @origin.name }` and `#{ @destination.name }` must exist"
  end
end