@@ -13,10 +13,11 @@ namespace :delete do
13
13
:destroy_all_meetings ,
14
14
:destroy_all_pages ,
15
15
:destroy_all_surveys ,
16
+ :destroy_all_users ,
16
17
:destroy_all_assemblies ,
17
18
:destroy_all_participatory_processes ,
18
19
:destroy_all_areas ,
19
- :destroy_all_users ,
20
+ :destroy_all_newsletters ,
20
21
:destroy_organization ,
21
22
:destroy_all_messages
22
23
]
@@ -203,23 +204,43 @@ namespace :delete do
203
204
puts "Finish destroy_all_areas of #{ ENV [ "DECIDIM_ORGANIZATION_NAME" ] } "
204
205
end
205
206
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
+
206
221
desc "Destroy all users for a given organization"
207
222
task destroy_all_users : :environment do
208
223
puts "Start destroy_all_users of #{ ENV [ "DECIDIM_ORGANIZATION_NAME" ] } "
209
224
210
225
organization = decidim_find_organization
211
226
return unless organization
212
227
228
+ form = OpenStruct . new ( valid? : true , delete_reason : "Testing" )
213
229
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 |
216
231
Decidim ::Gamifications ::DestroyAllBadges . call ( organization , user )
217
232
Decidim ::Authorization . where ( user : user ) . destroy_all
218
- puts "destroy user id: #{ user . id } "
219
- Decidim ::DestroyAccount . call ( user , form )
220
233
end
221
234
end
222
235
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
+
223
244
puts "Finish destroy_all_users of #{ ENV [ "DECIDIM_ORGANIZATION_NAME" ] } "
224
245
end
225
246
@@ -256,9 +277,11 @@ def decidim_find_organization
256
277
257
278
unless organization
258
279
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 "
260
281
return
261
282
end
262
283
284
+ puts "Organization found: '#{ ENV [ "DECIDIM_ORGANIZATION_NAME" ] } ' as '#{ organization . id } '"
285
+
263
286
organization
264
287
end
0 commit comments