migration(fix.data): when a repetable revision_types_de_champ points to a non active revision type de champs, relink it to the actuel revision_type_de_champs pointing to the stable id of its parent.type_de_champ.stable_id

This commit is contained in:
Martin 2022-05-09 18:01:02 +02:00
parent c20e400faa
commit 19360e7aac
2 changed files with 28 additions and 0 deletions

View file

@ -85,6 +85,18 @@ class ProcedureRevision < ApplicationRecord
end
end
def rebase_champs_repetable_parent_id
revision_types_de_champ.where.not(parent_id: nil).find_each do |champ_repetable|
if !revision_types_de_champ.exists?(id: champ_repetable.parent_id)
type_de_champ_stable_id = champ_repetable.parent.type_de_champ.stable_id
new_revision_type_de_champs_parent_id = revision_types_de_champ.joins(:type_de_champ).where(type_de_champ: { stable_id: type_de_champ_stable_id }).first&.id
if !new_revision_type_de_champs_parent_id.nil?
champ_repetable.update(parent_id: new_revision_type_de_champs_parent_id)
end
end
end
end
def remove_type_de_champ(stable_id)
type_de_champ = find_type_de_champ_by_stable_id(stable_id)

View file

@ -0,0 +1,16 @@
namespace :after_party do
desc 'Relink revision_types_de_champ repetition to current revision revision_types_de_champ'
task relink_procedure_revision_types_de_champ_with_most_recent_version: :environment do
puts "Running deploy task 'relink_procedure_revision_types_de_champ_with_most_recent_version'"
# Put your task implementation HERE.
Procedure.joins(:revisions).group('procedures.id').having("count(procedure_revisions.id) > 1").in_batches do |procedure|
procedure.revisions.map(&:rebase_champs_repetable_parent_id)
end
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end