soundcloud / lhm

Online MySQL schema migrations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rake task for cleaning up interrupted runs

psi opened this issue · comments

Occasionally I have a large-ish table copied in my dev db and kick off an LHM migration, but I don't want to take the minutes/hours it takes for it to run in my dev environment. In these cases, I generally kill the migration, TRUNCATE the table & kick the migration off again, but sometimes I need to clean up lhmn_* and lhma_* tables created by LHM before I can do so. I wrote this Rake task to do just that. Just thought I'd check to see if there's any interest in including this or something similar either in LHM itself or even just in the README. If there is interest in either, I'm happy to work up a pull request. If not, no sweat. :)

namespace :db do
  desc "Cleanup after botched LHM migrations"
  task :cleanup_lhm => :environment do
    db     = ActiveRecord::Base.connection
    tables = []

    %w(lhmn_ lhma_).each do |prefix|
      tables << db.execute("SHOW TABLES LIKE '#{prefix}_%'").to_a
    end

    tables.flatten!

    if tables.any?
      puts "Dropping #{tables.join(', ')}"

      tables.each do |table|
        db.execute("DROP TABLE #{table}")
      end
    else
      puts "No LHM tables found to clean up!"
    end
  end
end

Hey, thanks for bringing this up! With the latest release (1.3.0) we introduced a method for erasing old copy tables: Lhm.cleanup (#29)

It apparantly needs more visibility, so feel free to mention it in the readme!

Oh, snap! Awesome, thanks!

Closing in favor of #32 :)