From 1d10e59e6306419fac5a05273ba770f64f0ce982 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 27 Apr 2023 11:56:45 +0200 Subject: [PATCH] =?UTF-8?q?fix(commune):=20fix=20=E2=80=98undefined?= =?UTF-8?q?=E2=80=99=20departement=20codes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/migrations/normalize_communes_job.rb | 2 +- ...48_normalize_commune_code_departement.rake | 21 +++++++++++++++++++ .../migrations/normalize_communes_job_spec.rb | 17 ++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 lib/tasks/deployment/20230427094648_normalize_commune_code_departement.rake diff --git a/app/jobs/migrations/normalize_communes_job.rb b/app/jobs/migrations/normalize_communes_job.rb index 5fa0cfbef..1ae624d7e 100644 --- a/app/jobs/migrations/normalize_communes_job.rb +++ b/app/jobs/migrations/normalize_communes_job.rb @@ -5,7 +5,7 @@ class Migrations::NormalizeCommunesJob < ApplicationJob value_json = champ.value_json || {} - if !champ.departement? + if !champ.departement? || champ.code_departement == 'undefined' metro_code = champ.external_id[0..1] drom_com_code = champ.external_id[0..2] diff --git a/lib/tasks/deployment/20230427094648_normalize_commune_code_departement.rake b/lib/tasks/deployment/20230427094648_normalize_commune_code_departement.rake new file mode 100644 index 000000000..72a8503c1 --- /dev/null +++ b/lib/tasks/deployment/20230427094648_normalize_commune_code_departement.rake @@ -0,0 +1,21 @@ +namespace :after_party do + desc 'Deployment task: normalize_commune_code_departement' + task normalize_commune_code_departement: :environment do + puts "Running deploy task 'normalize_commune_code_departement'" + + champs = Champs::CommuneChamp.where.not(external_id: nil).where("value_json->>'code_departement' = ?", 'undefined') + progress = ProgressReport.new(champs.count) + + champs.pluck(:id).in_groups_of(10_000, false) do |champ_ids| + Migrations::NormalizeCommunesJob.perform_later(champ_ids) + progress.inc(champ_ids.count) + end + + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end diff --git a/spec/jobs/migrations/normalize_communes_job_spec.rb b/spec/jobs/migrations/normalize_communes_job_spec.rb index cdee14971..b71421de5 100644 --- a/spec/jobs/migrations/normalize_communes_job_spec.rb +++ b/spec/jobs/migrations/normalize_communes_job_spec.rb @@ -1,7 +1,8 @@ describe Migrations::NormalizeCommunesJob, type: :job do - let(:champ) { create(:champ_communes, external_id: code_insee, code_departement: nil) } + let(:champ) { create(:champ_communes, external_id: code_insee, code_departement:) } let(:code_insee) { '97209' } let(:value) { 'Fort-de-France (97200)' } + let(:code_departement) { nil } before { champ.update_column(:value, value) } @@ -29,4 +30,18 @@ describe Migrations::NormalizeCommunesJob, type: :job do expect(champ.reload.code_departement).to eq('2A') end end + + context 'undefined' do + let(:code_insee) { '2A004' } + let(:value) { 'Ajaccio (20000)' } + let(:code_departement) { 'undefined' } + + it 'assign code_departement and code_postal' do + expect(champ.reload.code_postal).to be_nil + expect(champ.reload.code_departement).to eq('undefined') + subject + expect(champ.reload.code_postal).to eq('20000') + expect(champ.reload.code_departement).to eq('2A') + end + end end