Fix published revisions

This commit is contained in:
Paul Chavard 2021-05-25 15:12:30 +02:00
parent 3fb10fb4b8
commit a0d4c9d54c
2 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,56 @@
namespace :after_party do
desc 'Deployment task: fix_published_revisions'
task fix_published_revisions: :environment do
puts "Running deploy task 'fix_published_revisions'"
procedure_ids = Procedure
.with_discarded
.joins(:revisions)
.group('procedures.id')
.having('count(procedure_id) > 2')
.map(&:id)
# Remove wrongfully created revisions. Every procedure should have only a draft revision
# and a published revision for published procedure
revisions = ProcedureRevision
.joins(:procedure)
.where(procedure_id: procedure_ids)
.where('procedure_revisions.id != procedures.draft_revision_id AND procedure_revisions.id != procedures.published_revision_id')
dossiers = Dossier.with_discarded.joins(:procedure).where(revision_id: revisions)
progress = ProgressReport.new(dossiers.count)
dossiers.find_each do |dossier|
dossier.update_column(:revision_id, dossier.procedure.published_revision_id)
progress.inc
end
progress.finish
types_de_champ = TypeDeChamp.joins(:procedure).where(revision_id: revisions)
progress = ProgressReport.new(types_de_champ.count)
types_de_champ.find_each do |type_de_champ|
type_de_champ.update_column(:revision_id, type_de_champ.procedure.published_revision_id)
progress.inc
end
progress.finish
ProcedureRevisionTypeDeChamp.where(revision_id: revisions).delete_all
revisions.delete_all
# Fill published_at column on all published revisions
published_revisions = ProcedureRevision
.joins(:procedure)
.where(published_at: nil)
.where('procedure_revisions.id = procedures.published_revision_id')
progress = ProgressReport.new(published_revisions.count)
published_revisions.find_each do |revision|
revision.update_column(:published_at, revision.procedure.published_at)
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

View file

@ -625,6 +625,7 @@ describe Procedure do
expect(procedure.draft_revision).not_to be_nil
expect(procedure.revisions.count).to eq(2)
expect(procedure.revisions).to eq([procedure.published_revision, procedure.draft_revision])
expect(procedure.published_revision.published_at).to eq(now)
end
end
@ -649,12 +650,37 @@ describe Procedure do
expect(procedure.canonical_procedure).to eq(canonical_procedure)
expect(procedure.closed_at).to be_nil
expect(procedure.published_at).to eq(now)
expect(procedure.published_revision.published_at).to eq(now)
end
it 'unpublishes parent procedure' do
expect(parent_procedure.unpublished_at).to eq(now)
end
end
context 'when republishing a previously closed procedure' do
let(:procedure) { create(:procedure, :published, administrateurs: [administrateur]) }
before do
procedure.close!
Timecop.freeze(now) do
procedure.publish_or_reopen!(administrateur)
end
end
it 'changes the procedure state to published' do
expect(procedure.closed_at).to be_nil
expect(procedure.published_at).to eq(now)
expect(procedure.published_revision.published_at).not_to eq(now)
end
it "shouldn't create a new revision" do
expect(procedure.published_revision).not_to be_nil
expect(procedure.draft_revision).not_to be_nil
expect(procedure.revisions.count).to eq(2)
expect(procedure.revisions).to eq([procedure.published_revision, procedure.draft_revision])
end
end
end
describe "#unpublish!" do