From 01b110bc5feb1b56067675193fb84ceaf3120d2c Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 15 Jan 2025 14:45:37 +0100 Subject: [PATCH 1/3] fix: allow old 'etablissement/naf' id for code_naf column --- app/models/concerns/columns_concern.rb | 8 +++- spec/models/concerns/columns_concern_spec.rb | 45 ++++++++++++++------ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/app/models/concerns/columns_concern.rb b/app/models/concerns/columns_concern.rb index c4beb2e0646..53a2637d35c 100644 --- a/app/models/concerns/columns_concern.rb +++ b/app/models/concerns/columns_concern.rb @@ -14,8 +14,12 @@ def find_column(h_id: nil, label: nil) # TODO: to remove after linked_drop_down column column_id migration if column.nil? && h_id.is_a?(Hash) && h_id[:column_id].present? - h_id[:column_id].gsub!('->', '.') - h_id[:column_id].gsub!('departement_code', 'department_code') + new_column_id = h_id[:column_id] + .gsub('->', '.') + .gsub('departement_code', 'department_code') + .gsub('naf', 'code_naf') + + h_id[:column_id] = new_column_id column = columns.find { _1.h_id == h_id } end diff --git a/spec/models/concerns/columns_concern_spec.rb b/spec/models/concerns/columns_concern_spec.rb index f25a22685e5..1cbe5622379 100644 --- a/spec/models/concerns/columns_concern_spec.rb +++ b/spec/models/concerns/columns_concern_spec.rb @@ -11,9 +11,10 @@ ] end let(:procedure) { create(:procedure, types_de_champ_public:) } + let(:procedure_id) { procedure.id } let(:notifications_column) { procedure.notifications_column } - it do + it 'works' do label = notifications_column.label expect(procedure.find_column(label:)).to eq(notifications_column) @@ -22,25 +23,41 @@ unknwon = 'unknown' expect { procedure.find_column(h_id: unknwon) }.to raise_error(ActiveRecord::RecordNotFound) + end - value_column = procedure.find_column(label: 'linked') + context 'when the column_id is a old linked drop down list id' do + let(:linked_drop_down_column) { procedure.find_column(label: 'linked') } + let(:linked_tdc) { procedure.active_revision.types_de_champ.find { _1.type_champ == 'linked_drop_down_list' } } - procedure_id = procedure.id - linked_tdc = procedure.active_revision.types_de_champ - .find { _1.type_champ == 'linked_drop_down_list' } + it do + column_id = "type_de_champ/#{linked_tdc.stable_id}->value" - column_id = "type_de_champ/#{linked_tdc.stable_id}->value" + h_id = { procedure_id:, column_id: } + expect(procedure.find_column(h_id:)).to eq(linked_drop_down_column) + end + end - h_id = { procedure_id:, column_id: } - expect(procedure.find_column(h_id:)).to eq(value_column) + context 'when the colum_id is an old department column id' do + let(:department_column) { procedure.find_column(label: "address – Département") } + let(:address_tdc) { procedure.active_revision.types_de_champ.find { _1.type_champ == 'address' } } - address_tdc = procedure.active_revision.types_de_champ - .find { _1.type_champ == 'address' } + it do + column_id = "type_de_champ/#{address_tdc.stable_id}-$.departement_code" - adresse_department_column = procedure.find_column(label: "address – Département") - column_id = "type_de_champ/#{address_tdc.stable_id}-$.departement_code" - h_id = { procedure_id:, column_id: } - expect(procedure.find_column(h_id:)).to eq(adresse_department_column) + h_id = { procedure_id:, column_id: } + expect(procedure.find_column(h_id:)).to eq(department_column) + end + end + + context 'when the column_id is an old naf column' do + let(:code_naf_column) { procedure.find_column(label: "Code NAF") } + + it do + column_id = "etablissement/naf" + + h_id = { procedure_id:, column_id: } + expect(procedure.find_column(h_id:)).to eq(code_naf_column) + end end end From dfdd2b4ceb5f580552cc81afa73957876d7cb528 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 15 Jan 2025 15:12:29 +0100 Subject: [PATCH 2/3] chore: rename refresh columns / add doc / add auto run --- ..._export_address_and_linked_columns_task.rb | 20 --------------- ...emplate_address_and_linked_columns_task.rb | 19 -------------- ...50115_refresh_columns_id_in_export_task.rb | 25 +++++++++++++++++++ ...resh_columns_id_in_export_template_task.rb | 24 ++++++++++++++++++ ...umns_id_in_procedure_presentation_task.rb} | 7 +++++- ...id_in_procedure_presentation_task_spec.rb} | 2 +- 6 files changed, 56 insertions(+), 41 deletions(-) delete mode 100644 app/tasks/maintenance/migrate_export_address_and_linked_columns_task.rb delete mode 100644 app/tasks/maintenance/migrate_export_template_address_and_linked_columns_task.rb create mode 100644 app/tasks/maintenance/t20250115_refresh_columns_id_in_export_task.rb create mode 100644 app/tasks/maintenance/t20250115_refresh_columns_id_in_export_template_task.rb rename app/tasks/maintenance/{migrate_procedure_presentation_address_and_linked_columns_task.rb => t20250115_refresh_columns_id_in_procedure_presentation_task.rb} (64%) rename spec/tasks/maintenance/{migrate_procedure_presentation_address_and_linked_columns_task_spec.rb => t20250115_refresh_columns_id_in_procedure_presentation_task_spec.rb} (97%) diff --git a/app/tasks/maintenance/migrate_export_address_and_linked_columns_task.rb b/app/tasks/maintenance/migrate_export_address_and_linked_columns_task.rb deleted file mode 100644 index 2345152d16a..00000000000 --- a/app/tasks/maintenance/migrate_export_address_and_linked_columns_task.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -module Maintenance - class MigrateExportAddressAndLinkedColumnsTask < MaintenanceTasks::Task - include RunnableOnDeployConcern - - def collection - Export.all - end - - def process(export) - # by using the `will_change!` method, we ensure that the column will be saved - # and thus the address and linked columns id will be migrated to the new format - export.filtered_columns_will_change! - export.sorted_column_will_change! - - export.save(validate: false) - end - end -end diff --git a/app/tasks/maintenance/migrate_export_template_address_and_linked_columns_task.rb b/app/tasks/maintenance/migrate_export_template_address_and_linked_columns_task.rb deleted file mode 100644 index 975edfdb6f1..00000000000 --- a/app/tasks/maintenance/migrate_export_template_address_and_linked_columns_task.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Maintenance - class MigrateExportTemplateAddressAndLinkedColumnsTask < MaintenanceTasks::Task - include RunnableOnDeployConcern - - def collection - ExportTemplate.all - end - - def process(export_template) - # by using the `will_change!` method, we ensure that the column will be saved - # and thus the address and linked columns id will be migrated to the new format - export_template.exported_columns_will_change! - - export_template.save(validate: false) - end - end -end diff --git a/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_task.rb b/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_task.rb new file mode 100644 index 00000000000..6c787bd5e73 --- /dev/null +++ b/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_task.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# This task run over all exports to reload the columns and reserialize them using their current id +# In order to be refreshed, the column must be unserialized so the method find_column in ColumnConcern must be adapted to work with the old and new id format +# this task should be use with the task t20250115refreshColumnsIdInExportTask, t20250115refreshColumnsIdInExportTemplateTask, t20250115refreshColumnsIdInProcedurePresentationTask +module Maintenance + class T20250115RefreshColumnsIdInExportTask < MaintenanceTasks::Task + include RunnableOnDeployConcern + + run_on_first_deploy + + def collection + Export.all + end + + def process(export) + # by using the `will_change!` method, we ensure that the column will be saved + # and thus the address and linked columns id will be migrated to the new format + export.filtered_columns_will_change! + export.sorted_column_will_change! + + export.save(validate: false) + end + end +end diff --git a/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_template_task.rb b/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_template_task.rb new file mode 100644 index 00000000000..4529792b447 --- /dev/null +++ b/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_template_task.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# This task run over all export_templates to reload the columns and reserialize them using their current id +# In order to be refreshed, the column must be unserialized so the method find_column in ColumnConcern must be adapted to work with the old and new id format +# this task should be use with the task t20250115refreshColumnsIdInExportTask, t20250115refreshColumnsIdInExportTemplateTask, t20250115refreshColumnsIdInProcedurePresentationTask +module Maintenance + class T20250115RefreshColumnsIdInExportTemplateTask < MaintenanceTasks::Task + include RunnableOnDeployConcern + + run_on_first_deploy + + def collection + ExportTemplate.all + end + + def process(export_template) + # by using the `will_change!` method, we ensure that the column will be saved + # and thus the address and linked columns id will be migrated to the new format + export_template.exported_columns_will_change! + + export_template.save(validate: false) + end + end +end diff --git a/app/tasks/maintenance/migrate_procedure_presentation_address_and_linked_columns_task.rb b/app/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task.rb similarity index 64% rename from app/tasks/maintenance/migrate_procedure_presentation_address_and_linked_columns_task.rb rename to app/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task.rb index 9a544e1907b..cf2ecd18f8b 100644 --- a/app/tasks/maintenance/migrate_procedure_presentation_address_and_linked_columns_task.rb +++ b/app/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task.rb @@ -1,9 +1,14 @@ # frozen_string_literal: true +# This task run over all procedure_presentations to reload the columns and reserialize them using their current id +# In order to be refreshed, the column must be unserialized so the method find_column in ColumnConcern must be adapted to work with the old and new id format +# this task should be use with the task t20250115refreshColumnsIdInExportTask, t20250115refreshColumnsIdInExportTemplateTask, t20250115refreshColumnsIdInProcedurePresentationTask module Maintenance - class MigrateProcedurePresentationAddressAndLinkedColumnsTask < MaintenanceTasks::Task + class T20250115RefreshColumnsIdInProcedurePresentationTask < MaintenanceTasks::Task include RunnableOnDeployConcern + run_on_first_deploy + def collection ProcedurePresentation.all end diff --git a/spec/tasks/maintenance/migrate_procedure_presentation_address_and_linked_columns_task_spec.rb b/spec/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task_spec.rb similarity index 97% rename from spec/tasks/maintenance/migrate_procedure_presentation_address_and_linked_columns_task_spec.rb rename to spec/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task_spec.rb index e1c44f068d6..843becc09d1 100644 --- a/spec/tasks/maintenance/migrate_procedure_presentation_address_and_linked_columns_task_spec.rb +++ b/spec/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" module Maintenance - RSpec.describe MigrateProcedurePresentationAddressAndLinkedColumnsTask do + RSpec.describe T20250115RefreshColumnsIdInProcedurePresentationTask do def displayed_column_ids raw = ProcedurePresentation .connection From 482c4356866e6418c17d65f01feb4c9a9cdd42cd Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 15 Jan 2025 18:04:41 +0100 Subject: [PATCH 3/3] fix: ignore invalid column --- ...50115_refresh_columns_id_in_export_task.rb | 4 +++ ...resh_columns_id_in_export_template_task.rb | 4 +++ ...lumns_id_in_procedure_presentation_task.rb | 4 +++ ..._id_in_procedure_presentation_task_spec.rb | 30 +++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_task.rb b/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_task.rb index 6c787bd5e73..610a0cdc5a2 100644 --- a/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_task.rb +++ b/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_task.rb @@ -20,6 +20,10 @@ def process(export) export.sorted_column_will_change! export.save(validate: false) + + # a column can be not found for various reasons (deleted tdc, changed type, etc) + # in this case we just ignore the error and continue + rescue ActiveRecord::RecordNotFound end end end diff --git a/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_template_task.rb b/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_template_task.rb index 4529792b447..0de5dc4bbc9 100644 --- a/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_template_task.rb +++ b/app/tasks/maintenance/t20250115_refresh_columns_id_in_export_template_task.rb @@ -19,6 +19,10 @@ def process(export_template) export_template.exported_columns_will_change! export_template.save(validate: false) + + # a column can be not found for various reasons (deleted tdc, changed type, etc) + # in this case we just ignore the error and continue + rescue ActiveRecord::RecordNotFound end end end diff --git a/app/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task.rb b/app/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task.rb index cf2ecd18f8b..61c697c1b42 100644 --- a/app/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task.rb +++ b/app/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task.rb @@ -28,6 +28,10 @@ def process(procedure_presentation) procedure_presentation.archives_filters_will_change! procedure_presentation.save(validate: false) + + # a column can be not found for various reasons (deleted tdc, changed type, etc) + # in this case we just ignore the error and continue + rescue ActiveRecord::RecordNotFound end end end diff --git a/spec/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task_spec.rb b/spec/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task_spec.rb index 843becc09d1..1ded1f47574 100644 --- a/spec/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task_spec.rb +++ b/spec/tasks/maintenance/t20250115_refresh_columns_id_in_procedure_presentation_task_spec.rb @@ -96,6 +96,36 @@ def department_column.h_id expect(displayed_column_ids.any? { _1.include?('departement') }).to be(false) end end + + describe "an invalid procedure presentation" do + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :text, libelle: 'text' }]) } + + let(:invalid_column) do + text_column = procedure.columns.filter { _1.label =~ /text/ }.first + + def text_column.h_id + original_h_id = super() + original_h_id[:column_id] = 'INVALID' + original_h_id + end + + text_column + end + + before do + procedure_presentation.update(displayed_columns: [invalid_column]) + + # destroy the columns cache + Current.procedure_columns = nil + end + + it do + # ensure invalid id is present in db + expect(displayed_column_ids.any? { _1.include?('INVALID') }).to be(true) + + expect { process }.not_to raise_error + end + end end end end