Skip to content

Commit

Permalink
fix(champs.orphaned): add maintenance task to clean orphaned champs w…
Browse files Browse the repository at this point in the history
…here dossier is missing
  • Loading branch information
mfo committed Aug 26, 2024
1 parent 2021767 commit 5af0925
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Maintenance

Check warning on line 3 in app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb#L3

Added line #L3 was not covered by tests
# On our production environment, the fk between `champs.dossier_id` and `dossiers.id` had not been created
# this maintenance task cleans up orphaned champ pointing to deleted dossier
# next step is to validate the recreated fk
# For our own record, use it with PG_STATEMENT_TIMEOUT=300000 ....
class DeleteOrphanedChampsWithMissingDossierTask < MaintenanceTasks::Task
def collection
Champ.select(:id, :type).where.missing(:dossier)
end

Check warning on line 11 in app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb#L8-L11

Added lines #L8 - L11 were not covered by tests

def process(champ)
champ.reload # in case we need more data on this champ
case champ.type
when 'Champs::CarteChamp' then
champ.geo_areas.delete_all
when 'Champs::RepetitionChamp' then
champ.champs.delete_all
when 'Champs::SiretChamp' then
champ.etablissement.delete
end
champ.delete
end

Check warning on line 24 in app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb#L13-L24

Added lines #L13 - L24 were not covered by tests

def count

Check warning on line 26 in app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb#L26

Added line #L26 was not covered by tests
# no count, it otherwise it will timeout
end
end
end

Check warning on line 30 in app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/delete_orphaned_champs_with_missing_dossier_task.rb#L28-L30

Added lines #L28 - L30 were not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "rails_helper"

module Maintenance
RSpec.describe DeleteOrphanedChampsWithMissingDossierTask do
describe "#process" do
let(:procedure) { create(:procedure, types_de_champ_public:) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
let(:champ) { dossier.champs.first }

subject(:process) { described_class.process(champ) }

context 'with other champs' do
let(:types_de_champ_public) { [{ type: :text }] }
it 'delete champ' do
expect { subject }.to change { Champ.exists?(champ.id) }.from(true).to(false)
end
end

context "with carte" do
let(:types_de_champ_public) { [{ type: :carte }] }
it 'deletes champ.geo_areas' do
geo_area_ids = champ.geo_areas.ids
expect { subject }.to change { GeoArea.where(id: geo_area_ids).count }.from(2).to(0)
expect(Champ.exists?(champ.id)).to be_falsey
end
end

context "with repetition" do
let(:types_de_champ_public) { [{ type: :repetition, mandatory: true, children: [{ type: :text }] }] }
it 'deletes champ.champs (children)' do
expect { subject }.to change { champ.champs.count }.from(2).to(0)
expect(Champ.exists?(champ.id)).to be_falsey
end
end

context "with siret" do
let(:types_de_champ_public) { [{ type: :siret }] }
it 'delete champ.etablissement' do
expect { subject }.to change { Etablissement.exists?(champ.etablissement_id) }.from(true).to(false)
expect(Champ.exists?(champ.id)).to be_falsey
end
end
end
end
end

0 comments on commit 5af0925

Please sign in to comment.