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?.

Constant Summary collapse

LOG_PREFIX =
"AtomicSwitcher"

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.



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

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



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

def atomic_switch
  "RENAME TABLE `#{ @origin.name }` TO `#{ @migration.archive_name }`, " \
  "`#{ @destination.name }` TO `#{ @origin.name }`"
end

#validateObject



33
34
35
36
37
38
# File 'lib/lhm/atomic_switcher.rb', line 33

def validate
  unless @connection.data_source_exists?(@origin.name) &&
    @connection.data_source_exists?(@destination.name)
    error "`#{ @origin.name }` AND `#{ @destination.name }` MUST EXIST"
  end
end