From eddbe77c09d8e7df7c33dc429311e7bc1c04a94d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 13 Sep 2024 10:20:59 +0200 Subject: [PATCH] remove virtual column attribute from procedure_presentation data --- ...al_column_from_procedure_presentation.rake | 22 +++++++++++++++ ...n_from_procedure_presentation.rake_spec.rb | 27 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 lib/tasks/deployment/20240912151317_clean_virtual_column_from_procedure_presentation.rake create mode 100644 spec/lib/tasks/deployment/20240912151317_clean_virtual_column_from_procedure_presentation.rake_spec.rb diff --git a/lib/tasks/deployment/20240912151317_clean_virtual_column_from_procedure_presentation.rake b/lib/tasks/deployment/20240912151317_clean_virtual_column_from_procedure_presentation.rake new file mode 100644 index 000000000..c1389dfbf --- /dev/null +++ b/lib/tasks/deployment/20240912151317_clean_virtual_column_from_procedure_presentation.rake @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +namespace :after_party do + desc 'Deployment task: clean_virtual_column_from_procedure_presentation' + task clean_virtual_column_from_procedure_presentation: :environment do + ids = ProcedurePresentation.where("jsonb_typeof(displayed_fields) = 'array' AND EXISTS ( select 1 from jsonb_array_elements(displayed_fields) AS element where element ? 'virtual')").ids + + progress = ProgressReport.new(ids.count) + + ProcedurePresentation.where(id: ids).find_each do |procedure_presentation| + procedure_presentation.displayed_fields = procedure_presentation.displayed_fields.map do |field| + field.except('virtual') + end + procedure_presentation.save!(validate: false) + + progress.inc + end + + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end diff --git a/spec/lib/tasks/deployment/20240912151317_clean_virtual_column_from_procedure_presentation.rake_spec.rb b/spec/lib/tasks/deployment/20240912151317_clean_virtual_column_from_procedure_presentation.rake_spec.rb new file mode 100644 index 000000000..803bb7fd4 --- /dev/null +++ b/spec/lib/tasks/deployment/20240912151317_clean_virtual_column_from_procedure_presentation.rake_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +describe '20240912151317_clean_virtual_column_from_procedure_presentation.rake' do + let(:rake_task) { Rake::Task['after_party:clean_virtual_column_from_procedure_presentation'] } + + let(:procedure) { create(:procedure) } + let(:instructeur) { create(:instructeur) } + let(:assign_to) { create(:assign_to, procedure:, instructeur:) } + + let!(:procedure_presentation) do + displayed_fields = [{ label: "test1", table: "user", column: "email", virtual: true }] + + create(:procedure_presentation, assign_to:, displayed_fields:) + end + + before do + rake_task.invoke + + procedure_presentation.reload + end + + after { rake_task.reenable } + + it 'removes the virtual field' do + expect(procedure_presentation.displayed_fields).to eq([{ "column" => "email", "label" => "test1", "table" => "user" }]) + end +end