Skip to content

Commit

Permalink
Merge pull request #11212 from demarches-simplifiees/fix_columns_migr…
Browse files Browse the repository at this point in the history
…ation

Opérateurs: ajoute de la doc sur les taches de mis a jour des id columns
  • Loading branch information
LeSim authored Jan 15, 2025
2 parents 6ca8ddb + 482c435 commit 9843450
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 57 deletions.
8 changes: 6 additions & 2 deletions app/models/concerns/columns_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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)

# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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)

# 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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,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
45 changes: 31 additions & 14 deletions spec/models/concerns/columns_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "rails_helper"

module Maintenance
RSpec.describe MigrateProcedurePresentationAddressAndLinkedColumnsTask do
RSpec.describe T20250115RefreshColumnsIdInProcedurePresentationTask do
def displayed_column_ids
raw = ProcedurePresentation
.connection
Expand Down Expand Up @@ -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

0 comments on commit 9843450

Please sign in to comment.