OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
make sure pending direct uploads are skipped during backup (#9850)
Markus Kahl821a48a537aa
diff --git a/app/models/attachment.rb b/app/models/attachment.rbindex f89506a6d34..2d4ce1dbbaa 100644--- a/app/models/attachment.rb+++ b/app/models/attachment.rb@@ -65,6 +65,9 @@ class Attachment < ApplicationRecordafter_commit :extract_fulltext, on: :create+ scope :pending_direct_upload, -> { where(digest: "", downloads: -1) }+ scope :not_pending_direct_upload, -> { where.not(digest: "", downloads: -1) }+### Returns an URL if the attachment is stored in an external (fog) attachment storage# or nil otherwise.@@ -282,10 +285,6 @@ class Attachment < ApplicationRecordendend- def self.pending_direct_uploads- where(digest: "", downloads: -1)- end-def pending_direct_upload?digest == "" && downloads == -1enddiff --git a/app/models/backup.rb b/app/models/backup.rbindex 7749d9229f0..d6df54c5b96 100644--- a/app/models/backup.rb+++ b/app/models/backup.rb@@ -19,6 +19,7 @@ class Backup < Exportdef attachments_queryAttachment+ .not_pending_direct_upload.where.not(container_type: nil).where.not(container_type: Export.name)enddiff --git a/app/workers/attachments/cleanup_uncontainered_job.rb b/app/workers/attachments/cleanup_uncontainered_job.rbindex 4e382bbcd57..ce32e4e8bda 100644--- a/app/workers/attachments/cleanup_uncontainered_job.rb+++ b/app/workers/attachments/cleanup_uncontainered_job.rb@@ -41,7 +41,7 @@ class Attachments::CleanupUncontaineredJob < ::Cron::CronJob.destroy_allAttachment- .pending_direct_uploads+ .pending_direct_upload.where(too_old).destroy_all # prepared direct uploads that never finishedenddiff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rbindex ca530e13705..b6474889ccf 100644--- a/app/workers/attachments/finish_direct_upload_job.rb+++ b/app/workers/attachments/finish_direct_upload_job.rb@@ -32,7 +32,7 @@ class Attachments::FinishDirectUploadJob < ApplicationJobqueue_with_priority :highdef perform(attachment_id, whitelist: true)- attachment = Attachment.pending_direct_uploads.find_by(id: attachment_id)+ attachment = Attachment.pending_direct_upload.find_by(id: attachment_id)# An attachment is guaranteed to have a file.# But if the attachment is nil the expression attachment&.file will be nil and attachment&.file.local_file# will throw a NoMethodError: undefined method local_file' for nil:NilClass`.diff --git a/lib/api/v3/attachments/attachments_api.rb b/lib/api/v3/attachments/attachments_api.rbindex 507eb96d623..73ef007b8cd 100644--- a/lib/api/v3/attachments/attachments_api.rb+++ b/lib/api/v3/attachments/attachments_api.rb@@ -66,7 +66,7 @@ module APInamespace :uploaded doget do- attachment = Attachment.pending_direct_uploads.where(id: params[:id]).first!+ attachment = Attachment.pending_direct_upload.find(params[:id])raise API::Errors::NotFound unless attachment.file.readable?diff --git a/modules/bim/app/controllers/bim/ifc_models/ifc_models_controller.rb b/modules/bim/app/controllers/bim/ifc_models/ifc_models_controller.rbindex c7aa171c533..507b170f8b4 100644--- a/modules/bim/app/controllers/bim/ifc_models/ifc_models_controller.rb+++ b/modules/bim/app/controllers/bim/ifc_models/ifc_models_controller.rb@@ -75,7 +75,7 @@ module Bimdef direct_upload_finishedid = request.params[:key].scan(/\/file\/(\d+)\//).flatten.first- attachment = Attachment.pending_direct_uploads.where(id: id).first+ attachment = Attachment.pending_direct_upload.where(id: id).firstif attachment.nil? # this should not happenflash[:error] = "Direct upload failed."redirect_to action: :newdiff --git a/spec/factories/attachment_factory.rb b/spec/factories/attachment_factory.rbindex fa68d9befb5..5ce4dbb1ed1 100644--- a/spec/factories/attachment_factory.rb+++ b/spec/factories/attachment_factory.rb@@ -56,5 +56,11 @@ FactoryBot.define dofactory :attached_picture docontent_type { 'image/jpeg' }end++ factory :pending_direct_upload do+ digest { "" }+ downloads { -1 }+ created_at { DateTime.now - 2.weeks }+ endendenddiff --git a/spec/workers/backup_job_spec.rb b/spec/workers/backup_job_spec.rbindex f5c19aaefaa..1bb19fbc101 100644--- a/spec/workers/backup_job_spec.rb+++ b/spec/workers/backup_job_spec.rb@@ -97,9 +97,13 @@ describe BackupJob, type: :model dolet(:db_dump_success) { true }let!(:attachment) { FactoryBot.create :attachment }+ let!(:pending_direct_upload) { FactoryBot.create :pending_direct_upload }let(:stored_backup) { Attachment.where(container_type: "Export").last }let(:backup_files) { Zip::File.open(stored_backup.file.path) { |zip| zip.entries.map(&:name) } }- let(:backed_up_attachment) { "attachment/file/#{attachment.id}/#{attachment.filename}" }++ def backed_up_attachment(attachment)+ "attachment/file/#{attachment.id}/#{attachment.filename}"+ endbefore { perform }@@ -117,11 +121,16 @@ describe BackupJob, type: :model doif opts[:include_attachments] != falseit "includes attachments in the backup" do- expect(backup_files).to include backed_up_attachment+ expect(backup_files).to include backed_up_attachment(attachment)+ end++ it "does not include pending direct uploads" do+ expect(backup_files).not_to include backed_up_attachment(pending_direct_upload)endelseit "does not include attachments in the backup" do- expect(backup_files).not_to include backed_up_attachment+ expect(backup_files).not_to include backed_up_attachment(attachment)+ expect(backup_files).not_to include backed_up_attachment(pending_direct_upload)endendend