Skip to content

Commit

Permalink
db(referentiel): add a weekly cron to delete orphan referentiels
Browse files Browse the repository at this point in the history
  • Loading branch information
E-L-T committed Dec 20, 2024
1 parent 839162c commit d1fcaed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/jobs/cron/purge_orphan_referentiels_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Cron::PurgeOrphanReferentielsJob < Cron::CronJob
self.schedule_expression = "every week at 4:00"

def perform
referentiel_ids = TypeDeChamp.pluck(:referentiel_id).compact.uniq

Referentiel.where.not(id: referentiel_ids)&.find_each(&:destroy)
end
end
7 changes: 7 additions & 0 deletions spec/factories/referentiel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :referentiel do
name { 'referentiel.csv' }
end
end
13 changes: 13 additions & 0 deletions spec/jobs/cron/purge_orphan_referentiels_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

RSpec.describe Cron::PurgeOrphanReferentielsJob, type: :job do
let!(:used_referentiel) { create(:referentiel) }
let!(:orphan_referentiel) { create(:referentiel) }
let!(:type_de_champ) { create(:type_de_champ, referentiel_id: used_referentiel.id) }

subject(:perform_job) { described_class.perform_now }

describe '#perform' do
it { expect { subject }.to change(Referentiel, :count).by(-1) }
end
end

0 comments on commit d1fcaed

Please sign in to comment.