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

RETRY_SLEEP_TIME =
10
MAX_RETRIES =
600

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


22
23
24
25
26
27
28
29
30
# File 'lib/lhm/atomic_switcher.rb', line 22

def initialize(migration, connection = nil)
  @migration = migration
  @connection = connection
  @origin = migration.origin
  @destination = migration.destination
  @retries = 0
  @max_retries = MAX_RETRIES
  @retry_sleep_time = RETRY_SLEEP_TIME
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection


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

def connection
  @connection
end

#max_retries=(value) ⇒ Object (writeonly)

Sets the attribute max_retries

Parameters:

  • value

    the value to set the attribute max_retries to.


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

def max_retries=(value)
  @max_retries = value
end

#retriesObject (readonly)

Returns the value of attribute retries


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

def retries
  @retries
end

#retry_sleep_time=(value) ⇒ Object (writeonly)

Sets the attribute retry_sleep_time

Parameters:

  • value

    the value to set the attribute retry_sleep_time to.


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

def retry_sleep_time=(value)
  @retry_sleep_time = value
end

Instance Method Details

#atomic_switchObject


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

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

#statementsObject


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

def statements
  atomic_switch
end

#validateObject


43
44
45
46
47
48
# File 'lib/lhm/atomic_switcher.rb', line 43

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