Remove migration service

This commit is contained in:
Paul Chavard 2020-08-27 19:55:10 +02:00
parent a845922df6
commit ec72fdd164
20 changed files with 109 additions and 176 deletions

View file

@ -66,6 +66,7 @@ module NewAdministrateur
def create
@procedure = Procedure.new(procedure_params.merge(administrateurs: [current_administrateur]))
@procedure.draft_revision = @procedure.revisions.build
if !@procedure.save
flash.now.alert = @procedure.errors.full_messages
@ -73,8 +74,6 @@ module NewAdministrateur
else
flash.notice = 'Démarche enregistrée.'
current_administrateur.instructeur.assign_to_procedure(@procedure)
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(@procedure)
redirect_to champs_admin_procedure_path(@procedure)
end

View file

@ -2,7 +2,6 @@ module NewAdministrateur
class TypesDeChampController < AdministrateurController
before_action :retrieve_procedure, only: [:create, :update, :move, :destroy]
before_action :procedure_locked?, only: [:create, :update, :move, :destroy]
before_action :revisions_migration
def create
type_de_champ = @procedure.draft_revision.add_type_de_champ(type_de_champ_create_params)
@ -16,7 +15,7 @@ module NewAdministrateur
end
def update
type_de_champ = @procedure.draft_revision.find_or_clone_type_de_champ(type_de_champ_stable_id)
type_de_champ = @procedure.draft_revision.find_or_clone_type_de_champ(TypeDeChamp.to_stable_id(params[:id]))
if type_de_champ.update(type_de_champ_update_params)
reset_procedure
@ -27,13 +26,13 @@ module NewAdministrateur
end
def move
@procedure.draft_revision.move_type_de_champ(type_de_champ_stable_id, (params[:position] || params[:order_place]).to_i)
@procedure.draft_revision.move_type_de_champ(TypeDeChamp.to_stable_id(params[:id]), (params[:position] || params[:order_place]).to_i)
head :no_content
end
def destroy
@procedure.draft_revision.remove_type_de_champ(type_de_champ_stable_id)
@procedure.draft_revision.remove_type_de_champ(TypeDeChamp.to_stable_id(params[:id]))
reset_procedure
head :no_content
@ -41,19 +40,11 @@ module NewAdministrateur
private
def type_de_champ_stable_id
TypeDeChamp.find(params[:id]).stable_id
end
def revisions_migration
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(@procedure)
end
def serialize_type_de_champ(type_de_champ)
{
type_de_champ: type_de_champ.as_json(
except: [
:id,
:created_at,
:options,
:order_place,
@ -73,7 +64,7 @@ module NewAdministrateur
:piece_justificative_template_url,
:quartiers_prioritaires
]
)
).merge(id: TypeDeChamp.format_stable_id(type_de_champ.stable_id))
}
end
@ -92,7 +83,7 @@ module NewAdministrateur
:type_champ)
if type_de_champ_params[:parent_id].present?
type_de_champ_params[:parent_id] = TypeDeChamp.find(type_de_champ_params[:parent_id]).stable_id
type_de_champ_params[:parent_id] = TypeDeChamp.to_stable_id(type_de_champ_params[:parent_id])
end
type_de_champ_params

View file

@ -313,7 +313,8 @@ class StatsController < ApplicationController
procedure_id_type_de_champs_count = TypeDeChamp
.where(private: false)
.group(:procedure_id)
.joins(:revision)
.group('procedure_revisions.procedure_id')
.count
groupe_instructeur_id_type_de_champs_count = groupe_instructeurs.reduce({}) do |acc, (gi_id, procedure_id)|

View file

@ -259,8 +259,6 @@ module Users
return redirect_to url_for dossiers_path
end
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(procedure)
dossier = Dossier.new(
revision: procedure.active_revision,
groupe_instructeur: procedure.defaut_groupe_instructeur,

View file

@ -8,8 +8,8 @@ class ProcedureDashboard < Administrate::BaseDashboard
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
types_de_champ: TypesDeChampCollectionField,
types_de_champ_private: TypesDeChampCollectionField,
published_types_de_champ: TypesDeChampCollectionField,
published_types_de_champ_private: TypesDeChampCollectionField,
path: ProcedureLinkField,
dossiers: Field::HasMany,
administrateurs: Field::HasMany,
@ -70,8 +70,8 @@ class ProcedureDashboard < Administrate::BaseDashboard
:whitelisted_at,
:hidden_at,
:closed_at,
:types_de_champ,
:types_de_champ_private,
:published_types_de_champ,
:published_types_de_champ_private,
:for_individual,
:auto_archive_on,
:initiated_mail_template,

View file

@ -210,7 +210,6 @@ export const FIELDS = [
'drop_down_list_value',
'libelle',
'mandatory',
'order_place',
'parcelles_agricoles',
'parent_id',
'piece_justificative_template',

View file

@ -22,7 +22,7 @@ export function moveTypeDeChampOperation(typeDeChamp, index, queue) {
return queue.enqueue({
path: `/${typeDeChamp.id}/move`,
method: 'patch',
payload: { order_place: index }
payload: { position: index }
});
}

View file

@ -37,8 +37,7 @@ export default function typeDeChampsReducer(state, { type, params, done }) {
function addTypeDeChamp(state, typeDeChamps, insertAfter, done) {
const typeDeChamp = {
...state.defaultTypeDeChampAttributes,
order_place: typeDeChamps.length
...state.defaultTypeDeChampAttributes
};
createTypeDeChampOperation(typeDeChamp, state.queue)

View file

@ -1,21 +0,0 @@
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.update_column(:revision_id, dossier.procedure.active_revision.id)
else
except << dossier.id
end
end
if dossiers.where.not(id: except).exists?
TmpDossiersMigrateRevisionsJob.perform_later(except)
end
end
end

View file

@ -47,6 +47,9 @@ class Champ < ApplicationRecord
scope :public_only, -> { where(private: false) }
scope :private_only, -> { where(private: true) }
scope :ordered, -> { includes(:type_de_champ).order(:row, 'types_de_champ.order_place') }
scope :public_ordered, -> { public_only.joins(dossier: { revision: :revision_types_de_champ }).where('procedure_revision_types_de_champ.type_de_champ_id = champs.type_de_champ_id').order(:position) }
scope :private_ordered, -> { private_only.joins(dossier: { revision: :revision_types_de_champ_private }).where('procedure_revision_types_de_champ.type_de_champ_id = champs.type_de_champ_id').order(:position) }
scope :root, -> { where(parent_id: nil) }
before_validation :set_dossier_id, if: :needs_dossier_id?

View file

@ -62,8 +62,8 @@ class Dossier < ApplicationRecord
has_one_attached :justificatif_motivation
has_many :champs, -> { root.public_only.ordered }, inverse_of: :dossier, dependent: :destroy
has_many :champs_private, -> { root.private_only.ordered }, class_name: 'Champ', inverse_of: :dossier, dependent: :destroy
has_many :champs, -> { root.public_ordered }, inverse_of: :dossier, dependent: :destroy
has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: :dossier, dependent: :destroy
has_many :commentaires, inverse_of: :dossier, dependent: :destroy
has_many :invites, dependent: :destroy
has_many :follows, -> { active }, inverse_of: :dossier
@ -76,10 +76,13 @@ class Dossier < ApplicationRecord
has_many :dossier_operation_logs, -> { order(:created_at) }, dependent: :nullify, inverse_of: :dossier
belongs_to :groupe_instructeur, optional: false
has_one :procedure, through: :groupe_instructeur
belongs_to :revision, class_name: 'ProcedureRevision', optional: true
belongs_to :revision, class_name: 'ProcedureRevision', optional: false
belongs_to :user, optional: false
has_one :procedure, through: :revision
has_many :types_de_champ, through: :revision
has_many :types_de_champ_private, through: :revision
accepts_nested_attributes_for :champs
accepts_nested_attributes_for :champs_private
@ -315,7 +318,6 @@ class Dossier < ApplicationRecord
accepts_nested_attributes_for :individual
delegate :siret, :siren, to: :etablissement, allow_nil: true
delegate :types_de_champ, to: :procedure
delegate :france_connect_information, to: :user
before_save :build_default_champs, if: Proc.new { groupe_instructeur_id_was.nil? }
@ -326,7 +328,7 @@ class Dossier < ApplicationRecord
after_create :send_draft_notification_email
validates :user, presence: true
validates :individual, presence: true, if: -> { procedure.for_individual? }
validates :individual, presence: true, if: -> { revision.procedure.for_individual? }
validates :groupe_instructeur, presence: true
def motivation
@ -351,10 +353,10 @@ class Dossier < ApplicationRecord
end
def build_default_champs
procedure.build_champs.each do |champ|
revision.build_champs.each do |champ|
champs << champ
end
procedure.build_champs_private.each do |champ|
revision.build_champs_private.each do |champ|
champs_private << champ
end
end

View file

@ -56,13 +56,16 @@ class Procedure < ApplicationRecord
MAX_DUREE_CONSERVATION = 36
MAX_DUREE_CONSERVATION_EXPORT = 3.hours
has_many :types_de_champ, -> { root.public_only.ordered }, inverse_of: :procedure, dependent: :destroy
has_many :types_de_champ_private, -> { root.private_only.ordered }, class_name: 'TypeDeChamp', inverse_of: :procedure, dependent: :destroy
has_many :revisions, -> { order(:id) }, class_name: 'ProcedureRevision', inverse_of: :procedure, dependent: :destroy
belongs_to :draft_revision, class_name: 'ProcedureRevision', optional: true
belongs_to :draft_revision, class_name: 'ProcedureRevision', optional: false
belongs_to :published_revision, class_name: 'ProcedureRevision', optional: true
has_many :deleted_dossiers, dependent: :destroy
has_many :published_types_de_champ, through: :published_revision, source: :types_de_champ
has_many :published_types_de_champ_private, through: :published_revision, source: :types_de_champ_private
has_many :draft_types_de_champ, through: :draft_revision, source: :types_de_champ
has_many :draft_types_de_champ_private, through: :draft_revision, source: :types_de_champ_private
has_one :module_api_carto, dependent: :destroy
has_one :attestation_template, dependent: :destroy
@ -74,6 +77,14 @@ class Procedure < ApplicationRecord
brouillon? ? draft_revision : published_revision
end
def types_de_champ
brouillon? ? draft_types_de_champ : published_types_de_champ
end
def types_de_champ_private
brouillon? ? draft_types_de_champ_private : published_types_de_champ_private
end
has_many :administrateurs_procedures
has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! }
has_many :groupe_instructeurs, dependent: :destroy
@ -93,9 +104,6 @@ class Procedure < ApplicationRecord
has_one_attached :notice
has_one_attached :deliberation
accepts_nested_attributes_for :types_de_champ, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
scope :brouillons, -> { where(aasm_state: :brouillon) }
scope :publiees, -> { where(aasm_state: :publiee) }
scope :closes, -> { where(aasm_state: [:close, :depubliee]) }
@ -114,9 +122,15 @@ class Procedure < ApplicationRecord
scope :for_api, -> {
includes(
:administrateurs,
:types_de_champ_private,
:types_de_champ,
:module_api_carto
:module_api_carto,
published_revision: [
:types_de_champ_private,
:types_de_champ
],
draft_revision: [
:types_de_champ_private,
:types_de_champ
]
)
}
@ -289,22 +303,13 @@ class Procedure < ApplicationRecord
# to save a dossier created from this method
def new_dossier
Dossier.new(
procedure: self,
revision: active_revision,
champs: build_champs,
champs_private: build_champs_private,
champs: active_revision.build_champs,
champs_private: active_revision.build_champs_private,
groupe_instructeur: defaut_groupe_instructeur
)
end
def build_champs
types_de_champ.map(&:build_champ)
end
def build_champs_private
types_de_champ_private.map(&:build_champ)
end
def path_customized?
!path.match?(/[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}/)
end
@ -318,9 +323,6 @@ class Procedure < ApplicationRecord
end
def clone(admin, from_library)
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(self)
is_different_admin = !admin.owns?(self)
populate_champ_stable_ids
@ -371,19 +373,11 @@ class Procedure < ApplicationRecord
end
procedure.save
procedure.draft_revision.types_de_champ.update_all(revision_id: procedure.draft_revision.id)
procedure.draft_revision.types_de_champ_private.update_all(revision_id: procedure.draft_revision.id)
# FIXUP: needed during transition to revisions
procedure.draft_revision.types_de_champ.each do |type_de_champ|
procedure.types_de_champ << type_de_champ
end
procedure.draft_revision.types_de_champ_private.each do |type_de_champ|
procedure.types_de_champ_private << type_de_champ
end
procedure.draft_types_de_champ.update_all(revision_id: procedure.draft_revision.id)
procedure.draft_types_de_champ_private.update_all(revision_id: procedure.draft_revision.id)
if is_different_admin || from_library
procedure.types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) }
procedure.draft_types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) }
end
procedure
@ -608,23 +602,14 @@ class Procedure < ApplicationRecord
end
def after_publish(canonical_procedure = nil)
# FIXUP: needed during transition to revisions
if RevisionsMigration.add_revisions(self)
update!(published_at: Time.zone.now, canonical_procedure: canonical_procedure)
else
update!(published_at: Time.zone.now, canonical_procedure: canonical_procedure, draft_revision: create_new_revision, published_revision: draft_revision)
end
update!(published_at: Time.zone.now, canonical_procedure: canonical_procedure, draft_revision: create_new_revision, published_revision: draft_revision)
end
def after_close
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(self)
update!(closed_at: Time.zone.now)
end
def after_unpublish
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(self)
update!(unpublished_at: Time.zone.now)
end

View file

@ -9,15 +9,22 @@
#
class ProcedureRevision < ApplicationRecord
self.implicit_order_column = :created_at
belongs_to :procedure, -> { with_discarded }, inverse_of: :revisions
belongs_to :procedure, -> { with_discarded }, inverse_of: :revisions, optional: false
has_many :revision_types_de_champ, -> { public_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
has_many :revision_types_de_champ_private, -> { private_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
has_many :types_de_champ, through: :revision_types_de_champ, source: :type_de_champ
has_many :types_de_champ_private, through: :revision_types_de_champ_private, source: :type_de_champ
def build_champs
types_de_champ.map(&:build_champ)
end
def build_champs_private
types_de_champ_private.map(&:build_champ)
end
def add_type_de_champ(params)
params[:procedure] = procedure
params[:revision] = self
if params[:parent_id]
@ -27,15 +34,9 @@ class ProcedureRevision < ApplicationRecord
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
end.create(params)
elsif params[:private]
types_de_champ_private.tap do |types_de_champ|
# FIXUP: needed during transition to revisions
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
end.create(params)
types_de_champ_private.create(params)
else
types_de_champ.tap do |types_de_champ|
# FIXUP: needed during transition to revisions
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
end.create(params)
types_de_champ.create(params)
end
end
@ -112,8 +113,6 @@ class ProcedureRevision < ApplicationRecord
if types_de_champ.delete_at(old_index)
types_de_champ.insert(new_index, type_de_champ)
.map.with_index do |type_de_champ, index|
# FIXUP: needed during transition to revisions
type_de_champ.update!(order_place: index)
[type_de_champ.id, index]
end
else

View file

@ -26,15 +26,11 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
private
def set_position
self.position ||= if private?
if revision.types_de_champ_private.present?
revision.revision_types_de_champ_private.filter(&:persisted?).last.position + 1
else
0
end
else
if revision.types_de_champ.present?
revision.revision_types_de_champ.filter(&:persisted?).last.position + 1
self.position ||= begin
types_de_champ = (private? ? revision.revision_types_de_champ_private : revision.revision_types_de_champ).filter(&:persisted?)
if types_de_champ.present?
types_de_champ.last.position + 1
else
0
end

View file

@ -49,8 +49,8 @@ class TypeDeChamp < ApplicationRecord
repetition: 'repetition'
}
belongs_to :procedure, optional: false
belongs_to :revision, class_name: 'ProcedureRevision', optional: true
has_one :procedure, through: :revision
belongs_to :parent, class_name: 'TypeDeChamp', optional: true
has_many :types_de_champ, -> { ordered }, foreign_key: :parent_id, class_name: 'TypeDeChamp', inverse_of: :parent, dependent: :destroy
@ -73,7 +73,6 @@ class TypeDeChamp < ApplicationRecord
serialize :options, WithIndifferentAccess
after_initialize :set_dynamic_type
before_validation :setup_procedure
after_create :populate_stable_id
attr_reader :dynamic_type
@ -298,9 +297,34 @@ class TypeDeChamp < ApplicationRecord
.merge(include: { types_de_champ: TYPES_DE_CHAMP_BASE })
def self.as_json_for_editor
includes(piece_justificative_template_attachment: :blob,
types_de_champ: [piece_justificative_template_attachment: :blob])
.as_json(TYPES_DE_CHAMP)
includes(piece_justificative_template_attachment: :blob, types_de_champ: [piece_justificative_template_attachment: :blob]).as_json(TYPES_DE_CHAMP)
end
def read_attribute_for_serialization(name)
if name == 'id'
self.class.format_stable_id(stable_id)
else
super
end
end
# FIXME: We are changing how id is exposed to the editor.
# We used to expose type_de_champ.id as primary key to the editor. With revisions
# we need primary key to be type_de_champ.stable_id because any update can create
# a new version but we do not want editor to know about this.
# This is only needed for a clean migration without downtime. We want to ensure
# that if editor send a simple id because it was loaded before deployment
# we would still do the right thing.
def self.format_stable_id(stable_id)
"stable:#{stable_id}"
end
def self.to_stable_id(id_or_stable_id)
if id_or_stable_id.to_s =~ /^stable:/
id_or_stable_id.to_s.gsub(/^stable:/, '')
else
find(id_or_stable_id).stable_id
end
end
private
@ -311,12 +335,6 @@ class TypeDeChamp < ApplicationRecord
result.blank? ? [] : [''] + result
end
def setup_procedure
types_de_champ.each do |type_de_champ|
type_de_champ.procedure = procedure
end
end
def populate_stable_id
if !stable_id
update_column(:stable_id, id)

View file

@ -67,7 +67,7 @@ class DossierSerializer < ActiveModel::Serializer
end
def types_de_piece_justificative
PiecesJustificativesService.serialize_types_de_champ_as_type_pj(object)
PiecesJustificativesService.serialize_types_de_champ_as_type_pj(object.revision)
end
def email

View file

@ -48,6 +48,6 @@ class ProcedureSerializer < ActiveModel::Serializer
end
def types_de_piece_justificative
PiecesJustificativesService.serialize_types_de_champ_as_type_pj(object)
PiecesJustificativesService.serialize_types_de_champ_as_type_pj(object.active_revision)
end
end

View file

@ -12,8 +12,8 @@ class PiecesJustificativesService
.sum(&:byte_size)
end
def self.serialize_types_de_champ_as_type_pj(procedure)
tdcs = procedure.types_de_champ.filter { |type_champ| type_champ.old_pj.present? }
def self.serialize_types_de_champ_as_type_pj(revision)
tdcs = revision.types_de_champ.filter { |type_champ| type_champ.old_pj.present? }
tdcs.map.with_index do |type_champ, order_place|
description = type_champ.description
if /^(?<original_description>.*?)(?:[\r\n]+)Récupérer le formulaire vierge pour mon dossier : (?<lien_demarche>http.*)$/m =~ description

View file

@ -1,36 +0,0 @@
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.brouillon?
published_revision = procedure.draft_revision
procedure.draft_revision = procedure.create_new_revision
procedure.published_revision = published_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

View file

@ -40,7 +40,7 @@
- if !@procedure.locked?
.card-admin
- if @procedure.types_de_champ.count > 0
- if @procedure.draft_types_de_champ.count > 0
%div
%span.icon.accept
%p.card-admin-status-accept Validé
@ -50,7 +50,7 @@
%p.card-admin-status-todo À faire
%div
%p.card-admin-title
%span.badge.baseline= @procedure.types_de_champ.count
%span.badge.baseline= @procedure.draft_types_de_champ.count
Champs du formulaire
%p.card-admin-subtitle À remplir par les usagers
.card-admin-action
@ -148,7 +148,7 @@
- if !@procedure.locked?
.card-admin
- if @procedure.types_de_champ_private.present?
- if @procedure.draft_types_de_champ_private.present?
%div
%span.icon.accept
%p.card-admin-status-accept Validé