-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
db(referentiel): add a weekly cron to delete orphan referentiels
- Loading branch information
Showing
3 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |