Commit 7549d17f authored by Michael Kozono's avatar Michael Kozono

Refactor

parent 67b58ffd
...@@ -51,28 +51,25 @@ module Gitlab ...@@ -51,28 +51,25 @@ module Gitlab
].freeze ].freeze
def ensure_tracked! def ensure_tracked!
add_to_uploads unless in_uploads? add_to_uploads_if_needed
delete delete
end end
def in_uploads? def add_to_uploads_if_needed
# Even though we are checking relative paths, path is enough to # Even though we are checking relative paths, path is enough to
# uniquely identify uploads. There is no ambiguity between # uniquely identify uploads. There is no ambiguity between
# FileUploader paths and other Uploader paths because we use the /-/ # FileUploader paths and other Uploader paths because we use the /-/
# separator kind of like an escape character. Project full_path will # separator kind of like an escape character. Project full_path will
# never conflict with an upload path starting with "uploads/-/". # never conflict with an upload path starting with "uploads/-/".
Upload.exists?(path: upload_path) Upload.
end where(path: upload_path).
first_or_create!(
def add_to_uploads uploader: uploader,
Upload.create!( model_type: model_type,
path: upload_path, model_id: model_id,
uploader: uploader, size: file_size
model_type: model_type, )
model_id: model_id,
size: file_size
)
end end
def upload_path def upload_path
......
...@@ -166,7 +166,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do ...@@ -166,7 +166,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do
end end
end end
describe '#add_to_uploads' do describe '#add_to_uploads_if_needed' do
shared_examples_for 'add_to_uploads_non_markdown_files' do shared_examples_for 'add_to_uploads_non_markdown_files' do
let!(:expected_upload_attrs) { model.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum') } let!(:expected_upload_attrs) { model.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum') }
let!(:untracked_file) { described_class.create!(path: expected_upload_attrs['path']) } let!(:untracked_file) { described_class.create!(path: expected_upload_attrs['path']) }
...@@ -177,7 +177,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do ...@@ -177,7 +177,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do
it 'creates an Upload record' do it 'creates an Upload record' do
expect do expect do
untracked_file.add_to_uploads untracked_file.add_to_uploads_if_needed
end.to change { model.reload.uploads.count }.from(0).to(1) end.to change { model.reload.uploads.count }.from(0).to(1)
expect(model.uploads.first.attributes).to include(expected_upload_attrs) expect(model.uploads.first.attributes).to include(expected_upload_attrs)
...@@ -246,7 +246,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do ...@@ -246,7 +246,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do
it 'creates an Upload record' do it 'creates an Upload record' do
expect do expect do
untracked_file.add_to_uploads untracked_file.add_to_uploads_if_needed
end.to change { model.reload.uploads.count }.from(0).to(1) end.to change { model.reload.uploads.count }.from(0).to(1)
expect(model.uploads.first.attributes).to include(@expected_upload_attrs) expect(model.uploads.first.attributes).to include(@expected_upload_attrs)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment