Class: Lhm::LockedSwitcher

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

Overview

Switches origin with destination table nonatomically using a locked write. LockedSwitcher adopts the Facebook strategy, with the following caveat:

"Since alter table causes an implicit commit in innodb, innodb locks get
released after the first alter table. So any transaction that sneaks in
after the first alter table and before the second alter table gets
a 'table not found' error. The second alter table is expected to be very
fast though because copytable is not visible to other transactions and so
there is no need to wait."

Constant Summary collapse

LOG_PREFIX =
"LockedSwitcher"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlHelper

#annotation, #idx_name, #idx_spec, #tagged, #version_string

Methods included from Command

#run

Constructor Details

#initialize(migration, connection = nil) ⇒ LockedSwitcher

Returns a new instance of LockedSwitcher.



27
28
29
30
31
32
# File 'lib/lhm/locked_switcher.rb', line 27

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.



23
24
25
# File 'lib/lhm/locked_switcher.rb', line 23

def connection
  @connection
end

Instance Method Details

#statementsObject



34
35
36
# File 'lib/lhm/locked_switcher.rb', line 34

def statements
  uncommitted { switch }
end

#switchObject



38
39
40
41
42
43
44
45
46
# File 'lib/lhm/locked_switcher.rb', line 38

def switch
  [
    "lock table `#{ @origin.name }` write, `#{ @destination.name }` write",
    "alter table `#{ @origin.name }` rename `#{ @migration.archive_name }`",
    "alter table `#{ @destination.name }` rename `#{ @origin.name }`",
    'commit',
    'unlock tables'
  ]
end

#uncommittedObject



48
49
50
51
52
53
54
55
# File 'lib/lhm/locked_switcher.rb', line 48

def uncommitted
  [
    'set @lhm_auto_commit = @@session.autocommit',
    'set session autocommit = 0',
    yield,
    'set session autocommit = @lhm_auto_commit'
  ].flatten
end

#validateObject



57
58
59
60
61
62
# File 'lib/lhm/locked_switcher.rb', line 57

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