From bc09b61684d9bf9520f966ac3b38c494b9cf8f59 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 26 Jun 2020 11:38:25 +0200 Subject: [PATCH] Add migration service and task --- .../tmp_dossiers_migrate_revisions_job.rb | 22 +++++++++++ app/services/revisions_migration.rb | 39 +++++++++++++++++++ .../20200625113026_migrate_revisions.rake | 22 +++++++++++ 3 files changed, 83 insertions(+) create mode 100644 app/jobs/tmp_dossiers_migrate_revisions_job.rb create mode 100644 app/services/revisions_migration.rb create mode 100644 lib/tasks/deployment/20200625113026_migrate_revisions.rake diff --git a/app/jobs/tmp_dossiers_migrate_revisions_job.rb b/app/jobs/tmp_dossiers_migrate_revisions_job.rb new file mode 100644 index 000000000..ad9e55dee --- /dev/null +++ b/app/jobs/tmp_dossiers_migrate_revisions_job.rb @@ -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 diff --git a/app/services/revisions_migration.rb b/app/services/revisions_migration.rb new file mode 100644 index 000000000..8a9f5d89c --- /dev/null +++ b/app/services/revisions_migration.rb @@ -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 diff --git a/lib/tasks/deployment/20200625113026_migrate_revisions.rake b/lib/tasks/deployment/20200625113026_migrate_revisions.rake new file mode 100644 index 000000000..ffa7c986b --- /dev/null +++ b/lib/tasks/deployment/20200625113026_migrate_revisions.rake @@ -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