forked from soundcloud/lhm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The AtomicSwitcher provides atomic renaming of origin / destination tables and will be used by default if mysql server version is not affected by the famous rename bug (http://bugs.mysql.com/bug.php?id=39675).
- Loading branch information
Showing
10 changed files
with
197 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# 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 using an atomic rename. | ||
class AtomicSwitcher | ||
include Command | ||
include SqlHelper | ||
|
||
attr_reader :connection | ||
|
||
def initialize(migration, connection = nil) | ||
@migration = migration | ||
@connection = connection | ||
@origin = migration.origin | ||
@destination = migration.destination | ||
end | ||
|
||
def statements | ||
atomic_switch | ||
end | ||
|
||
def atomic_switch | ||
[ | ||
"rename table `#{ @origin.name }` to `#{ @migration.archive_name }`, " + | ||
"`#{ @destination.name }` to `#{ @origin.name }`" | ||
] | ||
end | ||
|
||
def validate | ||
unless table?(@origin.name) && table?(@destination.name) | ||
error "`#{ @origin.name }` and `#{ @destination.name }` must exist" | ||
end | ||
end | ||
|
||
private | ||
def execute | ||
sql statements | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias | ||
# Schmidt | ||
|
||
require File.expand_path(File.dirname(__FILE__)) + '/integration_helper' | ||
|
||
require 'lhm/table' | ||
require 'lhm/migration' | ||
require 'lhm/atomic_switcher' | ||
|
||
describe Lhm::AtomicSwitcher do | ||
include IntegrationHelper | ||
|
||
before(:each) { connect_master! } | ||
|
||
describe "switching" do | ||
before(:each) do | ||
@origin = table_create("origin") | ||
@destination = table_create("destination") | ||
@migration = Lhm::Migration.new(@origin, @destination) | ||
end | ||
|
||
it "rename origin to archive" do | ||
switcher = Lhm::AtomicSwitcher.new(@migration, connection) | ||
switcher.run | ||
|
||
slave do | ||
table_exists?(@origin).must_equal true | ||
table_read(@migration.archive_name).columns.keys.must_include "origin" | ||
end | ||
end | ||
|
||
it "rename destination to origin" do | ||
switcher = Lhm::AtomicSwitcher.new(@migration, connection) | ||
switcher.run | ||
|
||
slave do | ||
table_exists?(@destination).must_equal false | ||
table_read(@origin.name).columns.keys.must_include "destination" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias | ||
# Schmidt | ||
|
||
require File.expand_path(File.dirname(__FILE__)) + '/unit_helper' | ||
|
||
require 'lhm/table' | ||
require 'lhm/migration' | ||
require 'lhm/atomic_switcher' | ||
|
||
describe Lhm::AtomicSwitcher do | ||
include UnitHelper | ||
|
||
before(:each) do | ||
@start = Time.now | ||
@origin = Lhm::Table.new("origin") | ||
@destination = Lhm::Table.new("destination") | ||
@migration = Lhm::Migration.new(@origin, @destination, @start) | ||
@switcher = Lhm::AtomicSwitcher.new(@migration, nil) | ||
end | ||
|
||
describe "atomic switch" do | ||
it "should perform a single atomic rename" do | ||
@switcher. | ||
statements. | ||
must_equal([ | ||
"rename table `origin` to `#{ @migration.archive_name }`, " + | ||
"`destination` to `origin`" | ||
]) | ||
end | ||
end | ||
end |
Oops, something went wrong.