From 6af07dd721895f149a8ecc7d03c275f30f3d4162 Mon Sep 17 00:00:00 2001 From: mfo Date: Tue, 10 Sep 2024 18:16:16 +0200 Subject: [PATCH] data(fix): backfill invalid procedure presentation filters having a value greater than pg max integer --- .../instructeurs/procedures_controller.rb | 2 -- ...ean_invalid_procedure_presentation_task.rb | 30 ++++++++++++++++++ spec/models/procedure_presentation_spec.rb | 4 +-- ...nvalid_procedure_presentation_task_spec.rb | 31 +++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 app/tasks/maintenance/clean_invalid_procedure_presentation_task.rb create mode 100644 spec/tasks/maintenance/clean_invalid_procedure_presentation_task_spec.rb diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index 0dc416865..f8a332189 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -95,9 +95,7 @@ module Instructeurs @has_export_notification = notify_exports? @last_export = last_export_for(statut) - @filtered_sorted_ids = procedure_presentation.filtered_sorted_ids(dossiers, statut, count: dossiers_count) - page = params[:page].presence || 1 @dossiers_count = @filtered_sorted_ids.size diff --git a/app/tasks/maintenance/clean_invalid_procedure_presentation_task.rb b/app/tasks/maintenance/clean_invalid_procedure_presentation_task.rb new file mode 100644 index 000000000..9da038ac4 --- /dev/null +++ b/app/tasks/maintenance/clean_invalid_procedure_presentation_task.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Maintenance + # PR: 10774 + # why: postgres does not support integer greater than ProcedurePresentation::PG_INTEGER_MAX_VALUE) + # it occures when user copypaste the dossier id twice (like missed copy paste,paste) + # once this huge integer is saved on procedure presentation, page with this filter can't be loaded + # when: run this migration when it appears in your maintenance tasks list, this file fix the data and we added some validations too + class CleanInvalidProcedurePresentationTask < MaintenanceTasks::Task + def collection + ProcedurePresentation.all + end + + def process(element) + element.filters = element.filters.transform_values do |filters_by_status| + filters_by_status.reject do |filter| + filter.is_a?(Hash) && + filter['column'] == 'id' && + (filter['value']&.to_i&. >= ProcedurePresentation::PG_INTEGER_MAX_VALUE) + end + end + element.save + end + + def count + # Optionally, define the number of rows that will be iterated over + # This is used to track the task's progress + end + end +end diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index 581798c2e..7515a8369 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -57,8 +57,8 @@ describe ProcedurePresentation do it { expect(build(:procedure_presentation, filters: { "suivis" => [{ table: "user", column: "email", "value" => "exceedingly long filter value" * 10 }] })).to be_invalid } describe 'check_filters_max_integer' do - it { expect(build(:procedure_presentation, filters: { "suivis" => [{ table: "self", column: "id", "value" => 2147483647.to_s }] })).to be_invalid } - it { expect(build(:procedure_presentation, filters: { "suivis" => [{ table: "self", column: "id", "value" => (2147483647 - 1).to_s }] })).to be_valid } + it { expect(build(:procedure_presentation, filters: { "suivis" => [{ table: "self", column: "id", "value" => ProcedurePresentation::PG_INTEGER_MAX_VALUE.to_s }] })).to be_invalid } + it { expect(build(:procedure_presentation, filters: { "suivis" => [{ table: "self", column: "id", "value" => (ProcedurePresentation::PG_INTEGER_MAX_VALUE - 1).to_s }] })).to be_valid } end end end diff --git a/spec/tasks/maintenance/clean_invalid_procedure_presentation_task_spec.rb b/spec/tasks/maintenance/clean_invalid_procedure_presentation_task_spec.rb new file mode 100644 index 000000000..6b95fe523 --- /dev/null +++ b/spec/tasks/maintenance/clean_invalid_procedure_presentation_task_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe CleanInvalidProcedurePresentationTask do + describe "#process" do + subject(:process) { described_class.process(element) } + let(:procedure) { create(:procedure) } + let(:groupe_instructeur) { create(:groupe_instructeur, procedure:, instructeurs: [build(:instructeur)]) } + let(:assign_to) { create(:assign_to, procedure:, instructeur: groupe_instructeur.instructeurs.first) } + let(:element) { create(:procedure_presentation, procedure:, assign_to:) } + + before { element.update_column(:filters, filters) } + + context 'when filter is valid' do + let(:filters) { { "suivis" => [{ 'table' => "self", 'column' => "id", "value" => (ProcedurePresentation::PG_INTEGER_MAX_VALUE - 1).to_s }] } } + it 'keeps it filters' do + expect { subject }.not_to change { element.reload.filters } + end + end + + context 'when filter is invalid, drop it' do + let(:filters) { { "suivis" => [{ 'table' => "self", 'column' => "id", "value" => (ProcedurePresentation::PG_INTEGER_MAX_VALUE).to_s }] } } + it 'drop invalid filters' do + expect { subject }.to change { element.reload.filters }.to({ "suivis" => [] }) + end + end + end + end +end