A platform for community discussion. Free, open, simple.
REFACTOR: Cleanup rake tasks based on feedback
Blake Ericksond26aa6e71e13
diff --git a/app/services/destroy_task.rb b/app/services/destroy_task.rbindex e5ff8e5789c..b76d8f7f579 100644--- a/app/services/destroy_task.rb+++ b/app/services/destroy_task.rb@@ -16,14 +16,13 @@ class DestroyTasktopics = Topic.where(category_id: c.id, pinned_at: nil).where.not(user_id: -1)end@io.puts "There are #{topics.count} topics to delete in #{descriptive_slug} category"- topics.each do |topic|+ topics.find_each do |topic|@io.puts "Deleting #{topic.slug}..."first_post = topic.ordered_posts.firstif first_post.nil?return @io.puts "Topic.ordered_posts.first was nil"end- system_user = User.find(-1)- @io.puts PostDestroyer.new(system_user, first_post).destroy+ @io.puts PostDestroyer.new(Discourse.system_user, first_post).destroyendend@@ -36,11 +35,10 @@ class DestroyTasktopics = Topic.where(category_id: c.id, pinned_at: nil).where.not(user_id: -1)end@io.puts "There are #{topics.count} topics to delete in #{c.slug} category"- topics.each do |topic|+ topics.find_each do |topic|first_post = topic.ordered_posts.firstreturn @io.puts "Topic.ordered_posts.first was nil for topic: #{topic.id}" if first_post.nil?- system_user = User.find(-1)- PostDestroyer.new(system_user, first_post).destroy+ PostDestroyer.new(Discourse.system_user, first_post).destroyendtopics = Topic.where(category_id: c.id, pinned_at: nil)@io.puts "There are #{topics.count} topics that could not be deleted in #{c.slug} category"@@ -54,22 +52,19 @@ class DestroyTaskenddef destroy_private_messages- pms = Topic.where(archetype: "private_message")- current_user = User.find(-1) #system- pms.each do |pm|+ Topic.where(archetype: "private_message").find_each do |pm|@io.puts "Destroying #{pm.slug} pm"first_post = pm.ordered_posts.first- @io.puts PostDestroyer.new(current_user, first_post).destroy+ @io.puts PostDestroyer.new(Discourse.system_user, first_post).destroyendenddef destroy_category(category_id, destroy_system_topics = false)c = Category.find_by_id(category_id)return @io.puts "A category with the id: #{category_id} could not be found" if c.nil?- subcategories = Category.where(parent_category_id: c.id).pluck(:id)+ subcategories = Category.where(parent_category_id: c.id)@io.puts "There are #{subcategories.count} subcategories to delete" if subcategories- subcategories.each do |subcategory_id|- s = Category.find_by_id(subcategory_id)+ subcategories.each do |s|category_topic_destroyer(s, destroy_system_topics)endcategory_topic_destroyer(c, destroy_system_topics)@@ -84,14 +79,9 @@ class DestroyTaskenddef destroy_users- users = User.where(admin: false, id: 1..Float::INFINITY)- @io.puts "There are #{users.count} users to delete"- options = {}- options[:delete_posts] = true- current_user = User.find(-1) #system- users.each do |user|+ User.human_users.where(admin: false).find_each do |user|begin- if UserDestroyer.new(current_user).destroy(user, options)+ if UserDestroyer.new(Discourse.system_user).destroy(user, delete_posts: true)@io.puts "#{user.username} deleted"else@io.puts "#{user.username} not deleted"@@ -121,7 +111,7 @@ class DestroyTaskprivatedef category_topic_destroyer(category, destroy_system_topics = false)- destroy_topics_log = destroy_topics_in_category(category.id, destroy_system_topics)+ destroy_topics_in_category(category.id, destroy_system_topics)@io.puts "Destroying #{category.slug} category"category.destroyenddiff --git a/lib/tasks/categories.rake b/lib/tasks/categories.rakeindex b2c9ab2c9f0..a3d58958929 100644--- a/lib/tasks/categories.rake+++ b/lib/tasks/categories.rake@@ -39,8 +39,13 @@ enddesc "Output a list of categories"task "categories:list" => :environment do- categories = Category.pluck(:id, :slug, :parent_category_id)+ categories = Category.where(parent_category_id: nil).order(:slug).pluck(:id, :slug)+ puts "id category-slug"+ puts "-- -----------------"categories.each do |c|- puts "id: #{c[0]}, slug: #{c[1]}, parent: #{c[2]}"+ puts "#{c[0]} #{c[1]}"+ Category.where(parent_category_id: c[0]).order(:slug).pluck(:id, :slug).each do |s|+ puts " #{s[0]} #{s[1]}"+ endendenddiff --git a/lib/tasks/destroy.rake b/lib/tasks/destroy.rakeindex dd99bf52deb..d65a2039c38 100644--- a/lib/tasks/destroy.rake+++ b/lib/tasks/destroy.rake@@ -4,47 +4,41 @@# content and users from your site.desc "Remove all topics in a category"task "destroy:topics", [:category, :parent_category] => :environment do |t, args|- destroy_task = DestroyTask.newcategory = args[:category]parent_category = args[:parent_category]descriptive_slug = parent_category ? "#{parent_category}/#{category}" : categoryputs "Going to delete all topics in the #{descriptive_slug} category"- destroy_task.destroy_topics(category, parent_category)+ DestroyTask.new.destroy_topics(category, parent_category)enddesc "Remove all topics in all categories"task "destroy:topics_all_categories" => :environment do- destroy_task = DestroyTask.newputs "Going to delete all topics in all categories..."- puts log = destroy_task.destroy_topics_all_categories+ DestroyTask.new.destroy_topics_all_categoriesenddesc "Remove all private messages"task "destroy:private_messages" => :environment do- destroy_task = DestroyTask.newputs "Going to delete all private messages..."- puts log = destroy_task.destroy_private_messages+ DestroyTask.new.destroy_private_messagesenddesc "Destroy all groups"task "destroy:groups" => :environment do- destroy_task = DestroyTask.newputs "Going to delete all non-default groups..."- puts log = destroy_task.destroy_groups+ DestroyTask.new.destroy_groupsenddesc "Destroy all non-admin users"task "destroy:users" => :environment do- destroy_task = DestroyTask.newputs "Going to delete all non-admin users..."- puts log = destroy_task.destroy_users+ DestroyTask.new.destroy_usersenddesc "Destroy site stats"task "destroy:stats" => :environment do- destroy_task = DestroyTask.newputs "Going to delete all site stats..."- destroy_task.destroy_stats+ DestroyTask.new.destroy_statsend# Example: rake destroy:categories[28,29,44,85]