Skip to content

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

make sure pending direct uploads are skipped during backup (#9850)

Markus Kahl821a48a537aa
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index f89506a6d34..2d4ce1dbbaa 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -65,6 +65,9 @@ class Attachment < ApplicationRecord
after_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 < ApplicationRecord
end
end
- def self.pending_direct_uploads
- where(digest: "", downloads: -1)
- end
-
def pending_direct_upload?
digest == "" && downloads == -1
end
diff --git a/app/models/backup.rb b/app/models/backup.rb
index 7749d9229f0..d6df54c5b96 100644
--- a/app/models/backup.rb
+++ b/app/models/backup.rb
@@ -19,6 +19,7 @@ class Backup < Export
def attachments_query
Attachment
+ .not_pending_direct_upload
.where.not(container_type: nil)
.where.not(container_type: Export.name)
end
diff --git a/app/workers/attachments/cleanup_uncontainered_job.rb b/app/workers/attachments/cleanup_uncontainered_job.rb
index 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_all
Attachment
- .pending_direct_uploads
+ .pending_direct_upload
.where(too_old)
.destroy_all # prepared direct uploads that never finished
end
diff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rb
index 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 < ApplicationJob
queue_with_priority :high
def 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.rb
index 507eb96d623..73ef007b8cd 100644
--- a/lib/api/v3/attachments/attachments_api.rb
+++ b/lib/api/v3/attachments/attachments_api.rb
@@ -66,7 +66,7 @@ module API
namespace :uploaded do
get 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.rb
index 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 Bim
def direct_upload_finished
id = 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).first
if attachment.nil? # this should not happen
flash[:error] = "Direct upload failed."
redirect_to action: :new
diff --git a/spec/factories/attachment_factory.rb b/spec/factories/attachment_factory.rb
index fa68d9befb5..5ce4dbb1ed1 100644
--- a/spec/factories/attachment_factory.rb
+++ b/spec/factories/attachment_factory.rb
@@ -56,5 +56,11 @@ FactoryBot.define do
factory :attached_picture do
content_type { 'image/jpeg' }
end
+
+ factory :pending_direct_upload do
+ digest { "" }
+ downloads { -1 }
+ created_at { DateTime.now - 2.weeks }
+ end
end
end
diff --git a/spec/workers/backup_job_spec.rb b/spec/workers/backup_job_spec.rb
index 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 do
let(: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}"
+ end
before { perform }
@@ -117,11 +121,16 @@ describe BackupJob, type: :model do
if opts[:include_attachments] != false
it "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)
end
else
it "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)
end
end
end