Class: Lhm::AtomicSwitcher

Inherits:
Object
  • Object
show all
Includes:
Command
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 Command

#run

Constructor Details

#initialize(migration, connection = nil) ⇒ AtomicSwitcher

Returns a new instance of AtomicSwitcher.



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

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.



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

def connection
  @connection
end

Instance Method Details

#atomic_switchObject



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

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

#statementsObject



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

def statements
  atomic_switch
end

#validateObject



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

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