@@ -19,16 +19,20 @@ namespace :signatures do
1919
2020 desc "Deduplicate signatures from petitions and subscriptions from partners"
2121 task deduplicate : :environment do
22- sig_dups = Signature . group ( :petition_id , :email ) . having ( "count(*) > 1" )
23- sig_dups . pluck ( :petition_id , :email ) . each do |petition_id , email |
24- ids = Signature . where ( petition_id : petition_id , email : email ) . order ( :created_at ) . ids
25- Signature . where ( id : ids [ 1 ..-1 ] ) . delete_all
22+ sig_dups = Signature . group ( :email , :petition_id ) . count . map do |data , count |
23+ data if count > 1
24+ end . compact
25+ sig_dups . each do |email , petition_id |
26+ Signature . where ( petition_id : petition_id , email : email )
27+ . order ( :created_at ) . drop ( 1 ) . map ( &:delete )
2628 end
2729
28- sub_dups = Subscription . group ( :partner_id , :email ) . having ( "count(*) > 1" )
29- sub_dups . pluck ( :partner_id , :email ) . each do |partner_id , email |
30+ sub_dups = Subscription . group ( :email , :partner_id ) . count . map do |data , count |
31+ data if count > 1
32+ end . compact
33+ sub_dups . each do |email , partner_id |
3034 Subscription . where ( partner_id : partner_id , email : email )
31- . order ( :created_at ) [ 1 ..- 1 ] . each ( &:delete )
35+ . order ( :created_at ) . drop ( 1 ) . map ( &:delete )
3236 end
3337 end
3438end
0 commit comments