lib/lhm/locked_switcher.rb in lhm-1.0.0.rc2 vs lib/lhm/locked_switcher.rb in lhm-1.0.0.rc3
- old
+ new
@@ -1,31 +1,33 @@
-#
-# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
-# Schmidt
-#
-# Switches origin with destination table with a write lock. Use this as a safe
-# alternative to rename, which can cause slave inconsistencies:
-#
-# http://bugs.mysql.com/bug.php?id=39675
-#
-# 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."
-#
+# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
+# Schmidt
require 'lhm/command'
require 'lhm/migration'
+require 'lhm/sql_helper'
module Lhm
+ # Switches origin with destination table with a write lock. Use this as
+ # a safe alternative to rename, which can cause slave inconsistencies:
+ #
+ # http://bugs.mysql.com/bug.php?id=39675
+ #
+ # 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."
+ #
class LockedSwitcher
include Command
+ include SqlHelper
+ attr_reader :connection
+
def initialize(migration, connection = nil)
@migration = migration
@connection = connection
@origin = migration.origin
@destination = migration.destination
@@ -49,31 +51,25 @@
[
"set @lhm_auto_commit = @@session.autocommit",
"set session autocommit = 0",
yield,
"set session autocommit = @lhm_auto_commit"
-
].flatten
end
- #
- # Command interface
- #
-
def validate
unless table?(@origin.name) && table?(@destination.name)
error "`#{ @origin.name }` and `#{ @destination.name }` must exist"
end
end
+ private
+
def revert
sql "unlock tables"
end
- private
-
def execute
sql statements
end
end
end
-