From 4f9eee3b3272b43eb7c22e3ae2c51841b4feae45 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Thu, 23 Apr 2015 12:53:58 +0200 Subject: [PATCH] Script to delete inconsistent vizs #3286 --- lib/tasks/viz_maintenance.rake | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/tasks/viz_maintenance.rake diff --git a/lib/tasks/viz_maintenance.rake b/lib/tasks/viz_maintenance.rake new file mode 100644 index 000000000000..3a0081cccaaf --- /dev/null +++ b/lib/tasks/viz_maintenance.rake @@ -0,0 +1,44 @@ +namespace :cartodb do + namespace :vizs do + + desc "Purges broken visualizations due to bug during deletion." + task :delete_inconsistent, [:username] => :environment do |t, args| + username = args[:username] + raise "You should pass a username param" unless username + user = User[username: username] + collection = CartoDB::Visualization::Collection.new.fetch(user_id: user.id) + + collection.each do |viz| + if is_inconsistent?(viz) + delete_with_confirmation(viz) + end + end + end + + def is_inconsistent?(viz) + (viz.table? && viz.related_tables.empty?) || (viz.derived? && viz.map.nil?) + end + + def delete_with_confirmation(viz) + display_info(viz) + if ok_to_delete? + viz.delete + STDOUT.puts "deleted!" + end + end + + def display_info(viz) + STDOUT.puts "\nviz.name = #{viz.name}" + STDOUT.puts "viz.type = #{viz.type}" + STDOUT.puts "viz.related_tables = #{viz.related_tables.map {|t| t.name}}" + STDOUT.puts "viz.map_id = #{viz.map_id}" + end + + def ok_to_delete? + STDOUT.puts "About to delete. Are you sure? (y/n)" + input = STDIN.gets.strip + return input == 'y' + end + + end +end