diff --git a/app/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task.rb b/app/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task.rb index da6fe8dd4..7ccf5ce32 100644 --- a/app/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task.rb +++ b/app/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task.rb @@ -7,10 +7,12 @@ module Maintenance DEFAULT_INSTRUCTEUR_EMAIL = ENV.fetch('DEFAULT_INSTRUCTEUR_EMAIL') { CONTACT_EMAIL } def collection - Champs::CommuneChamp.where.not(value: nil) + Champs::CommuneChamp.select(:id, :value, :external_id) end def process(champ) + return if !(champ.value.present? && champ.external_id.blank?) + champ.reload return if !fixable?(champ) response = APIGeoService.commune_by_name_or_postal_code(champ.value) @@ -36,7 +38,7 @@ module Maintenance end def count - # 2.4M champs, count is not an option + # osf, count is not an option end private @@ -61,7 +63,7 @@ module Maintenance end def fixable?(champ) - champ.value.present? && [champ.dossier.en_instruction? || champ.dossier.en_construction?] + champ.dossier.en_instruction? || champ.dossier.en_construction? end def notify(message, champ) = Sentry.capture_message(message, extra: { champ: }) diff --git a/spec/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task_spec.rb b/spec/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task_spec.rb index 8317d9ffe..c93e1aa3e 100644 --- a/spec/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task_spec.rb +++ b/spec/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task_spec.rb @@ -8,7 +8,9 @@ module Maintenance let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :communes }]) } let(:dossier) { create(:dossier, state, :with_populated_champs, procedure:) } let(:champ) { dossier.champs.first } - subject(:process) { described_class.process(champ) } + subject(:process) do + described_class.process(champ) + end context 'when search find one result', vcr: { cassette_name: 'fix-champs-commune-with-one-results' } do let(:state) { [:en_instruction, :en_construction].sample }