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
51913c32
Commit
51913c32
authored
May 19, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simpler way of renaming users
parent
84f8cd17
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
10 deletions
+10
-10
db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb
...ate/20170518200835_rename_users_with_renamed_namespace.rb
+6
-9
spec/migrations/rename_users_with_renamed_namespace_spec.rb
spec/migrations/rename_users_with_renamed_namespace_spec.rb
+4
-1
No files found.
db/post_migrate/20170518200835_rename_users_with_renamed_namespace.rb
View file @
51913c32
...
...
@@ -26,15 +26,12 @@ class RenameUsersWithRenamedNamespace < ActiveRecord::Migration
]
def
up
namespace_table
=
Arel
::
Table
.
new
(
'namespaces'
)
users_table
=
Arel
::
Table
.
new
(
'users'
)
matching_path
=
namespace_table
.
project
(
namespace_table
[
:path
])
.
join
(
users_table
).
on
(
users_table
[
:id
].
eq
(
namespace_table
[
:owner_id
]))
.
where
(
users_table
[
:username
].
not_eq
(
namespace_table
[
:path
]))
path_name
=
Arel
::
Nodes
::
SqlLiteral
.
new
(
"matching_path.path FROM (
#{
matching_path
.
to_sql
}
) as matching_path"
)
update_column_in_batches
(
:users
,
:username
,
path_name
)
do
|
table
,
query
|
query
.
where
(
table
[
:username
].
matches_any
(
DISALLOWED_ROOT_PATHS
))
DISALLOWED_ROOT_PATHS
.
each
do
|
path
|
update_sql
=
"UPDATE users SET username = namespaces.path "
\
"FROM namespaces WHERE namespaces.owner_id = users.id "
\
"AND namespaces.type IS NULL "
\
"AND users.username ILIKE '
#{
path
}
'"
connection
.
execute
(
update_sql
)
end
end
...
...
spec/migrations/rename_users_with_renamed_namespace_spec.rb
View file @
51913c32
...
...
@@ -2,8 +2,9 @@ require 'spec_helper'
require
Rails
.
root
.
join
(
'db'
,
'post_migrate'
,
'20170518200835_rename_users_with_renamed_namespace.rb'
)
describe
RenameUsersWithRenamedNamespace
,
truncate:
true
do
it
'renames a user that had
his
namespace renamed to the namespace path'
do
it
'renames a user that had
their
namespace renamed to the namespace path'
do
other_user
=
create
(
:user
,
username:
'kodingu'
)
other_user1
=
create
(
:user
,
username:
'api0'
)
user
=
create
(
:user
,
username:
"Users0"
)
user
.
update_attribute
(
:username
,
'Users'
)
...
...
@@ -14,6 +15,8 @@ describe RenameUsersWithRenamedNamespace, truncate: true do
expect
(
user
.
reload
.
username
).
to
eq
(
'Users0'
)
expect
(
user1
.
reload
.
username
).
to
eq
(
'import0'
)
expect
(
other_user
.
reload
.
username
).
to
eq
(
'kodingu'
)
expect
(
other_user1
.
reload
.
username
).
to
eq
(
'api0'
)
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