Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
2ab3031b
Commit
2ab3031b
authored
Nov 08, 2017
by
Michael Kozono
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor, no change in behavior
parent
0e9efa74
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
53 additions
and
39 deletions
+53
-39
lib/gitlab/background_migration/populate_untracked_uploads.rb
...gitlab/background_migration/populate_untracked_uploads.rb
+4
-6
lib/gitlab/background_migration/prepare_unhashed_uploads.rb
lib/gitlab/background_migration/prepare_unhashed_uploads.rb
+21
-14
spec/lib/gitlab/background_migration/populate_untracked_uploads_spec.rb
...b/background_migration/populate_untracked_uploads_spec.rb
+2
-7
spec/migrations/track_untracked_uploads_spec.rb
spec/migrations/track_untracked_uploads_spec.rb
+14
-12
spec/support/track_untracked_uploads_helpers.rb
spec/support/track_untracked_uploads_helpers.rb
+12
-0
No files found.
lib/gitlab/background_migration/populate_untracked_uploads.rb
View file @
2ab3031b
...
@@ -55,9 +55,7 @@ module Gitlab
...
@@ -55,9 +55,7 @@ module Gitlab
def
ensure_tracked!
def
ensure_tracked!
return
if
persisted?
&&
tracked?
return
if
persisted?
&&
tracked?
unless
in_uploads?
add_to_uploads
unless
in_uploads?
add_to_uploads
end
mark_as_tracked
mark_as_tracked
end
end
...
@@ -82,8 +80,7 @@ module Gitlab
...
@@ -82,8 +80,7 @@ module Gitlab
end
end
def
mark_as_tracked
def
mark_as_tracked
self
.
tracked
=
true
update!
(
tracked:
true
)
self
.
save!
end
end
def
upload_path
def
upload_path
...
@@ -121,7 +118,8 @@ module Gitlab
...
@@ -121,7 +118,8 @@ module Gitlab
# Not including a leading slash
# Not including a leading slash
def
path_relative_to_upload_dir
def
path_relative_to_upload_dir
@path_relative_to_upload_dir
||=
path
.
sub
(
%r{
\A
#{
Gitlab
::
BackgroundMigration
::
PrepareUnhashedUploads
::
UPLOAD_DIR
}
/}
,
''
)
base
=
%r{
\A
#{
Regexp
.
escape
(
Gitlab
::
BackgroundMigration
::
PrepareUnhashedUploads
::
UPLOAD_DIR
)
}
/}
@path_relative_to_upload_dir
||=
path
.
sub
(
base
,
''
)
end
end
# Not including a leading slash
# Not including a leading slash
...
...
lib/gitlab/background_migration/prepare_unhashed_uploads.rb
View file @
2ab3031b
...
@@ -35,29 +35,36 @@ module Gitlab
...
@@ -35,29 +35,36 @@ module Gitlab
def
store_unhashed_upload_file_paths
def
store_unhashed_upload_file_paths
return
unless
Dir
.
exist?
(
UPLOAD_DIR
)
return
unless
Dir
.
exist?
(
UPLOAD_DIR
)
file_paths
=
[]
each_file_batch
(
UPLOAD_DIR
,
FILE_PATH_BATCH_SIZE
)
do
|
file_paths
|
each_file_path
(
UPLOAD_DIR
)
do
|
file_path
|
insert_file_paths
(
file_paths
)
file_paths
<<
file_path
if
file_paths
.
size
>=
FILE_PATH_BATCH_SIZE
insert_file_paths
(
file_paths
)
file_paths
=
[]
end
end
end
insert_file_paths
(
file_paths
)
if
file_paths
.
any?
end
end
def
each_file_
path
(
search_dir
,
&
block
)
def
each_file_
batch
(
search_dir
,
batch_size
,
&
block
)
cmd
=
build_find_command
(
search_dir
)
cmd
=
build_find_command
(
search_dir
)
Open3
.
popen2
(
*
cmd
)
do
|
stdin
,
stdout
,
status_thread
|
Open3
.
popen2
(
*
cmd
)
do
|
stdin
,
stdout
,
status_thread
|
stdout
.
each_line
(
"
\0
"
)
do
|
line
|
yield_paths_in_batches
(
stdout
,
batch_size
,
&
block
)
yield
(
line
.
chomp
(
"
\0
"
))
end
raise
"Find command failed"
unless
status_thread
.
value
.
success?
raise
"Find command failed"
unless
status_thread
.
value
.
success?
end
end
end
end
def
yield_paths_in_batches
(
stdout
,
batch_size
,
&
block
)
paths
=
[]
stdout
.
each_line
(
"
\0
"
)
do
|
line
|
paths
<<
line
.
chomp
(
"
\0
"
)
if
paths
.
size
>=
batch_size
yield
(
paths
)
paths
=
[]
end
end
yield
(
paths
)
end
def
build_find_command
(
search_dir
)
def
build_find_command
(
search_dir
)
hashed_path
=
"
#{
UPLOAD_DIR
}
/@hashed/*"
hashed_path
=
"
#{
UPLOAD_DIR
}
/@hashed/*"
tmp_path
=
"
#{
UPLOAD_DIR
}
/tmp/*"
tmp_path
=
"
#{
UPLOAD_DIR
}
/tmp/*"
...
...
spec/lib/gitlab/background_migration/populate_untracked_uploads_spec.rb
View file @
2ab3031b
...
@@ -114,6 +114,8 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads, :migration, :sid
...
@@ -114,6 +114,8 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads, :migration, :sid
end
end
describe
Gitlab
::
BackgroundMigration
::
PopulateUntrackedUploads
::
UnhashedUploadFile
do
describe
Gitlab
::
BackgroundMigration
::
PopulateUntrackedUploads
::
UnhashedUploadFile
do
include
TrackUntrackedUploadsHelpers
let
(
:upload_class
)
{
Gitlab
::
BackgroundMigration
::
PopulateUntrackedUploads
::
Upload
}
let
(
:upload_class
)
{
Gitlab
::
BackgroundMigration
::
PopulateUntrackedUploads
::
Upload
}
describe
'#ensure_tracked!'
do
describe
'#ensure_tracked!'
do
...
@@ -596,11 +598,4 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UnhashedUploadFi
...
@@ -596,11 +598,4 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UnhashedUploadFi
end
end
end
end
end
end
def
rails_sample_jpg_attrs
{
"size"
=>
35255
,
"checksum"
=>
'f2d1fd9d8d8a3368d468fa067888605d74a66f41c16f55979ceaf2af77375844'
}
end
end
end
spec/migrations/track_untracked_uploads_spec.rb
View file @
2ab3031b
...
@@ -2,6 +2,8 @@ require 'spec_helper'
...
@@ -2,6 +2,8 @@ require 'spec_helper'
require
Rails
.
root
.
join
(
'db'
,
'post_migrate'
,
'20171103140253_track_untracked_uploads'
)
require
Rails
.
root
.
join
(
'db'
,
'post_migrate'
,
'20171103140253_track_untracked_uploads'
)
describe
TrackUntrackedUploads
,
:migration
,
:sidekiq
do
describe
TrackUntrackedUploads
,
:migration
,
:sidekiq
do
include
TrackUntrackedUploadsHelpers
class
UnhashedUploadFile
<
ActiveRecord
::
Base
class
UnhashedUploadFile
<
ActiveRecord
::
Base
self
.
table_name
=
'unhashed_upload_files'
self
.
table_name
=
'unhashed_upload_files'
end
end
...
@@ -36,11 +38,18 @@ describe TrackUntrackedUploads, :migration, :sidekiq do
...
@@ -36,11 +38,18 @@ describe TrackUntrackedUploads, :migration, :sidekiq do
it
'has a path field long enough for really long paths'
do
it
'has a path field long enough for really long paths'
do
migrate!
migrate!
max_length_namespace_path
=
max_length_project_path
=
max_length_filename
=
'a'
*
255
component
=
'a'
*
255
long_path
=
"./uploads
#{
"/
#{
max_length_namespace_path
}
"
*
Namespace
::
NUMBER_OF_ANCESTORS_ALLOWED
}
/
#{
max_length_project_path
}
/
#{
max_length_filename
}
"
unhashed_upload_file
=
UnhashedUploadFile
.
new
(
path:
long_path
)
long_path
=
[
unhashed_upload_file
.
save!
CarrierWave
.
root
,
expect
(
UnhashedUploadFile
.
first
.
path
.
size
).
to
eq
(
5641
)
'uploads'
,
[
component
]
*
Namespace
::
NUMBER_OF_ANCESTORS_ALLOWED
,
# namespaces
component
,
# project
component
# filename
].
flatten
.
join
(
'/'
)
record
=
UnhashedUploadFile
.
create!
(
path:
long_path
)
expect
(
record
.
reload
.
path
.
size
).
to
eq
(
5711
)
end
end
context
'with tracked and untracked uploads'
do
context
'with tracked and untracked uploads'
do
...
@@ -132,11 +141,4 @@ describe TrackUntrackedUploads, :migration, :sidekiq do
...
@@ -132,11 +141,4 @@ describe TrackUntrackedUploads, :migration, :sidekiq do
end
end
end
end
end
end
def
rails_sample_jpg_attrs
{
"size"
=>
35255
,
"checksum"
=>
'f2d1fd9d8d8a3368d468fa067888605d74a66f41c16f55979ceaf2af77375844'
}
end
end
end
spec/support/track_untracked_uploads_helpers.rb
0 → 100644
View file @
2ab3031b
module
TrackUntrackedUploadsHelpers
def
rails_sample_jpg_attrs
@rails_sample_jpg_attrs
||=
{
"size"
=>
File
.
size
(
rails_sample_file_path
),
"checksum"
=>
Digest
::
SHA256
.
file
(
rails_sample_file_path
).
hexdigest
}
end
def
rails_sample_file_path
Rails
.
root
.
join
(
'spec'
,
'fixtures'
,
'rails_sample.jpg'
)
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment