Merge pull request #9689 from mfo/US/fix-not-normalized-champs-commune

correctif(Champs::CommuneChamp): il restait des champs commune a normaliser
This commit is contained in:
mfo 2023-11-10 09:55:11 +00:00 committed by GitHub
commit e70c49d47b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 16 deletions

View file

@ -1,27 +1,31 @@
class Migrations::NormalizeCommunesJob < ApplicationJob class Migrations::NormalizeCommunesJob < ApplicationJob
def perform(ids) def perform(ids)
Champs::CommuneChamp.where(id: ids).find_each do |champ| Champs::CommuneChamp.where(id: ids).find_each do |champ|
next if champ.external_id.blank? if champ.external_id.blank?
champ.value = nil
champ.value_json = {}
champ.save!
else
value_json = champ.value_json || {}
value_json = champ.value_json || {} if !champ.departement? || champ.code_departement == 'undefined' || champ.code_departement == '99'
metro_code = champ.external_id[0..1]
drom_com_code = champ.external_id[0..2]
if !champ.departement? || champ.code_departement == 'undefined' || champ.code_departement == '99' if metro_code == '97' || metro_code == '98'
metro_code = champ.external_id[0..1] value_json[:code_departement] = drom_com_code
drom_com_code = champ.external_id[0..2] else
value_json[:code_departement] = metro_code
if metro_code == '97' || metro_code == '98' end
value_json[:code_departement] = drom_com_code
else
value_json[:code_departement] = metro_code
end end
end
if !champ.code_postal? && code_postal_with_fallback(champ).present? if !champ.code_postal? && code_postal_with_fallback(champ).present?
value_json[:code_postal] = code_postal_with_fallback(champ) value_json[:code_postal] = code_postal_with_fallback(champ)
end end
if value_json.present? if value_json.present?
champ.update_column(:value_json, value_json) champ.update_column(:value_json, value_json)
end
end end
end end
end end

View file

@ -0,0 +1,22 @@
namespace :after_party do
desc 'Deployment task: fix_not_normalized_champs_commune'
task fix_not_normalized_champs_commune: :environment do
puts "Running deploy task 'fix_not_normalized_champs_commune'"
champs = Champs::CommuneChamp.where(external_id: "")
.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

@ -0,0 +1,13 @@
describe Migrations::NormalizeCommunesJob, type: :job do
context 'when value is "", external_id is "", and code_departement is "undefined"' do
let(:champ) { create(:champ_communes) }
before { champ.update_columns(external_id: "", value: "", value_json: { code_departement: 'undefined', departement: 'undefined' }) }
subject { described_class.perform_now([champ.id]) }
it 'empty the champs' do
subject
champ.reload
expect(champ.code_postal).to be_nil
expect(champ.code_departement).to be_nil
end
end
end