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
3111c2f5
Commit
3111c2f5
authored
Oct 12, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate user private tokens to personal access tokens
parent
3f24f9ed
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
0 deletions
+103
-0
db/migrate/20171012125712_migrate_user_authentication_token_to_personal_access_token.rb
...ate_user_authentication_token_to_personal_access_token.rb
+78
-0
spec/migrations/migrate_user_authentication_token_to_personal_access_token_spec.rb
...ser_authentication_token_to_personal_access_token_spec.rb
+25
-0
No files found.
db/migrate/20171012125712_migrate_user_authentication_token_to_personal_access_token.rb
0 → 100644
View file @
3111c2f5
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class
MigrateUserAuthenticationTokenToPersonalAccessToken
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
# Set this constant to true if this migration requires downtime.
DOWNTIME
=
false
# disable_ddl_transaction!
TOKEN_NAME
=
'Private Token'
.
freeze
def
up
execute
<<~
SQL
INSERT INTO personal_access_tokens (user_id, token, name, created_at, updated_at, scopes)
SELECT id, authentication_token, '
#{
TOKEN_NAME
}
', NOW(), NOW(), '
#{
%
w
[
api
].
to_yaml
}
'
FROM users
WHERE authentication_token IS NOT NULL
AND admin = FALSE
AND NOT EXISTS (
SELECT true
FROM personal_access_tokens
WHERE user_id = users.id
AND token = users.authentication_token
)
SQL
# Admins also need the `sudo` scope
execute
<<~
SQL
INSERT INTO personal_access_tokens (user_id, token, name, created_at, updated_at, scopes)
SELECT id, authentication_token, '
#{
TOKEN_NAME
}
', NOW(), NOW(), '
#{
%
w
[
api
sudo
].
to_yaml
}
'
FROM users
WHERE authentication_token IS NOT NULL
AND admin = TRUE
AND NOT EXISTS (
SELECT true
FROM personal_access_tokens
WHERE user_id = users.id
AND token = users.authentication_token
)
SQL
end
def
down
if
Gitlab
::
Database
.
postgresql?
execute
<<~
SQL
UPDATE users
SET authentication_token = pats.token
FROM (
SELECT user_id, token
FROM personal_access_tokens
WHERE name = '
#{
TOKEN_NAME
}
'
) AS pats
WHERE id = pats.user_id
SQL
else
execute
<<~
SQL
UPDATE users
INNER JOIN personal_access_tokens AS pats
ON users.id = pats.user_id
SET authentication_token = pats.token
WHERE pats.name = '
#{
TOKEN_NAME
}
'
SQL
end
execute
<<~
SQL
DELETE FROM personal_access_tokens
WHERE name = '
#{
TOKEN_NAME
}
'
AND EXISTS (
SELECT true
FROM users
WHERE id = personal_access_tokens.user_id
AND authentication_token = personal_access_tokens.token
)
SQL
end
end
spec/migrations/migrate_user_authentication_token_to_personal_access_token_spec.rb
0 → 100644
View file @
3111c2f5
require
'spec_helper'
require
Rails
.
root
.
join
(
'db'
,
'migrate'
,
'20171012125712_migrate_user_authentication_token_to_personal_access_token.rb'
)
describe
MigrateUserAuthenticationTokenToPersonalAccessToken
,
:migration
do
let
(
:users
)
{
table
(
:users
)
}
let
(
:personal_access_tokens
)
{
table
(
:personal_access_tokens
)
}
let!
(
:user
)
{
users
.
create!
(
id:
1
,
email:
'user@example.com'
,
authentication_token:
'user-token'
,
admin:
false
)
}
let!
(
:admin
)
{
users
.
create!
(
id:
2
,
email:
'admin@example.com'
,
authentication_token:
'admin-token'
,
admin:
true
)
}
it
'migrates private tokens to Personal Access Tokens'
do
migrate!
expect
(
personal_access_tokens
.
count
).
to
eq
(
2
)
user_token
=
personal_access_tokens
.
find_by
(
user_id:
user
.
id
)
admin_token
=
personal_access_tokens
.
find_by
(
user_id:
admin
.
id
)
expect
(
user_token
.
token
).
to
eq
(
'user-token'
)
expect
(
admin_token
.
token
).
to
eq
(
'admin-token'
)
expect
(
user_token
.
scopes
).
to
eq
(
%w[api]
.
to_yaml
)
expect
(
admin_token
.
scopes
).
to
eq
(
%w[api sudo]
.
to_yaml
)
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