Skip to content

Commit

Permalink
🐛 Ajout d'une tache rake pour supprimer les données sociodémographiqu…
Browse files Browse the repository at this point in the history
…e en double
  • Loading branch information
cprodhomme authored and etienneCharignon committed Jan 2, 2023
1 parent aeba340 commit e4d5b4e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/donnee_sociodemographique.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class DonneeSociodemographique < ApplicationRecord
belongs_to :evaluation

validates :evaluation_id, presence: true, uniqueness: true
validates :evaluation_id, uniqueness: true

GENRES = %w[homme femme autre].freeze
enum :genre, GENRES.zip(GENRES).to_h
Expand Down
38 changes: 38 additions & 0 deletions lib/tasks/donnees_socio_demographique.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'rake_logger'

namespace :donnees_socio_demographique do
desc 'Supprime les données socio démographique en double'
task supprimer_doublons: :environment do
logger = RakeLogger.logger

doublons = recupere_doublons

total = doublons.count
count = 0
logger.info "Nombre de doublons : #{total}"
doublons.find_each do |donnee_socio_demographique|
count += 1
logger.info "#{count}/#{total}"

if DonneeSociodemographique
.with_deleted.where(evaluation_id: donnee_socio_demographique.evaluation_id)
.where.not(id: donnee_socio_demographique.id)
.exists?
donnee_socio_demographique.really_destroy!
end
end
logger.info "C'est fini"
end
end

def recupere_doublons
evaluation_ids = DonneeSociodemographique.with_deleted
.select(:evaluation_id)
.group(:evaluation_id)
.having('count(*) > 1')
.select(:evaluation_id)
DonneeSociodemographique.with_deleted
.where(evaluation_id: evaluation_ids)
end

0 comments on commit e4d5b4e

Please sign in to comment.