From ef2ba2b4a162e737f51375824dcd7d49bbe1e378 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 10 Jan 2025 15:26:09 +0100 Subject: [PATCH] backfill address champ value_json --- .../backfill_address_value_json_task.rb | 18 +++++++++++++ .../backfill_address_value_json_task_spec.rb | 27 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 app/tasks/maintenance/backfill_address_value_json_task.rb create mode 100644 spec/tasks/maintenance/backfill_address_value_json_task_spec.rb diff --git a/app/tasks/maintenance/backfill_address_value_json_task.rb b/app/tasks/maintenance/backfill_address_value_json_task.rb new file mode 100644 index 00000000000..89051e595b7 --- /dev/null +++ b/app/tasks/maintenance/backfill_address_value_json_task.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Maintenance + class BackfillAddressValueJSONTask < MaintenanceTasks::Task + include RunnableOnDeployConcern + include StatementsHelpersConcern + + no_collection + + def process + sql = "UPDATE champs SET value_json = data WHERE type = 'Champs::AddressChamp' AND data IS NOT NULL;" + + with_statement_timeout("5min") do + Champ.connection.execute(sql) + end + end + end +end diff --git a/spec/tasks/maintenance/backfill_address_value_json_task_spec.rb b/spec/tasks/maintenance/backfill_address_value_json_task_spec.rb new file mode 100644 index 00000000000..178b416e750 --- /dev/null +++ b/spec/tasks/maintenance/backfill_address_value_json_task_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe BackfillAddressValueJSONTask do + describe "#process" do + subject(:process) { described_class.process } + + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :address, libelle: 'address' }]) } + let(:dossier) { create(:dossier, procedure:) } + let(:address_champ) { dossier.project_champs_public.first } + let(:address_data) { { 'address' => 'address' } } + + before { address_champ.update(data: address_data) } + + it do + expect(address_champ.value_json).to be_nil + + process + address_champ.reload + + expect(address_champ.value_json).to eq(address_data) + end + end + end +end