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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
579d8891
Commit
579d8891
authored
Apr 13, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename projects in a migrationhelper
parent
7508ee56
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
201 additions
and
62 deletions
+201
-62
lib/gitlab/database/rename_reserved_paths_migration.rb
lib/gitlab/database/rename_reserved_paths_migration.rb
+31
-0
lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb
...ab/database/rename_reserved_paths_migration/namespaces.rb
+5
-23
lib/gitlab/database/rename_reserved_paths_migration/projects.rb
...tlab/database/rename_reserved_paths_migration/projects.rb
+38
-0
spec/lib/gitlab/database/rename_reserved_paths_migration/namespaces_spec.rb
...tabase/rename_reserved_paths_migration/namespaces_spec.rb
+4
-38
spec/lib/gitlab/database/rename_reserved_paths_migration/projects_spec.rb
...database/rename_reserved_paths_migration/projects_spec.rb
+76
-0
spec/lib/gitlab/database/rename_reserved_paths_migration_spec.rb
...b/gitlab/database/rename_reserved_paths_migration_spec.rb
+47
-1
No files found.
lib/gitlab/database/rename_reserved_paths_migration.rb
View file @
579d8891
...
...
@@ -8,6 +8,7 @@ module Gitlab
def
rename_wildcard_paths
(
one_or_more_paths
)
paths
=
Array
(
one_or_more_paths
)
rename_namespaces
(
paths
,
type: :wildcard
)
rename_projects
(
paths
)
end
def
rename_root_paths
(
paths
)
...
...
@@ -69,6 +70,36 @@ module Gitlab
def
route_exists?
(
full_path
)
MigrationClasses
::
Route
.
where
(
Route
.
arel_table
[
:path
].
matches
(
full_path
)).
any?
end
def
move_pages
(
old_path
,
new_path
)
move_folders
(
pages_dir
,
old_path
,
new_path
)
end
def
move_uploads
(
old_path
,
new_path
)
return
unless
file_storage?
move_folders
(
uploads_dir
,
old_path
,
new_path
)
end
def
move_folders
(
directory
,
old_relative_path
,
new_relative_path
)
old_path
=
File
.
join
(
directory
,
old_relative_path
)
return
unless
File
.
directory?
(
old_path
)
new_path
=
File
.
join
(
directory
,
new_relative_path
)
FileUtils
.
mv
(
old_path
,
new_path
)
end
def
file_storage?
CarrierWave
::
Uploader
::
Base
.
storage
==
CarrierWave
::
Storage
::
File
end
def
uploads_dir
File
.
join
(
CarrierWave
.
root
,
"uploads"
)
end
def
pages_dir
Settings
.
pages
.
path
end
end
end
end
lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb
View file @
579d8891
...
...
@@ -16,23 +16,17 @@ module Gitlab
elsif
type
==
:top_level
MigrationClasses
::
Namespace
.
where
(
parent_id:
nil
)
end
namespaces
.
where
(
'lower(path) in (?)'
,
paths
.
map
(
&
:downcase
))
with_paths
=
MigrationClasses
::
Namespace
.
arel_table
[
:path
].
matches_any
(
paths
)
namespaces
.
where
(
with_paths
)
end
def
rename_namespace
(
namespace
)
old_full_path
,
new_full_path
=
rename_path_for_routable
(
namespace
)
move_repositories
(
namespace
,
old_full_path
,
new_full_path
)
move_namespace_folders
(
uploads_dir
,
old_full_path
,
new_full_path
)
if
file_storage?
move_namespace_folders
(
pages_dir
,
old_full_path
,
new_full_path
)
end
def
move_namespace_folders
(
directory
,
old_relative_path
,
new_relative_path
)
old_path
=
File
.
join
(
directory
,
old_relative_path
)
return
unless
File
.
directory?
(
old_path
)
new_path
=
File
.
join
(
directory
,
new_relative_path
)
FileUtils
.
mv
(
old_path
,
new_path
)
move_uploads
(
old_full_path
,
new_full_path
)
move_pages
(
old_full_path
,
new_full_path
)
end
def
move_repositories
(
namespace
,
old_full_path
,
new_full_path
)
...
...
@@ -70,18 +64,6 @@ module Gitlab
end
ids
end
def
file_storage?
CarrierWave
::
Uploader
::
Base
.
storage
==
CarrierWave
::
Storage
::
File
end
def
uploads_dir
File
.
join
(
CarrierWave
.
root
,
"uploads"
)
end
def
pages_dir
Settings
.
pages
.
path
end
end
end
end
...
...
lib/gitlab/database/rename_reserved_paths_migration/projects.rb
0 → 100644
View file @
579d8891
module
Gitlab
module
Database
module
RenameReservedPathsMigration
module
Projects
include
Gitlab
::
ShellAdapter
def
rename_projects
(
paths
)
projects_for_paths
(
paths
).
each
do
|
project
|
rename_project
(
project
)
end
end
def
rename_project
(
project
)
old_full_path
,
new_full_path
=
rename_path_for_routable
(
project
)
move_repository
(
project
,
old_full_path
,
new_full_path
)
move_repository
(
project
,
"
#{
old_full_path
}
.wiki"
,
"
#{
new_full_path
}
.wiki"
)
move_uploads
(
old_full_path
,
new_full_path
)
move_pages
(
old_full_path
,
new_full_path
)
end
def
move_repository
(
project
,
old_path
,
new_path
)
unless
gitlab_shell
.
mv_repository
(
project
.
repository_storage_path
,
old_path
,
new_path
)
Rails
.
logger
.
error
"Error moving
#{
old_path
}
to
#{
new_path
}
"
end
end
def
projects_for_paths
(
paths
)
with_paths
=
MigrationClasses
::
Project
.
arel_table
[
:path
]
.
matches_any
(
paths
)
MigrationClasses
::
Project
.
where
(
with_paths
)
end
end
end
end
end
spec/lib/gitlab/database/rename_reserved_paths_migration/namespaces_spec.rb
View file @
579d8891
require
'spec_helper'
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
Namespaces
,
:truncate
do
let
(
:test_dir
)
{
File
.
join
(
Rails
.
root
,
'tmp'
,
'tests'
,
'rename_namespaces_test'
)
}
let
(
:uploads_dir
)
{
File
.
join
(
test_dir
,
'public'
,
'uploads'
)
}
let
(
:subject
)
do
ActiveRecord
::
Migration
.
new
.
extend
(
Gitlab
::
Database
::
RenameReservedPathsMigration
...
...
@@ -10,10 +8,6 @@ describe Gitlab::Database::RenameReservedPathsMigration::Namespaces, :truncate d
end
before
do
FileUtils
.
remove_dir
(
test_dir
)
if
File
.
directory?
(
test_dir
)
FileUtils
.
mkdir_p
(
uploads_dir
)
FileUtils
.
remove_dir
(
TestEnv
.
repos_path
)
if
File
.
directory?
(
TestEnv
.
repos_path
)
allow
(
subject
).
to
receive
(
:uploads_dir
).
and_return
(
uploads_dir
)
allow
(
subject
).
to
receive
(
:say
)
end
...
...
@@ -87,32 +81,6 @@ describe Gitlab::Database::RenameReservedPathsMigration::Namespaces, :truncate d
end
end
describe
'#move_namespace_folders'
do
it
'moves a namespace with files'
do
source
=
File
.
join
(
uploads_dir
,
'parent-group'
,
'sub-group'
)
FileUtils
.
mkdir_p
(
source
)
destination
=
File
.
join
(
uploads_dir
,
'parent-group'
,
'moved-group'
)
FileUtils
.
touch
(
File
.
join
(
source
,
'test.txt'
))
expected_file
=
File
.
join
(
destination
,
'test.txt'
)
subject
.
move_namespace_folders
(
uploads_dir
,
File
.
join
(
'parent-group'
,
'sub-group'
),
File
.
join
(
'parent-group'
,
'moved-group'
))
expect
(
File
.
exist?
(
expected_file
)).
to
be
(
true
)
end
it
'moves a parent namespace uploads'
do
source
=
File
.
join
(
uploads_dir
,
'parent-group'
,
'sub-group'
)
FileUtils
.
mkdir_p
(
source
)
destination
=
File
.
join
(
uploads_dir
,
'moved-parent'
,
'sub-group'
)
FileUtils
.
touch
(
File
.
join
(
source
,
'test.txt'
))
expected_file
=
File
.
join
(
destination
,
'test.txt'
)
subject
.
move_namespace_folders
(
uploads_dir
,
'parent-group'
,
'moved-parent'
)
expect
(
File
.
exist?
(
expected_file
)).
to
be
(
true
)
end
end
describe
"#child_ids_for_parent"
do
it
"collects child ids for all levels"
do
parent
=
create
(
:namespace
)
...
...
@@ -148,15 +116,13 @@ describe Gitlab::Database::RenameReservedPathsMigration::Namespaces, :truncate d
end
it
"moves the uploads for the namespace"
do
allow
(
subject
).
to
receive
(
:move_namespace_folders
).
with
(
Settings
.
pages
.
path
,
"the-path"
,
"the-path0"
)
expect
(
subject
).
to
receive
(
:move_namespace_folders
).
with
(
uploads_dir
,
"the-path"
,
"the-path0"
)
expect
(
subject
).
to
receive
(
:move_uploads
).
with
(
"the-path"
,
"the-path0"
)
subject
.
rename_namespace
(
namespace
)
end
it
"moves the pages for the namespace"
do
allow
(
subject
).
to
receive
(
:move_namespace_folders
).
with
(
uploads_dir
,
"the-path"
,
"the-path0"
)
expect
(
subject
).
to
receive
(
:move_namespace_folders
).
with
(
Settings
.
pages
.
path
,
"the-path"
,
"the-path0"
)
expect
(
subject
).
to
receive
(
:move_pages
).
with
(
"the-path"
,
"the-path0"
)
subject
.
rename_namespace
(
namespace
)
end
...
...
spec/lib/gitlab/database/rename_reserved_paths_migration/projects_spec.rb
0 → 100644
View file @
579d8891
require
'spec_helper'
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
Projects
,
:truncate
do
let
(
:subject
)
do
ActiveRecord
::
Migration
.
new
.
extend
(
Gitlab
::
Database
::
RenameReservedPathsMigration
)
end
before
do
allow
(
subject
).
to
receive
(
:say
)
end
describe
'#projects_for_paths'
do
it
'includes the correct projects'
do
project
=
create
(
:empty_project
,
path:
'THE-path'
)
_other_project
=
create
(
:empty_project
)
result_ids
=
subject
.
projects_for_paths
([
'the-PATH'
]).
map
(
&
:id
)
expect
(
result_ids
).
to
contain_exactly
(
project
.
id
)
end
end
describe
'#rename_project'
do
let
(
:project
)
do
create
(
:empty_project
,
path:
'the-path'
,
namespace:
create
(
:namespace
,
path:
'known-parent'
))
end
it
'renames path & route for the project'
do
expect
(
subject
).
to
receive
(
:rename_path_for_routable
).
with
(
project
).
and_call_original
subject
.
rename_project
(
project
)
end
it
'moves the wiki & the repo'
do
expect
(
subject
).
to
receive
(
:move_repository
).
with
(
project
,
'known-parent/the-path.wiki'
,
'known-parent/the-path0.wiki'
)
expect
(
subject
).
to
receive
(
:move_repository
).
with
(
project
,
'known-parent/the-path'
,
'known-parent/the-path0'
)
subject
.
rename_project
(
project
)
end
it
'moves uploads'
do
expect
(
subject
).
to
receive
(
:move_uploads
).
with
(
'known-parent/the-path'
,
'known-parent/the-path0'
)
subject
.
rename_project
(
project
)
end
it
'moves pages'
do
expect
(
subject
).
to
receive
(
:move_pages
).
with
(
'known-parent/the-path'
,
'known-parent/the-path0'
)
subject
.
rename_project
(
project
)
end
end
describe
'#move_repository'
do
let
(
:known_parent
)
{
create
(
:namespace
,
path:
'known-parent'
)
}
let
(
:project
)
{
create
(
:project
,
path:
'the-path'
,
namespace:
known_parent
)
}
it
'moves the repository for a project'
do
expected_path
=
File
.
join
(
TestEnv
.
repos_path
,
'known-parent'
,
'new-repo.git'
)
subject
.
move_repository
(
project
,
'known-parent/the-path'
,
'known-parent/new-repo'
)
expect
(
File
.
directory?
(
expected_path
)).
to
be
(
true
)
end
end
end
spec/lib/gitlab/database/rename_reserved_paths_migration_spec.rb
View file @
579d8891
...
...
@@ -19,7 +19,11 @@ describe Gitlab::Database::RenameReservedPathsMigration do
subject
.
rename_wildcard_paths
([
'first-path'
,
'second-path'
])
end
it
'should rename projects'
it
'should rename projects'
do
expect
(
subject
).
to
receive
(
:rename_projects
).
with
([
'the-path'
])
subject
.
rename_wildcard_paths
([
'the-path'
])
end
end
describe
'#rename_root_paths'
do
...
...
@@ -106,4 +110,46 @@ describe Gitlab::Database::RenameReservedPathsMigration do
end
end
end
describe
"#move_uploads"
do
let
(
:test_dir
)
{
File
.
join
(
Rails
.
root
,
'tmp'
,
'tests'
,
'rename_reserved_paths'
)
}
let
(
:uploads_dir
)
{
File
.
join
(
test_dir
,
'public'
,
'uploads'
)
}
it
'moves subdirectories in the uploads folder'
do
expect
(
subject
).
to
receive
(
:uploads_dir
).
and_return
(
uploads_dir
)
expect
(
subject
).
to
receive
(
:move_folders
).
with
(
uploads_dir
,
'old_path'
,
'new_path'
)
subject
.
move_uploads
(
'old_path'
,
'new_path'
)
end
it
"doesn't move uploads when they are stored in object storage"
do
expect
(
subject
).
to
receive
(
:file_storage?
).
and_return
(
false
)
expect
(
subject
).
not_to
receive
(
:move_folders
)
subject
.
move_uploads
(
'old_path'
,
'new_path'
)
end
end
describe
'#move_folders'
do
let
(
:test_dir
)
{
File
.
join
(
Rails
.
root
,
'tmp'
,
'tests'
,
'rename_reserved_paths'
)
}
let
(
:uploads_dir
)
{
File
.
join
(
test_dir
,
'public'
,
'uploads'
)
}
before
do
FileUtils
.
remove_dir
(
test_dir
)
if
File
.
directory?
(
test_dir
)
FileUtils
.
mkdir_p
(
uploads_dir
)
allow
(
subject
).
to
receive
(
:uploads_dir
).
and_return
(
uploads_dir
)
end
it
'moves a folder with files'
do
source
=
File
.
join
(
uploads_dir
,
'parent-group'
,
'sub-group'
)
FileUtils
.
mkdir_p
(
source
)
destination
=
File
.
join
(
uploads_dir
,
'parent-group'
,
'moved-group'
)
FileUtils
.
touch
(
File
.
join
(
source
,
'test.txt'
))
expected_file
=
File
.
join
(
destination
,
'test.txt'
)
subject
.
move_folders
(
uploads_dir
,
File
.
join
(
'parent-group'
,
'sub-group'
),
File
.
join
(
'parent-group'
,
'moved-group'
))
expect
(
File
.
exist?
(
expected_file
)).
to
be
(
true
)
end
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