Merge pull request #7299 from tchak/fix-migrate-parent
Fix migrate parent
This commit is contained in:
commit
1cdffe9455
4 changed files with 64 additions and 9 deletions
|
@ -732,6 +732,13 @@ class Procedure < ApplicationRecord
|
||||||
|
|
||||||
new_draft.revision_types_de_champ.reload
|
new_draft.revision_types_de_champ.reload
|
||||||
|
|
||||||
|
# Some revisions do not have links to children types de champ
|
||||||
|
new_draft
|
||||||
|
.types_de_champ
|
||||||
|
.filter(&:repetition?)
|
||||||
|
.flat_map(&:types_de_champ)
|
||||||
|
.each(&:migrate_parent!)
|
||||||
|
|
||||||
new_draft
|
new_draft
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -381,13 +381,12 @@ class ProcedureRevision < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def revise_type_de_champ(type_de_champ)
|
def revise_type_de_champ(type_de_champ)
|
||||||
types_de_champ_association = type_de_champ.private? ? :revision_types_de_champ_private : :revision_types_de_champ_public
|
revision_type_de_champ = revision_types_de_champ.find_by!(type_de_champ: type_de_champ)
|
||||||
association = send(types_de_champ_association).find_by!(type_de_champ: type_de_champ)
|
|
||||||
cloned_type_de_champ = type_de_champ.deep_clone(include: [:types_de_champ]) do |original, kopy|
|
cloned_type_de_champ = type_de_champ.deep_clone(include: [:types_de_champ]) do |original, kopy|
|
||||||
PiecesJustificativesService.clone_attachments(original, kopy)
|
PiecesJustificativesService.clone_attachments(original, kopy)
|
||||||
end
|
end
|
||||||
cloned_type_de_champ.revision = self
|
cloned_type_de_champ.revision = self
|
||||||
association.update!(type_de_champ: cloned_type_de_champ)
|
revision_type_de_champ.update!(type_de_champ: cloned_type_de_champ)
|
||||||
cloned_type_de_champ.types_de_champ.each(&:migrate_parent!)
|
cloned_type_de_champ.types_de_champ.each(&:migrate_parent!)
|
||||||
cloned_type_de_champ
|
cloned_type_de_champ
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
# stable_id :bigint
|
# stable_id :bigint
|
||||||
#
|
#
|
||||||
class TypeDeChamp < ApplicationRecord
|
class TypeDeChamp < ApplicationRecord
|
||||||
|
self.ignored_columns = [:migrated_parent]
|
||||||
|
|
||||||
enum type_champs: {
|
enum type_champs: {
|
||||||
text: 'text',
|
text: 'text',
|
||||||
textarea: 'textarea',
|
textarea: 'textarea',
|
||||||
|
@ -394,12 +396,13 @@ class TypeDeChamp < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def migrate_parent!
|
def migrate_parent!
|
||||||
if parent_id.present? && migrated_parent.nil?
|
if parent_id.present? && revision_types_de_champ.empty?
|
||||||
ProcedureRevisionTypeDeChamp.create(parent: parent.revision_type_de_champ,
|
parent.revision_types_de_champ.each do |revision_type_de_champ|
|
||||||
|
ProcedureRevisionTypeDeChamp.create(parent: revision_type_de_champ,
|
||||||
type_de_champ: self,
|
type_de_champ: self,
|
||||||
revision_id: parent.revision_type_de_champ.revision_id,
|
revision_id: revision_type_de_champ.revision_id,
|
||||||
position: order_place)
|
position: order_place)
|
||||||
update_column(:migrated_parent, true)
|
end
|
||||||
end
|
end
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
namespace :after_party do
|
||||||
|
desc 'Deployment task: fix_child_types_de_champ'
|
||||||
|
task fix_child_types_de_champ: :environment do
|
||||||
|
puts "Running deploy task 'fix_child_types_de_champ'"
|
||||||
|
|
||||||
|
children = TypeDeChamp.where.not(parent_id: nil).where.missing(:revision_types_de_champ).includes(parent: :revision_types_de_champ)
|
||||||
|
progress = ProgressReport.new(children.count)
|
||||||
|
|
||||||
|
children.find_each do |type_de_champ|
|
||||||
|
type_de_champ.parent.revision_types_de_champ.each do |revision_type_de_champ|
|
||||||
|
ProcedureRevisionTypeDeChamp.create(parent: revision_type_de_champ,
|
||||||
|
type_de_champ: type_de_champ,
|
||||||
|
revision_id: revision_type_de_champ.revision_id,
|
||||||
|
position: type_de_champ.order_place)
|
||||||
|
end
|
||||||
|
progress.inc
|
||||||
|
end
|
||||||
|
progress.finish
|
||||||
|
|
||||||
|
children = TypeDeChamp.where.not(parent_id: nil).includes(:revision_types_de_champ, parent: :revision_types_de_champ)
|
||||||
|
progress = ProgressReport.new(children.count)
|
||||||
|
|
||||||
|
children.find_each do |type_de_champ|
|
||||||
|
prtdcs = type_de_champ.parent.revision_types_de_champ
|
||||||
|
rtdcs = type_de_champ.revision_types_de_champ
|
||||||
|
|
||||||
|
if prtdcs.size > rtdcs.size
|
||||||
|
missing_rtdcs = prtdcs.filter { |prtdc| !prtdc.revision_id.in?(rtdcs.map(&:revision_id)) }
|
||||||
|
|
||||||
|
missing_rtdcs.each do |revision_type_de_champ|
|
||||||
|
ProcedureRevisionTypeDeChamp.create(parent: revision_type_de_champ,
|
||||||
|
type_de_champ: type_de_champ,
|
||||||
|
revision_id: revision_type_de_champ.revision_id,
|
||||||
|
position: type_de_champ.order_place)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
progress.inc
|
||||||
|
end
|
||||||
|
progress.finish
|
||||||
|
|
||||||
|
# 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
|
Loading…
Reference in a new issue