Skip to content

Commit bfa814e

Browse files
authored
Merge pull request #506 from codeforjapan/destroy-all
`delete:destroy_all`の修正
2 parents 965ddad + 99a0b19 commit bfa814e

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

app/commands/decidim/comments/destroy_all_comments.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@ def initialize(organization)
1717
#
1818
# Returns nothing.
1919
def call
20-
Decidim::Comments::Comment.find_each do |comment|
21-
if comment.organization == organization
22-
puts "destroy comment id: #{comment.id}, for #{comment.decidim_root_commentable_type}:#{comment.decidim_root_commentable_id}"
23-
comment.destroy!
20+
begin
21+
deletable_ids = []
22+
Decidim::Comments::Comment.find_each(batch_size: 100) do |comment|
23+
if comment&.organization == organization # rubocop:disable Style/IfUnlessModifier
24+
deletable_ids << comment.id
25+
end
26+
rescue Module::DelegationError
27+
# If commentable of comment is nil, the comment should be removed
28+
deletable_ids << comment.id
2429
end
30+
31+
deletable_ids.reverse.each_slice(30) do |ids|
32+
Decidim::Comments::Comment.where(id: ids).order(id: :desc).each do |comment|
33+
puts "destroy comment id: #{comment.id}, for #{comment.decidim_root_commentable_type}:#{comment.decidim_root_commentable_id}"
34+
# force to delete (ignore validation)
35+
Decidim::Comments::CommentVote.where(comment: comment).delete_all
36+
comment.delete
37+
end
38+
end
39+
rescue Exception => e # rubocop:disable Lint/RescueException
40+
pp "error?: #{e.inspect}"
2541
end
2642

2743
broadcast(:ok)

app/commands/decidim/organizations/destroy_organization.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@ def call
2424
end
2525
end
2626

27+
Decidim::Verifications::Conflict.find_each do |conflict|
28+
if conflict.current_user.organization == organization || conflict.managed_user.organization == organization
29+
puts "destroy verifications_conflict id: #{conflict.id}"
30+
conflict.destroy!
31+
end
32+
end
33+
34+
Decidim::DecidimAwesome::EditorImage.where(organization: organization).delete_all
35+
2736
Decidim::ActionLog.where(organization: organization).delete_all
2837

38+
Decidim::AssembliesSetting.where(organization: organization).delete_all
39+
2940
organization.destroy!
3041

3142
broadcast(:ok)

lib/tasks/delete.rake

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ namespace :delete do
1313
:destroy_all_meetings,
1414
:destroy_all_pages,
1515
:destroy_all_surveys,
16+
:destroy_all_users,
1617
:destroy_all_assemblies,
1718
:destroy_all_participatory_processes,
1819
:destroy_all_areas,
19-
:destroy_all_users,
20+
:destroy_all_newsletters,
2021
:destroy_organization,
2122
:destroy_all_messages
2223
]
@@ -203,23 +204,43 @@ namespace :delete do
203204
puts "Finish destroy_all_areas of #{ENV["DECIDIM_ORGANIZATION_NAME"]}"
204205
end
205206

207+
desc "Destroy all newsletters for a given organization"
208+
task destroy_all_newsletters: :environment do
209+
puts "Start destroy_all_newsletters of #{ENV["DECIDIM_ORGANIZATION_NAME"]}"
210+
211+
organization = decidim_find_organization
212+
return unless organization
213+
214+
Decidim::Newsletter.transaction do
215+
Decidim::Newsletter.where(organization: organization).destroy_all
216+
end
217+
218+
puts "Finish destroy_all_newsletters of #{ENV["DECIDIM_ORGANIZATION_NAME"]}"
219+
end
220+
206221
desc "Destroy all users for a given organization"
207222
task destroy_all_users: :environment do
208223
puts "Start destroy_all_users of #{ENV["DECIDIM_ORGANIZATION_NAME"]}"
209224

210225
organization = decidim_find_organization
211226
return unless organization
212227

228+
form = OpenStruct.new(valid?: true, delete_reason: "Testing")
213229
Decidim::User.transaction do
214-
form = OpenStruct.new(valid?: true, delete_reason: "Testing")
215-
Decidim::User.where(organization: organization).find_each do |user|
230+
Decidim::User.where(organization: organization).find_each(batch_size: 100) do |user|
216231
Decidim::Gamifications::DestroyAllBadges.call(organization, user)
217232
Decidim::Authorization.where(user: user).destroy_all
218-
puts "destroy user id: #{user.id}"
219-
Decidim::DestroyAccount.call(user, form)
220233
end
221234
end
222235

236+
# Use tranzaction in Decidim::DestroyAccount
237+
Decidim::User.where(organization: organization).find_each(batch_size: 100) do |user|
238+
puts "destroy user id: #{user.id}"
239+
Decidim::DestroyAccount.call(user, form)
240+
rescue StandardError => e
241+
puts "Decidim::DestroyAccount failed: #{e.inspect}"
242+
end
243+
223244
puts "Finish destroy_all_users of #{ENV["DECIDIM_ORGANIZATION_NAME"]}"
224245
end
225246

@@ -256,9 +277,11 @@ def decidim_find_organization
256277

257278
unless organization
258279
puts "Organization not found: '#{ENV["DECIDIM_ORGANIZATION_NAME"]}'"
259-
puts "Usage: DECIDIM_ORGANIZATION_NAME=<organization name> rails delete::destroy_all_pages"
280+
puts "Usage: DECIDIM_ORGANIZATION_NAME=<organization name> rails delete::destroy_all"
260281
return
261282
end
262283

284+
puts "Organization found: '#{ENV["DECIDIM_ORGANIZATION_NAME"]}' as '#{organization.id}'"
285+
263286
organization
264287
end

0 commit comments

Comments
 (0)