-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3342 from CartoDB/3286-fix-undefined-method-scrol…
…lwheel Script to delete inconsistent vizs #3286
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |