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
99a03fd6
Commit
99a03fd6
authored
Apr 25, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move ReservedPathsMigration into V1 namespace
parent
12735eef
Changes
16
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
391 additions
and
378 deletions
+391
-378
db/post_migrate/20170412174900_rename_reserved_dynamic_paths.rb
...t_migrate/20170412174900_rename_reserved_dynamic_paths.rb
+13
-10
lib/gitlab/database/rename_reserved_paths_migration.rb
lib/gitlab/database/rename_reserved_paths_migration.rb
+0
-20
lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb
...base/rename_reserved_paths_migration/migration_classes.rb
+0
-94
lib/gitlab/database/rename_reserved_paths_migration/rename_base.rb
...b/database/rename_reserved_paths_migration/rename_base.rb
+0
-125
lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces.rb
...base/rename_reserved_paths_migration/rename_namespaces.rb
+0
-70
lib/gitlab/database/rename_reserved_paths_migration/rename_projects.rb
...tabase/rename_reserved_paths_migration/rename_projects.rb
+0
-43
lib/gitlab/database/rename_reserved_paths_migration/v1.rb
lib/gitlab/database/rename_reserved_paths_migration/v1.rb
+22
-0
lib/gitlab/database/rename_reserved_paths_migration/v1/migration_classes.rb
...e/rename_reserved_paths_migration/v1/migration_classes.rb
+96
-0
lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb
...atabase/rename_reserved_paths_migration/v1/rename_base.rb
+127
-0
lib/gitlab/database/rename_reserved_paths_migration/v1/rename_namespaces.rb
...e/rename_reserved_paths_migration/v1/rename_namespaces.rb
+72
-0
lib/gitlab/database/rename_reserved_paths_migration/v1/rename_projects.rb
...ase/rename_reserved_paths_migration/v1/rename_projects.rb
+45
-0
spec/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base_spec.rb
...se/rename_reserved_paths_migration/v1/rename_base_spec.rb
+4
-4
spec/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_namespaces_spec.rb
...ame_reserved_paths_migration/v1/rename_namespaces_spec.rb
+3
-3
spec/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_projects_spec.rb
...ename_reserved_paths_migration/v1/rename_projects_spec.rb
+2
-2
spec/lib/gitlab/database/rename_reserved_paths_migration_spec.rb
...b/gitlab/database/rename_reserved_paths_migration_spec.rb
+5
-5
spec/support/fake_migration_classes.rb
spec/support/fake_migration_classes.rb
+2
-2
No files found.
db/post_migrate/20170412174900_rename_reserved_dynamic_paths.rb
View file @
99a03fd6
...
@@ -2,33 +2,36 @@
...
@@ -2,33 +2,36 @@
# for more information on how to write migrations for GitLab.
# for more information on how to write migrations for GitLab.
class
RenameReservedDynamicPaths
<
ActiveRecord
::
Migration
class
RenameReservedDynamicPaths
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
RenameReservedPathsMigration
include
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
DOWNTIME
=
false
DOWNTIME
=
false
disable_ddl_transaction!
disable_ddl_transaction!
DISALLOWED_ROOT_PATHS
=
%w[
DISALLOWED_ROOT_PATHS
=
%w[
-
abuse_reports
api
api
autocomplete
autocomplete
member
explore
explore
uploads
health_check
import
import
notification_settings
abuse_reports
invites
invites
koding
health_check
jwt
jwt
koding
member
notification_settings
oauth
oauth
sent_notifications
sent_notifications
-
uploads
users
users
]
]
DISALLOWED_WILDCARD_PATHS
=
%w[info/lfs/objects gitlab-lfs/objects
DISALLOWED_WILDCARD_PATHS
=
%w[
environments/folders]
environments/folders
gitlab-lfs/objects
info/lfs/objects
]
def
up
def
up
rename_root_paths
(
DISALLOWED_ROOT_PATHS
)
rename_root_paths
(
DISALLOWED_ROOT_PATHS
)
...
...
lib/gitlab/database/rename_reserved_paths_migration.rb
deleted
100644 → 0
View file @
12735eef
module
Gitlab
module
Database
module
RenameReservedPathsMigration
def
self
.
included
(
kls
)
kls
.
include
(
MigrationHelpers
)
end
def
rename_wildcard_paths
(
one_or_more_paths
)
paths
=
Array
(
one_or_more_paths
)
RenameNamespaces
.
new
(
paths
,
self
).
rename_namespaces
(
type: :wildcard
)
RenameProjects
.
new
(
paths
,
self
).
rename_projects
end
def
rename_root_paths
(
paths
)
paths
=
Array
(
paths
)
RenameNamespaces
.
new
(
paths
,
self
).
rename_namespaces
(
type: :top_level
)
end
end
end
end
lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb
deleted
100644 → 0
View file @
12735eef
module
Gitlab
module
Database
module
RenameReservedPathsMigration
module
MigrationClasses
module
Routable
def
full_path
if
route
&&
route
.
path
.
present?
@full_path
||=
route
.
path
else
update_route
if
persisted?
build_full_path
end
end
def
build_full_path
if
parent
&&
path
parent
.
full_path
+
'/'
+
path
else
path
end
end
def
update_route
prepare_route
route
.
save
end
def
prepare_route
route
||
build_route
(
source:
self
)
route
.
path
=
build_full_path
route
.
name
=
build_full_name
@full_path
=
nil
@full_name
=
nil
end
def
build_full_name
if
parent
&&
name
parent
.
human_name
+
' / '
+
name
else
name
end
end
def
human_name
owner
&
.
name
end
end
class
User
<
ActiveRecord
::
Base
self
.
table_name
=
'users'
end
class
Namespace
<
ActiveRecord
::
Base
include
MigrationClasses
::
Routable
self
.
table_name
=
'namespaces'
belongs_to
:parent
,
class_name:
"
#{
MigrationClasses
.
name
}
::Namespace"
has_one
:route
,
as: :source
has_many
:children
,
class_name:
"
#{
MigrationClasses
.
name
}
::Namespace"
,
foreign_key: :parent_id
belongs_to
:owner
,
class_name:
"
#{
MigrationClasses
.
name
}
::User"
# Overridden to have the correct `source_type` for the `route` relation
def
self
.
name
'Namespace'
end
end
class
Route
<
ActiveRecord
::
Base
self
.
table_name
=
'routes'
belongs_to
:source
,
polymorphic:
true
end
class
Project
<
ActiveRecord
::
Base
include
MigrationClasses
::
Routable
has_one
:route
,
as: :source
self
.
table_name
=
'projects'
def
repository_storage_path
Gitlab
.
config
.
repositories
.
storages
[
repository_storage
][
'path'
]
end
# Overridden to have the correct `source_type` for the `route` relation
def
self
.
name
'Project'
end
end
end
end
end
end
lib/gitlab/database/rename_reserved_paths_migration/rename_base.rb
deleted
100644 → 0
View file @
12735eef
module
Gitlab
module
Database
module
RenameReservedPathsMigration
class
RenameBase
attr_reader
:paths
,
:migration
delegate
:update_column_in_batches
,
:replace_sql
,
to: :migration
def
initialize
(
paths
,
migration
)
@paths
=
paths
@migration
=
migration
end
def
path_patterns
@path_patterns
||=
paths
.
map
{
|
path
|
"%
#{
path
}
"
}
end
def
rename_path_for_routable
(
routable
)
old_path
=
routable
.
path
old_full_path
=
routable
.
full_path
# Only remove the last occurrence of the path name to get the parent namespace path
namespace_path
=
remove_last_occurrence
(
old_full_path
,
old_path
)
new_path
=
rename_path
(
namespace_path
,
old_path
)
new_full_path
=
join_routable_path
(
namespace_path
,
new_path
)
# skips callbacks & validations
routable
.
class
.
where
(
id:
routable
).
update_all
(
path:
new_path
)
rename_routes
(
old_full_path
,
new_full_path
)
[
old_full_path
,
new_full_path
]
end
def
rename_routes
(
old_full_path
,
new_full_path
)
replace_statement
=
replace_sql
(
Route
.
arel_table
[
:path
],
old_full_path
,
new_full_path
)
update_column_in_batches
(
:routes
,
:path
,
replace_statement
)
do
|
table
,
query
|
query
.
where
(
MigrationClasses
::
Route
.
arel_table
[
:path
].
matches
(
"
#{
old_full_path
}
%"
))
end
end
def
rename_path
(
namespace_path
,
path_was
)
counter
=
0
path
=
"
#{
path_was
}#{
counter
}
"
while
route_exists?
(
join_routable_path
(
namespace_path
,
path
))
counter
+=
1
path
=
"
#{
path_was
}#{
counter
}
"
end
path
end
def
remove_last_occurrence
(
string
,
pattern
)
string
.
reverse
.
sub
(
pattern
.
reverse
,
""
).
reverse
end
def
join_routable_path
(
namespace_path
,
top_level
)
if
namespace_path
.
present?
File
.
join
(
namespace_path
,
top_level
)
else
top_level
end
end
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
remove_cached_html_for_projects
(
project_ids
)
update_column_in_batches
(
:projects
,
:description_html
,
nil
)
do
|
table
,
query
|
query
.
where
(
table
[
:id
].
in
(
project_ids
))
end
update_column_in_batches
(
:issues
,
:description_html
,
nil
)
do
|
table
,
query
|
query
.
where
(
table
[
:project_id
].
in
(
project_ids
))
end
update_column_in_batches
(
:merge_requests
,
:description_html
,
nil
)
do
|
table
,
query
|
query
.
where
(
table
[
:target_project_id
].
in
(
project_ids
))
end
update_column_in_batches
(
:notes
,
:note_html
,
nil
)
do
|
table
,
query
|
query
.
where
(
table
[
:project_id
].
in
(
project_ids
))
end
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
end
lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces.rb
deleted
100644 → 0
View file @
12735eef
module
Gitlab
module
Database
module
RenameReservedPathsMigration
class
RenameNamespaces
<
RenameBase
include
Gitlab
::
ShellAdapter
def
rename_namespaces
(
type
:)
namespaces_for_paths
(
type:
type
).
each
do
|
namespace
|
rename_namespace
(
namespace
)
end
end
def
namespaces_for_paths
(
type
:)
namespaces
=
case
type
when
:wildcard
MigrationClasses
::
Namespace
.
where
.
not
(
parent_id:
nil
)
when
:top_level
MigrationClasses
::
Namespace
.
where
(
parent_id:
nil
)
end
with_paths
=
MigrationClasses
::
Route
.
arel_table
[
:path
].
matches_any
(
path_patterns
)
namespaces
.
joins
(
:route
).
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_uploads
(
old_full_path
,
new_full_path
)
move_pages
(
old_full_path
,
new_full_path
)
remove_cached_html_for_projects
(
projects_for_namespace
(
namespace
).
map
(
&
:id
))
end
def
move_repositories
(
namespace
,
old_full_path
,
new_full_path
)
repo_paths_for_namespace
(
namespace
).
each
do
|
repository_storage_path
|
# Ensure old directory exists before moving it
gitlab_shell
.
add_namespace
(
repository_storage_path
,
old_full_path
)
unless
gitlab_shell
.
mv_namespace
(
repository_storage_path
,
old_full_path
,
new_full_path
)
message
=
"Exception moving path
#{
repository_storage_path
}
\
from
#{
old_full_path
}
to
#{
new_full_path
}
"
Rails
.
logger
.
error
message
end
end
end
def
repo_paths_for_namespace
(
namespace
)
projects_for_namespace
(
namespace
).
distinct
.
select
(
:repository_storage
).
map
(
&
:repository_storage_path
)
end
def
projects_for_namespace
(
namespace
)
namespace_ids
=
child_ids_for_parent
(
namespace
,
ids:
[
namespace
.
id
])
namespace_or_children
=
MigrationClasses
::
Project
.
arel_table
[
:namespace_id
].
in
(
namespace_ids
)
MigrationClasses
::
Project
.
where
(
namespace_or_children
)
end
def
child_ids_for_parent
(
namespace
,
ids:
[])
namespace
.
children
.
each
do
|
child
|
ids
<<
child
.
id
child_ids_for_parent
(
child
,
ids:
ids
)
if
child
.
children
.
any?
end
ids
end
end
end
end
end
lib/gitlab/database/rename_reserved_paths_migration/rename_projects.rb
deleted
100644 → 0
View file @
12735eef
module
Gitlab
module
Database
module
RenameReservedPathsMigration
class
RenameProjects
<
RenameBase
include
Gitlab
::
ShellAdapter
def
rename_projects
projects_for_paths
.
each
do
|
project
|
rename_project
(
project
)
end
remove_cached_html_for_projects
(
projects_for_paths
.
map
(
&
:id
))
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
return
@projects_for_paths
if
@projects_for_paths
with_paths
=
MigrationClasses
::
Route
.
arel_table
[
:path
]
.
matches_any
(
path_patterns
)
@projects_for_paths
=
MigrationClasses
::
Project
.
joins
(
:route
).
where
(
with_paths
)
end
end
end
end
end
lib/gitlab/database/rename_reserved_paths_migration/v1.rb
0 → 100644
View file @
99a03fd6
module
Gitlab
module
Database
module
RenameReservedPathsMigration
module
V1
def
self
.
included
(
kls
)
kls
.
include
(
MigrationHelpers
)
end
def
rename_wildcard_paths
(
one_or_more_paths
)
paths
=
Array
(
one_or_more_paths
)
RenameNamespaces
.
new
(
paths
,
self
).
rename_namespaces
(
type: :wildcard
)
RenameProjects
.
new
(
paths
,
self
).
rename_projects
end
def
rename_root_paths
(
paths
)
paths
=
Array
(
paths
)
RenameNamespaces
.
new
(
paths
,
self
).
rename_namespaces
(
type: :top_level
)
end
end
end
end
end
lib/gitlab/database/rename_reserved_paths_migration/v1/migration_classes.rb
0 → 100644
View file @
99a03fd6
module
Gitlab
module
Database
module
RenameReservedPathsMigration
module
V1
module
MigrationClasses
module
Routable
def
full_path
if
route
&&
route
.
path
.
present?
@full_path
||=
route
.
path
else
update_route
if
persisted?
build_full_path
end
end
def
build_full_path
if
parent
&&
path
parent
.
full_path
+
'/'
+
path
else
path
end
end
def
update_route
prepare_route
route
.
save
end
def
prepare_route
route
||
build_route
(
source:
self
)
route
.
path
=
build_full_path
route
.
name
=
build_full_name
@full_path
=
nil
@full_name
=
nil
end
def
build_full_name
if
parent
&&
name
parent
.
human_name
+
' / '
+
name
else
name
end
end
def
human_name
owner
&
.
name
end
end
class
User
<
ActiveRecord
::
Base
self
.
table_name
=
'users'
end
class
Namespace
<
ActiveRecord
::
Base
include
MigrationClasses
::
Routable
self
.
table_name
=
'namespaces'
belongs_to
:parent
,
class_name:
"
#{
MigrationClasses
.
name
}
::Namespace"
has_one
:route
,
as: :source
has_many
:children
,
class_name:
"
#{
MigrationClasses
.
name
}
::Namespace"
,
foreign_key: :parent_id
belongs_to
:owner
,
class_name:
"
#{
MigrationClasses
.
name
}
::User"
# Overridden to have the correct `source_type` for the `route` relation
def
self
.
name
'Namespace'
end
end
class
Route
<
ActiveRecord
::
Base
self
.
table_name
=
'routes'
belongs_to
:source
,
polymorphic:
true
end
class
Project
<
ActiveRecord
::
Base
include
MigrationClasses
::
Routable
has_one
:route
,
as: :source
self
.
table_name
=
'projects'
def
repository_storage_path
Gitlab
.
config
.
repositories
.
storages
[
repository_storage
][
'path'
]
end
# Overridden to have the correct `source_type` for the `route` relation
def
self
.
name
'Project'
end
end
end
end
end
end
end
lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb
0 → 100644
View file @
99a03fd6
module
Gitlab
module
Database
module
RenameReservedPathsMigration
module
V1
class
RenameBase
attr_reader
:paths
,
:migration
delegate
:update_column_in_batches
,
:replace_sql
,
to: :migration
def
initialize
(
paths
,
migration
)
@paths
=
paths
@migration
=
migration
end
def
path_patterns
@path_patterns
||=
paths
.
map
{
|
path
|
"%
#{
path
}
"
}
end
def
rename_path_for_routable
(
routable
)
old_path
=
routable
.
path
old_full_path
=
routable
.
full_path
# Only remove the last occurrence of the path name to get the parent namespace path
namespace_path
=
remove_last_occurrence
(
old_full_path
,
old_path
)
new_path
=
rename_path
(
namespace_path
,
old_path
)
new_full_path
=
join_routable_path
(
namespace_path
,
new_path
)
# skips callbacks & validations
routable
.
class
.
where
(
id:
routable
).
update_all
(
path:
new_path
)
rename_routes
(
old_full_path
,
new_full_path
)
[
old_full_path
,
new_full_path
]
end
def
rename_routes
(
old_full_path
,
new_full_path
)
replace_statement
=
replace_sql
(
Route
.
arel_table
[
:path
],
old_full_path
,
new_full_path
)
update_column_in_batches
(
:routes
,
:path
,
replace_statement
)
do
|
table
,
query
|
query
.
where
(
MigrationClasses
::
Route
.
arel_table
[
:path
].
matches
(
"
#{
old_full_path
}
%"
))
end
end
def
rename_path
(
namespace_path
,
path_was
)
counter
=
0
path
=
"
#{
path_was
}#{
counter
}
"
while
route_exists?
(
join_routable_path
(
namespace_path
,
path
))
counter
+=
1
path
=
"
#{
path_was
}#{
counter
}
"
end
path
end
def
remove_last_occurrence
(
string
,
pattern
)
string
.
reverse
.
sub
(
pattern
.
reverse
,
""
).
reverse
end
def
join_routable_path
(
namespace_path
,
top_level
)
if
namespace_path
.
present?
File
.
join
(
namespace_path
,
top_level
)
else
top_level
end
end
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
remove_cached_html_for_projects
(
project_ids
)
update_column_in_batches
(
:projects
,
:description_html
,
nil
)
do
|
table
,
query
|
query
.
where
(
table
[
:id
].
in
(
project_ids
))
end
update_column_in_batches
(
:issues
,
:description_html
,
nil
)
do
|
table
,
query
|
query
.
where
(
table
[
:project_id
].
in
(
project_ids
))
end
update_column_in_batches
(
:merge_requests
,
:description_html
,
nil
)
do
|
table
,
query
|
query
.
where
(
table
[
:target_project_id
].
in
(
project_ids
))
end
update_column_in_batches
(
:notes
,
:note_html
,
nil
)
do
|
table
,
query
|
query
.
where
(
table
[
:project_id
].
in
(
project_ids
))
end
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
end
end
lib/gitlab/database/rename_reserved_paths_migration/v1/rename_namespaces.rb
0 → 100644
View file @
99a03fd6
module
Gitlab
module
Database
module
RenameReservedPathsMigration
module
V1
class
RenameNamespaces
<
RenameBase
include
Gitlab
::
ShellAdapter
def
rename_namespaces
(
type
:)
namespaces_for_paths
(
type:
type
).
each
do
|
namespace
|
rename_namespace
(
namespace
)
end
end
def
namespaces_for_paths
(
type
:)
namespaces
=
case
type
when
:wildcard
MigrationClasses
::
Namespace
.
where
.
not
(
parent_id:
nil
)
when
:top_level
MigrationClasses
::
Namespace
.
where
(
parent_id:
nil
)
end
with_paths
=
MigrationClasses
::
Route
.
arel_table
[
:path
].
matches_any
(
path_patterns
)
namespaces
.
joins
(
:route
).
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_uploads
(
old_full_path
,
new_full_path
)
move_pages
(
old_full_path
,
new_full_path
)
remove_cached_html_for_projects
(
projects_for_namespace
(
namespace
).
map
(
&
:id
))
end
def
move_repositories
(
namespace
,
old_full_path
,
new_full_path
)
repo_paths_for_namespace
(
namespace
).
each
do
|
repository_storage_path
|
# Ensure old directory exists before moving it
gitlab_shell
.
add_namespace
(
repository_storage_path
,
old_full_path
)
unless
gitlab_shell
.
mv_namespace
(
repository_storage_path
,
old_full_path
,
new_full_path
)
message
=
"Exception moving path
#{
repository_storage_path
}
\
from
#{
old_full_path
}
to
#{
new_full_path
}
"
Rails
.
logger
.
error
message
end
end
end
def
repo_paths_for_namespace
(
namespace
)
projects_for_namespace
(
namespace
).
distinct
.
select
(
:repository_storage
).
map
(
&
:repository_storage_path
)
end
def
projects_for_namespace
(
namespace
)
namespace_ids
=
child_ids_for_parent
(
namespace
,
ids:
[
namespace
.
id
])
namespace_or_children
=
MigrationClasses
::
Project
.
arel_table
[
:namespace_id
].
in
(
namespace_ids
)
MigrationClasses
::
Project
.
where
(
namespace_or_children
)
end
def
child_ids_for_parent
(
namespace
,
ids:
[])
namespace
.
children
.
each
do
|
child
|
ids
<<
child
.
id
child_ids_for_parent
(
child
,
ids:
ids
)
if
child
.
children
.
any?
end
ids
end
end
end
end
end
end
lib/gitlab/database/rename_reserved_paths_migration/v1/rename_projects.rb
0 → 100644
View file @
99a03fd6
module
Gitlab
module
Database
module
RenameReservedPathsMigration
module
V1
class
RenameProjects
<
RenameBase
include
Gitlab
::
ShellAdapter
def
rename_projects
projects_for_paths
.
each
do
|
project
|
rename_project
(
project
)
end
remove_cached_html_for_projects
(
projects_for_paths
.
map
(
&
:id
))
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
return
@projects_for_paths
if
@projects_for_paths
with_paths
=
MigrationClasses
::
Route
.
arel_table
[
:path
]
.
matches_any
(
path_patterns
)
@projects_for_paths
=
MigrationClasses
::
Project
.
joins
(
:route
).
where
(
with_paths
)
end
end
end
end
end
end
spec/lib/gitlab/database/rename_reserved_paths_migration/rename_base_spec.rb
→
spec/lib/gitlab/database/rename_reserved_paths_migration/
v1/
rename_base_spec.rb
View file @
99a03fd6
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
RenameBase
do
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
::
RenameBase
do
let
(
:migration
)
{
FakeRenameReservedPathMigration
.
new
}
let
(
:migration
)
{
FakeRenameReservedPathMigration
V1
.
new
}
let
(
:subject
)
{
described_class
.
new
([
'the-path'
],
migration
)
}
let
(
:subject
)
{
described_class
.
new
([
'the-path'
],
migration
)
}
before
do
before
do
...
@@ -9,12 +9,12 @@ describe Gitlab::Database::RenameReservedPathsMigration::RenameBase do
...
@@ -9,12 +9,12 @@ describe Gitlab::Database::RenameReservedPathsMigration::RenameBase do
end
end
def
migration_namespace
(
namespace
)
def
migration_namespace
(
namespace
)
Gitlab
::
Database
::
RenameReservedPathsMigration
::
MigrationClasses
::
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
::
MigrationClasses
::
Namespace
.
find
(
namespace
.
id
)
Namespace
.
find
(
namespace
.
id
)
end
end
def
migration_project
(
project
)
def
migration_project
(
project
)
Gitlab
::
Database
::
RenameReservedPathsMigration
::
MigrationClasses
::
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
::
MigrationClasses
::
Project
.
find
(
project
.
id
)
Project
.
find
(
project
.
id
)
end
end
...
...
spec/lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces_spec.rb
→
spec/lib/gitlab/database/rename_reserved_paths_migration/
v1/
rename_namespaces_spec.rb
View file @
99a03fd6
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
RenameNamespaces
do
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
::
RenameNamespaces
do
let
(
:migration
)
{
FakeRenameReservedPathMigration
.
new
}
let
(
:migration
)
{
FakeRenameReservedPathMigration
V1
.
new
}
let
(
:subject
)
{
described_class
.
new
([
'the-path'
],
migration
)
}
let
(
:subject
)
{
described_class
.
new
([
'the-path'
],
migration
)
}
before
do
before
do
...
@@ -9,7 +9,7 @@ describe Gitlab::Database::RenameReservedPathsMigration::RenameNamespaces do
...
@@ -9,7 +9,7 @@ describe Gitlab::Database::RenameReservedPathsMigration::RenameNamespaces do
end
end
def
migration_namespace
(
namespace
)
def
migration_namespace
(
namespace
)
Gitlab
::
Database
::
RenameReservedPathsMigration
::
MigrationClasses
::
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
::
MigrationClasses
::
Namespace
.
find
(
namespace
.
id
)
Namespace
.
find
(
namespace
.
id
)
end
end
...
...
spec/lib/gitlab/database/rename_reserved_paths_migration/rename_projects_spec.rb
→
spec/lib/gitlab/database/rename_reserved_paths_migration/
v1/
rename_projects_spec.rb
View file @
99a03fd6
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
RenameProjects
do
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
::
RenameProjects
do
let
(
:migration
)
{
FakeRenameReservedPathMigration
.
new
}
let
(
:migration
)
{
FakeRenameReservedPathMigration
V1
.
new
}
let
(
:subject
)
{
described_class
.
new
([
'the-path'
],
migration
)
}
let
(
:subject
)
{
described_class
.
new
([
'the-path'
],
migration
)
}
before
do
before
do
...
...
spec/lib/gitlab/database/rename_reserved_paths_migration_spec.rb
View file @
99a03fd6
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
do
describe
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
do
let
(
:subject
)
{
FakeRenameReservedPathMigration
.
new
}
let
(
:subject
)
{
FakeRenameReservedPathMigration
V1
.
new
}
before
do
before
do
allow
(
subject
).
to
receive
(
:say
)
allow
(
subject
).
to
receive
(
:say
)
...
@@ -10,7 +10,7 @@ describe Gitlab::Database::RenameReservedPathsMigration do
...
@@ -10,7 +10,7 @@ describe Gitlab::Database::RenameReservedPathsMigration do
describe
'#rename_wildcard_paths'
do
describe
'#rename_wildcard_paths'
do
it
'should rename namespaces'
do
it
'should rename namespaces'
do
rename_namespaces
=
double
rename_namespaces
=
double
expect
(
Gitlab
::
Database
::
RenameReservedPathsMigration
::
RenameNamespaces
).
expect
(
described_class
::
RenameNamespaces
).
to
receive
(
:new
).
with
([
'first-path'
,
'second-path'
],
subject
).
to
receive
(
:new
).
with
([
'first-path'
,
'second-path'
],
subject
).
and_return
(
rename_namespaces
)
and_return
(
rename_namespaces
)
expect
(
rename_namespaces
).
to
receive
(
:rename_namespaces
).
expect
(
rename_namespaces
).
to
receive
(
:rename_namespaces
).
...
@@ -21,7 +21,7 @@ describe Gitlab::Database::RenameReservedPathsMigration do
...
@@ -21,7 +21,7 @@ describe Gitlab::Database::RenameReservedPathsMigration do
it
'should rename projects'
do
it
'should rename projects'
do
rename_projects
=
double
rename_projects
=
double
expect
(
Gitlab
::
Database
::
RenameReservedPathsMigration
::
RenameProjects
).
expect
(
described_class
::
RenameProjects
).
to
receive
(
:new
).
with
([
'the-path'
],
subject
).
to
receive
(
:new
).
with
([
'the-path'
],
subject
).
and_return
(
rename_projects
)
and_return
(
rename_projects
)
...
@@ -34,7 +34,7 @@ describe Gitlab::Database::RenameReservedPathsMigration do
...
@@ -34,7 +34,7 @@ describe Gitlab::Database::RenameReservedPathsMigration do
describe
'#rename_root_paths'
do
describe
'#rename_root_paths'
do
it
'should rename namespaces'
do
it
'should rename namespaces'
do
rename_namespaces
=
double
rename_namespaces
=
double
expect
(
Gitlab
::
Database
::
RenameReservedPathsMigration
::
RenameNamespaces
).
expect
(
described_class
::
RenameNamespaces
).
to
receive
(
:new
).
with
([
'the-path'
],
subject
).
to
receive
(
:new
).
with
([
'the-path'
],
subject
).
and_return
(
rename_namespaces
)
and_return
(
rename_namespaces
)
expect
(
rename_namespaces
).
to
receive
(
:rename_namespaces
).
expect
(
rename_namespaces
).
to
receive
(
:rename_namespaces
).
...
...
spec/support/fake_migration_classes.rb
View file @
99a03fd6
class
FakeRenameReservedPathMigration
<
ActiveRecord
::
Migration
class
FakeRenameReservedPathMigration
V1
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
RenameReservedPathsMigration
include
Gitlab
::
Database
::
RenameReservedPathsMigration
::
V1
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