Add migration service and task
This commit is contained in:
parent
9de917592b
commit
bc09b61684
3 changed files with 83 additions and 0 deletions
22
app/jobs/tmp_dossiers_migrate_revisions_job.rb
Normal file
22
app/jobs/tmp_dossiers_migrate_revisions_job.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
class TmpDossiersMigrateRevisionsJob < ApplicationJob
|
||||
def perform(except)
|
||||
dossiers = Dossier.with_discarded.where(revision_id: nil)
|
||||
|
||||
dossiers.where
|
||||
.not(id: except)
|
||||
.includes(procedure: [:draft_revision, :published_revision])
|
||||
.limit(2000)
|
||||
.find_each do |dossier|
|
||||
if dossier.procedure.present?
|
||||
dossier.revision = dossier.procedure.active_revision
|
||||
dossier.save!(validate: false)
|
||||
else
|
||||
except << dossier.id
|
||||
end
|
||||
end
|
||||
|
||||
if dossiers.where.not(id: except).exists?
|
||||
TmpDossiersMigrateRevisionsJob.perform_later(except)
|
||||
end
|
||||
end
|
||||
end
|
39
app/services/revisions_migration.rb
Normal file
39
app/services/revisions_migration.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
class RevisionsMigration
|
||||
def self.add_revisions(procedure)
|
||||
if procedure.draft_revision.present?
|
||||
return false
|
||||
end
|
||||
|
||||
procedure.draft_revision = procedure.revisions.create
|
||||
procedure.save!(validate: false)
|
||||
|
||||
add_types_de_champs_to_revision(procedure, :types_de_champ)
|
||||
add_types_de_champs_to_revision(procedure, :types_de_champ_private)
|
||||
|
||||
if procedure.publiee?
|
||||
published_revision = procedure.draft_revision
|
||||
|
||||
procedure.draft_revision = procedure.create_new_revision
|
||||
procedure.published_revision = published_revision
|
||||
procedure.save!(validate: false)
|
||||
elsif procedure.close? || procedure.depubliee?
|
||||
procedure.draft_revision = procedure.create_new_revision
|
||||
procedure.save!(validate: false)
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def self.add_types_de_champs_to_revision(procedure, types_de_champ_scope)
|
||||
types_de_champ = procedure.send(types_de_champ_scope)
|
||||
types_de_champ.where(revision_id: nil).update_all(revision_id: procedure.draft_revision.id)
|
||||
|
||||
types_de_champ.each.with_index do |type_de_champ, index|
|
||||
type_de_champ.types_de_champ.where(revision_id: nil).update_all(revision_id: procedure.draft_revision.id)
|
||||
procedure.draft_revision.send(:"revision_#{types_de_champ_scope}").create!(
|
||||
type_de_champ: type_de_champ,
|
||||
position: index
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
22
lib/tasks/deployment/20200625113026_migrate_revisions.rake
Normal file
22
lib/tasks/deployment/20200625113026_migrate_revisions.rake
Normal file
|
@ -0,0 +1,22 @@
|
|||
namespace :after_party do
|
||||
desc 'Deployment task: migrate_revisions'
|
||||
task migrate_revisions: :environment do
|
||||
puts "Running deploy task 'migrate_revisions'"
|
||||
|
||||
procedures = Procedure.with_discarded.where(draft_revision_id: nil)
|
||||
progress = ProgressReport.new(procedures.count)
|
||||
|
||||
puts "Processing procedures"
|
||||
procedures.find_each do |procedure|
|
||||
RevisionsMigration.add_revisions(procedure)
|
||||
progress.inc
|
||||
end
|
||||
progress.finish
|
||||
|
||||
TmpDossiersMigrateRevisionsJob.perform_later([])
|
||||
|
||||
# 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: '20200625113026'
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue