Add destroy rake task
Adds several rake tasks to delete users, topics, pm's and site stats so that you can have a fresh site but maintain site settings and category structure.
This commit is contained in:
parent
4fcbe9128e
commit
31ce955487
|
@ -0,0 +1,90 @@
|
|||
## Because these methods are meant to be called from a rake task
|
||||
# we are capturing all log output into a log array to return
|
||||
# to the rake task rather than using `puts` statements.
|
||||
class DestroyTask
|
||||
def self.destroy_topics(category)
|
||||
c = Category.find_by_slug(category)
|
||||
log = []
|
||||
return "A category with the slug: #{category} could not be found" if c.nil?
|
||||
topics = Topic.where(category_id: c.id, pinned_at: nil).where.not(user_id: -1)
|
||||
log << "There are #{topics.count} topics to delete in #{category} category"
|
||||
topics.each do |topic|
|
||||
log << "Deleting #{topic.slug}..."
|
||||
first_post = topic.ordered_posts.first
|
||||
if first_post.nil?
|
||||
return log << "Topic.ordered_posts.first was nil"
|
||||
end
|
||||
system_user = User.find(-1)
|
||||
log << PostDestroyer.new(system_user, first_post).destroy
|
||||
end
|
||||
log
|
||||
end
|
||||
|
||||
def self.destroy_topics_all_categories
|
||||
categories = Category.all
|
||||
log = []
|
||||
categories.each do |c|
|
||||
log << destroy_topics(c.slug)
|
||||
end
|
||||
log
|
||||
end
|
||||
|
||||
def self.destroy_private_messages
|
||||
pms = Topic.where(archetype: "private_message")
|
||||
current_user = User.find(-1) #system
|
||||
log = []
|
||||
pms.each do |pm|
|
||||
log << "Destroying #{pm.slug} pm"
|
||||
first_post = pm.ordered_posts.first
|
||||
log << PostDestroyer.new(current_user, first_post).destroy
|
||||
end
|
||||
log
|
||||
end
|
||||
|
||||
def self.destroy_groups
|
||||
groups = Group.where(automatic: false)
|
||||
log = []
|
||||
groups.each do |group|
|
||||
log << "destroying group: #{group.id}"
|
||||
log << group.destroy
|
||||
end
|
||||
log
|
||||
end
|
||||
|
||||
def self.destroy_users
|
||||
log = []
|
||||
users = User.where(admin: false, id: 1..Float::INFINITY)
|
||||
log << "There are #{users.count} users to delete"
|
||||
options = {}
|
||||
options[:delete_posts] = true
|
||||
current_user = User.find(-1) #system
|
||||
users.each do |user|
|
||||
begin
|
||||
if UserDestroyer.new(current_user).destroy(user, options)
|
||||
log << "#{user.username} deleted"
|
||||
else
|
||||
log << "#{user.username} not deleted"
|
||||
end
|
||||
rescue UserDestroyer::PostsExistError
|
||||
raise Discourse::InvalidAccess.new("User #{user.username} has #{user.post_count} posts, so can't be deleted.")
|
||||
rescue NoMethodError
|
||||
log << "#{user.username} could not be deleted"
|
||||
end
|
||||
end
|
||||
log
|
||||
end
|
||||
|
||||
def self.destroy_stats
|
||||
ApplicationRequest.destroy_all
|
||||
IncomingLink.destroy_all
|
||||
UserVisit.destroy_all
|
||||
UserProfileView.destroy_all
|
||||
user_profiles = UserProfile.all
|
||||
user_profiles.each do |user_profile|
|
||||
user_profile.views = 0
|
||||
user_profile.save!
|
||||
end
|
||||
PostAction.unscoped.destroy_all
|
||||
EmailLog.destroy_all
|
||||
end
|
||||
end
|
|
@ -0,0 +1,39 @@
|
|||
## These tasks are destructive and are for clearing out all the
|
||||
# content and users from your site, but keeping your site settings,
|
||||
# theme, and category structrue.
|
||||
desc "Remove all topics in a category"
|
||||
task "destroy:topics", [:category] => :environment do |t, args|
|
||||
category = args[:category]
|
||||
puts "Going to delete all topics in the #{category} category"
|
||||
puts log = DestroyTask.destroy_topics(category)
|
||||
end
|
||||
|
||||
desc "Remove all topics in all categories"
|
||||
task "destroy:topics_all_categories" => :environment do
|
||||
puts "Going to delete all topics in all categories..."
|
||||
puts log = DestroyTask.destroy_topics_all_categories
|
||||
end
|
||||
|
||||
desc "Remove all private messages"
|
||||
task "destroy:private_messages" => :environment do
|
||||
puts "Going to delete all private messages..."
|
||||
puts log = DestroyTask.destroy_private_messages
|
||||
end
|
||||
|
||||
desc "Destroy all groups"
|
||||
task "destroy:groups" => :environment do
|
||||
puts "Going to delete all non-default groups..."
|
||||
puts log = DestroyTask.destroy_groups
|
||||
end
|
||||
|
||||
desc "Destroy all non-admin users"
|
||||
task "destroy:users" => :environment do
|
||||
puts "Going to delete all non-admin users..."
|
||||
puts log = DestroyTask.destroy_users
|
||||
end
|
||||
|
||||
desc "Destory site stats"
|
||||
task "destroy:stats" => :environment do
|
||||
puts "Going to delete all site stats..."
|
||||
DestroyTask.destroy_stats
|
||||
end
|
|
@ -0,0 +1,74 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe DestroyTask do
|
||||
|
||||
describe 'destroy topics' do
|
||||
let!(:c) { Fabricate(:category) }
|
||||
let!(:t) { Fabricate(:topic, category_id: c.id) }
|
||||
let!(:p) { Fabricate(:post, topic_id: t.id) }
|
||||
let!(:c2) { Fabricate(:category) }
|
||||
let!(:t2) { Fabricate(:topic, category_id: c2.id) }
|
||||
let!(:p2) { Fabricate(:post, topic_id: t2.id) }
|
||||
|
||||
it 'destroys all topics in a category' do
|
||||
before_count = Topic.where(category_id: c.id).count
|
||||
DestroyTask.destroy_topics(c.slug)
|
||||
expect(Topic.where(category_id: c.id).count).to eq before_count - 1
|
||||
end
|
||||
|
||||
it "doesn't destroy system topics" do
|
||||
DestroyTask.destroy_topics(c2.slug)
|
||||
expect(Topic.where(category_id: c2.id).count).to eq 1
|
||||
end
|
||||
|
||||
it 'destroys topics in all categories' do
|
||||
DestroyTask.destroy_topics_all_categories
|
||||
expect(Post.where(topic_id: [t.id, t2.id]).count).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
describe 'private messages' do
|
||||
let!(:pm) { Fabricate(:private_message_post) }
|
||||
let!(:pm2) { Fabricate(:private_message_post) }
|
||||
|
||||
it 'destroys all private messages' do
|
||||
DestroyTask.destroy_private_messages
|
||||
expect(Topic.where(archetype: "private_message").count).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
describe 'groups' do
|
||||
let!(:g) { Fabricate(:group) }
|
||||
let!(:g2) { Fabricate(:group) }
|
||||
|
||||
it 'destroys all groups' do
|
||||
before_count = Group.count
|
||||
DestroyTask.destroy_groups
|
||||
expect(Group.where(automatic: false).count).to eq 0
|
||||
end
|
||||
|
||||
it "doesn't destroy default groups" do
|
||||
before_count = Group.count
|
||||
DestroyTask.destroy_groups
|
||||
expect(Group.count).to eq before_count - 2
|
||||
end
|
||||
end
|
||||
|
||||
describe 'users' do
|
||||
let!(:u) { Fabricate(:user) }
|
||||
let!(:u2) { Fabricate(:user) }
|
||||
let!(:a) { Fabricate(:admin) }
|
||||
|
||||
it 'destroys all non-admin users' do
|
||||
DestroyTask.destroy_users
|
||||
expect(User.where(admin: false).count).to eq 0
|
||||
expect(User.count).to eq 2 #system + 1 other admin
|
||||
end
|
||||
end
|
||||
|
||||
describe 'stats' do
|
||||
it 'destroys all site stats' do
|
||||
DestroyTask.destroy_stats
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue