Created
December 21, 2009 07:27
-
-
Save sumskyi/260835 to your computer and use it in GitHub Desktop.
desc 'clean unused IVR tables'
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
namespace :db do | |
desc 'clean unused IVR tables' | |
task :clean_unused_ivr_tables, :suffix1, :suffix2, :needs => :environment do |t, args| | |
def current_version | |
ivr_config_file = File.dirname(__FILE__) + "/../../config/ivr_version.yml" | |
raise 'Missing ivr_version.yml' unless File.exists?(ivr_config_file) | |
ivr_version = YAML.load_file(File.expand_path(ivr_config_file)).to_s | |
raise 'Empty or broken ivr_version.yml' unless ivr_version =~ /\A\d+\Z/ | |
ivr_version | |
end | |
# default value if no args passed | |
args.with_defaults(:suffix1 => current_version) | |
# 1234|5678 if 2 args, else 1234 | |
ivr_versions = [args[:suffix1], args[:suffix2]].compact.join('|') | |
db = ActiveRecord::Base.connection | |
IVR_TABLES = %w( | |
ivr_digraph_versions | |
ivr_entry_node_configurations | |
ivr_error_prompt_segments | |
ivr_error_prompts | |
ivr_menu_options | |
ivr_menu_prompt_segments | |
ivr_node_rules | |
ivr_nodes | |
ivr_opening_prompt_segments | |
ivr_prompt_segments | |
) | |
regexp = %r(\A(#{IVR_TABLES.join '|'})_[0-9]+\Z) | |
tables = db.select_values("SHOW TABLES LIKE 'ivr_%'"). | |
select{ |el| el =~ regexp }. | |
reject{ |el| el =~ /(#{ivr_versions})\Z/ } | |
tables.each do |table| | |
cmd = "DROP TABLE #{table}" | |
puts cmd | |
db.execute cmd | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment