fix(commune): fix ‘undefined’ departement codes

This commit is contained in:
Paul Chavard 2023-04-27 11:56:45 +02:00
parent b0f4c578a6
commit 1d10e59e63
3 changed files with 38 additions and 2 deletions

View file

@ -5,7 +5,7 @@ class Migrations::NormalizeCommunesJob < ApplicationJob
value_json = champ.value_json || {} value_json = champ.value_json || {}
if !champ.departement? if !champ.departement? || champ.code_departement == 'undefined'
metro_code = champ.external_id[0..1] metro_code = champ.external_id[0..1]
drom_com_code = champ.external_id[0..2] drom_com_code = champ.external_id[0..2]

View file

@ -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

View file

@ -1,7 +1,8 @@
describe Migrations::NormalizeCommunesJob, type: :job do 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(:code_insee) { '97209' }
let(:value) { 'Fort-de-France (97200)' } let(:value) { 'Fort-de-France (97200)' }
let(:code_departement) { nil }
before { champ.update_column(:value, value) } before { champ.update_column(:value, value) }
@ -29,4 +30,18 @@ describe Migrations::NormalizeCommunesJob, type: :job do
expect(champ.reload.code_departement).to eq('2A') expect(champ.reload.code_departement).to eq('2A')
end end
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 end