data(fix): backfill invalid procedure presentation filters having a value greater than pg max integer

This commit is contained in:
mfo 2024-09-10 18:16:16 +02:00
parent 02bbf26d3f
commit 6af07dd721
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
4 changed files with 63 additions and 4 deletions

View file

@ -95,9 +95,7 @@ module Instructeurs
@has_export_notification = notify_exports? @has_export_notification = notify_exports?
@last_export = last_export_for(statut) @last_export = last_export_for(statut)
@filtered_sorted_ids = procedure_presentation.filtered_sorted_ids(dossiers, statut, count: dossiers_count) @filtered_sorted_ids = procedure_presentation.filtered_sorted_ids(dossiers, statut, count: dossiers_count)
page = params[:page].presence || 1 page = params[:page].presence || 1
@dossiers_count = @filtered_sorted_ids.size @dossiers_count = @filtered_sorted_ids.size

View file

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

View file

@ -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 } 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 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" => ProcedurePresentation::PG_INTEGER_MAX_VALUE.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 - 1).to_s }] })).to be_valid }
end end
end end
end end

View file

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