Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensure Lhm.cleanup removes temporary triggers

In the event of a failed migration, triggers may not have
been dropped properly. To avoid errors, we must drop
the lhm triggers before the lhm tables.
  • Loading branch information...
commit 46f5bd69a010bc12657d7a0cbcdbc525ff295859 1 parent df63a51
@edmundsalvacion edmundsalvacion authored
View
11 README.md
@@ -182,17 +182,18 @@ during the run will happen on the new table as well.
## Cleaning up after an interrupted Lhm run
If an Lhm migration is interrupted, it may leave behind the temporary tables
-used in the migration. If the migration is re-started, the unexpected presence
-of these tables will cause an error. In this case, `Lhm.cleanup` can be used
-to drop any orphaned Lhm temporary tables.
+and/or triggers used in the migration. If the migration is re-started, the
+unexpected presence of these tables will cause an error.
-To see what Lhm tables are found:
+In this case, `Lhm.cleanup` can be used to drop any orphaned Lhm temporary tables or triggers.
+
+To see what Lhm tables/triggers are found:
```ruby
Lhm.cleanup
```
-To remove any Lhm tables found:
+To remove any Lhm tables/triggers found:
```ruby
Lhm.cleanup(true)
```
View
16 lib/lhm.rb
@@ -49,17 +49,25 @@ def change_table(table_name, options = {}, &block)
end
def cleanup(run = false)
- lhm_tables = connection.select_values("show tables").select do |name|
- name =~ /^lhm(a|n)_/
- end
- return true if lhm_tables.empty?
+ lhm_tables = connection.select_values("show tables").select { |name| name =~ /^lhm(a|n)_/ }
+ lhm_triggers = connection.select_values("show triggers").collect do |trigger|
+ trigger.respond_to?(:trigger) ? trigger.trigger : trigger
+ end.select { |name| name =~ /^lhmt/ }
+
if run
+ lhm_triggers.each do |trigger|
+ connection.execute("drop trigger #{trigger}")
+ end
lhm_tables.each do |table|
connection.execute("drop table #{table}")
end
true
+ elsif lhm_tables.empty? && lhm_triggers.empty?
+ puts "Everything is clean. Nothing to do."
+ true
else
puts "Existing LHM backup tables: #{lhm_tables.join(", ")}."
+ puts "Existing LHM triggers: #{lhm_triggers.join(", ")}."
puts "Run Lhm.cleanup(true) to drop them all."
false
end
View
22 spec/integration/cleanup_spec.rb
@@ -12,12 +12,18 @@
describe "changes" do
before(:each) do
table_create(:users)
- Lhm.change_table(:users, :atomic_switch => false) do |t|
- t.add_column(:logins, "INT(12) DEFAULT '0'")
- t.add_index(:logins)
+ simulate_failed_migration do
+ Lhm.change_table(:users, :atomic_switch => false) do |t|
+ t.add_column(:logins, "INT(12) DEFAULT '0'")
+ t.add_index(:logins)
+ end
end
end
+ after(:each) do
+ Lhm.cleanup(true)
+ end
+
it "should show temporary tables" do
output = capture_stdout do
Lhm.cleanup
@@ -26,6 +32,16 @@
output.must_match(/lhma_[0-9_]*_users/)
end
+ it "should show temporary triggers" do
+ output = capture_stdout do
+ Lhm.cleanup
+ end
+ output.must_include("Existing LHM triggers")
+ output.must_include("lhmt_ins_users")
+ output.must_include("lhmt_del")
+ output.must_include("lhmt_upd_users")
+ end
+
it "should delete temporary tables" do
Lhm.cleanup(true).must_equal(true)
Lhm.cleanup.must_equal(true)
View
19 spec/integration/integration_helper.rb
@@ -162,7 +162,7 @@ def master_slave_mode?
#
# Misc
#
-
+
def capture_stdout
out = StringIO.new
$stdout = out
@@ -171,4 +171,21 @@ def capture_stdout
ensure
$stdout = ::STDOUT
end
+
+ def simulate_failed_migration
+ Lhm::Entangler.class_eval do
+ alias_method :old_after, :after
+ def after
+ true
+ end
+ end
+
+ yield
+ ensure
+ Lhm::Entangler.class_eval do
+ undef_method :after
+ alias_method :after, :old_after
+ undef_method :old_after
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.