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:
commit
e70c49d47b
3 changed files with 55 additions and 16 deletions
|
@ -1,8 +1,11 @@
|
||||||
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'
|
if !champ.departement? || champ.code_departement == 'undefined' || champ.code_departement == '99'
|
||||||
|
@ -25,6 +28,7 @@ class Migrations::NormalizeCommunesJob < ApplicationJob
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
|
@ -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
|
13
spec/jobs/migrations/normalize_communes_job_spec.rb
Normal file
13
spec/jobs/migrations/normalize_communes_job_spec.rb
Normal 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
|
Loading…
Reference in a new issue