Skip to content

Commit 5a73ace

Browse files
author
Lars Gierth
committed
Merge pull request soundcloud#29 from bogdan/master
Lhm.cleanup method
2 parents 9382ac8 + f821ce2 commit 5a73ace

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

lib/lhm.rb

+23-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ module Lhm
3434
# @return [Boolean] Returns true if the migration finishes
3535
# @raise [Error] Raises Lhm::Error in case of a error and aborts the migration
3636
def self.change_table(table_name, options = {}, &block)
37-
connection = Connection.new(adapter)
38-
3937
origin = Table.parse(table_name, connection)
4038
invoker = Invoker.new(origin, connection)
4139
block.call(invoker.migrator)
@@ -44,6 +42,23 @@ def self.change_table(table_name, options = {}, &block)
4442
true
4543
end
4644

45+
def self.cleanup(run = false)
46+
lhm_tables = connection.select_values("show tables").select do |name|
47+
name =~ /^lhm(a|n)_/
48+
end
49+
return true if lhm_tables.empty?
50+
if run
51+
lhm_tables.each do |table|
52+
connection.execute("drop table #{table}")
53+
end
54+
true
55+
else
56+
puts "Existing LHM backup tables: #{lhm_tables.join(", ")}."
57+
puts "Run Lhm.cleanup(true) to drop them all."
58+
false
59+
end
60+
end
61+
4762
def self.setup(adapter)
4863
@@adapter = adapter
4964
end
@@ -55,4 +70,10 @@ def self.adapter
5570
ActiveRecord::Base.connection
5671
end
5772
end
73+
74+
protected
75+
def self.connection
76+
Connection.new(adapter)
77+
end
78+
5879
end

lib/lhm/connection.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def sql(statements)
2828

2929
def show_create(table_name)
3030
sql = "show create table `#{ table_name }`"
31-
select_values(sql).last
31+
select_one(sql).values.last
3232
end
3333

3434
def current_database
@@ -51,7 +51,7 @@ def select_one(sql)
5151
end
5252

5353
def select_values(sql)
54-
select_one(sql).values
54+
select_all(sql)
5555
end
5656

5757
def select_value(sql)

spec/integration/cleanup_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2011 - 2013, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
2+
# Schmidt
3+
4+
require File.expand_path(File.dirname(__FILE__)) + '/integration_helper'
5+
6+
require 'lhm'
7+
8+
describe Lhm, "cleanup" do
9+
include IntegrationHelper
10+
before(:each) { connect_master! }
11+
12+
describe "changes" do
13+
before(:each) do
14+
table_create(:users)
15+
Lhm.change_table(:users, :atomic_switch => false) do |t|
16+
t.add_column(:logins, "INT(12) DEFAULT '0'")
17+
t.add_index(:logins)
18+
end
19+
end
20+
21+
it "should show temporary tables" do
22+
output = capture_stdout do
23+
Lhm.cleanup
24+
end
25+
output.must_include("Existing LHM backup tables")
26+
output.must_match(/lhma_[0-9_]*_users/)
27+
end
28+
29+
it "should delete temporary tables" do
30+
Lhm.cleanup(true).must_equal(true)
31+
Lhm.cleanup.must_equal(true)
32+
end
33+
end
34+
end

spec/integration/integration_helper.rb

+17
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def connect!(port)
5050
end
5151

5252
Lhm.setup(adapter)
53+
unless defined?(@@cleaned_up)
54+
Lhm.cleanup(true)
55+
@@cleaned_up = true
56+
end
5357
@connection = Lhm::Connection.new(adapter)
5458
end
5559

@@ -154,4 +158,17 @@ def index?(table_name, key_name, type = :non_unique)
154158
def master_slave_mode?
155159
!!ENV["MASTER_SLAVE"]
156160
end
161+
162+
#
163+
# Misc
164+
#
165+
166+
def capture_stdout
167+
out = StringIO.new
168+
$stdout = out
169+
yield
170+
return out.string
171+
ensure
172+
$stdout = ::STDOUT
173+
end
157174
end

0 commit comments

Comments
 (0)