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
183e4b0d
Commit
183e4b0d
authored
Jul 13, 2020
by
Manoj M J
Committed by
Toon Claes
Jul 13, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backfill missing routes for Bot Users
This change backfills missing routes for Bot Users
parent
8fc065bc
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
178 additions
and
0 deletions
+178
-0
changelogs/unreleased/backfill-routes-for-users-migration.yml
...gelogs/unreleased/backfill-routes-for-users-migration.yml
+5
-0
db/post_migrate/20200703064117_generate_missing_routes_for_bots.rb
...igrate/20200703064117_generate_missing_routes_for_bots.rb
+92
-0
db/structure.sql
db/structure.sql
+1
-0
spec/migrations/generate_missing_routes_for_bots_spec.rb
spec/migrations/generate_missing_routes_for_bots_spec.rb
+80
-0
No files found.
changelogs/unreleased/backfill-routes-for-users-migration.yml
0 → 100644
View file @
183e4b0d
---
title
:
Backfill missing routes for Bot users
merge_request
:
35960
author
:
type
:
fixed
db/post_migrate/20200703064117_generate_missing_routes_for_bots.rb
0 → 100644
View file @
183e4b0d
# frozen_string_literal: true
class
GenerateMissingRoutesForBots
<
ActiveRecord
::
Migration
[
6.0
]
DOWNTIME
=
false
disable_ddl_transaction!
class
User
<
ActiveRecord
::
Base
self
.
table_name
=
'users'
USER_TYPES
=
{
human:
nil
,
support_bot:
1
,
alert_bot:
2
,
visual_review_bot:
3
,
service_user:
4
,
ghost:
5
,
project_bot:
6
,
migration_bot:
7
}.
with_indifferent_access
.
freeze
BOT_USER_TYPES
=
%w[alert_bot project_bot support_bot visual_review_bot migration_bot]
.
freeze
scope
:bots
,
->
{
where
(
user_type:
USER_TYPES
.
values_at
(
*
BOT_USER_TYPES
))
}
end
class
Route
<
ActiveRecord
::
Base
self
.
table_name
=
'routes'
validates
:path
,
uniqueness:
{
case_sensitive:
false
}
end
class
Namespace
<
ActiveRecord
::
Base
self
.
table_name
=
'namespaces'
belongs_to
:owner
,
class_name:
'GenerateMissingRoutesForBots::User'
scope
:for_user
,
->
{
where
(
'type IS NULL'
)
}
scope
:for_bots
,
->
{
for_user
.
joins
(
:owner
).
merge
(
GenerateMissingRoutesForBots
::
User
.
bots
)
}
scope
:without_routes
,
->
do
where
(
'NOT EXISTS (
SELECT 1
FROM routes
WHERE source_type = ?
AND source_id = namespaces.id
)'
,
self
.
source_type_for_route
)
end
def
self
.
source_type_for_route
'Namespace'
end
def
attributes_for_insert
{
source_type:
self
.
class
.
source_type_for_route
,
source_id:
id
,
name:
name
,
path:
path
}
end
end
def
up
# Reset the column information of all the models that update the database
# to ensure the Active Record's knowledge of the table structure is current
Route
.
reset_column_information
logger
=
Gitlab
::
BackgroundMigration
::
Logger
.
build
attributes_to_be_logged
=
%w(id path name)
GenerateMissingRoutesForBots
::
Namespace
.
for_bots
.
without_routes
.
each
do
|
namespace
|
route
=
GenerateMissingRoutesForBots
::
Route
.
create
(
namespace
.
attributes_for_insert
)
namespace_details
=
namespace
.
as_json
.
slice
(
*
attributes_to_be_logged
)
if
route
.
persisted?
logger
.
info
namespace_details
.
merge
(
message:
'a new route was created for the namespace'
)
else
errors
=
route
.
errors
.
full_messages
.
join
(
','
)
logger
.
info
namespace_details
.
merge
(
message:
'route creation failed for the namespace'
,
errors:
errors
)
end
end
end
def
down
# no op
end
end
db/structure.sql
View file @
183e4b0d
...
...
@@ -23636,6 +23636,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200701093859
20200701205710
20200702123805
20200703064117
20200703121557
20200703154822
20200704143633
...
...
spec/migrations/generate_missing_routes_for_bots_spec.rb
0 → 100644
View file @
183e4b0d
# frozen_string_literal: true
require
'spec_helper'
require
Rails
.
root
.
join
(
'db'
,
'post_migrate'
,
'20200703064117_generate_missing_routes_for_bots.rb'
)
RSpec
.
describe
GenerateMissingRoutesForBots
,
:migration
do
let
(
:users
)
{
table
(
:users
)
}
let
(
:namespaces
)
{
table
(
:namespaces
)
}
let
(
:routes
)
{
table
(
:routes
)
}
let
(
:visual_review_bot
)
do
users
.
create!
(
email:
'visual-review-bot@gitlab.com'
,
name:
'GitLab Visual Review Bot'
,
username:
'visual-review-bot'
,
user_type:
3
,
projects_limit:
5
)
end
let
(
:migration_bot
)
do
users
.
create!
(
email:
'migration-bot@gitlab.com'
,
name:
'GitLab Migration Bot'
,
username:
'migration-bot'
,
user_type:
7
,
projects_limit:
5
)
end
let!
(
:visual_review_bot_namespace
)
do
namespaces
.
create!
(
owner_id:
visual_review_bot
.
id
,
name:
visual_review_bot
.
name
,
path:
visual_review_bot
.
username
)
end
let!
(
:migration_bot_namespace
)
do
namespaces
.
create!
(
owner_id:
migration_bot
.
id
,
name:
migration_bot
.
name
,
path:
migration_bot
.
username
)
end
context
'for bot users without an existing route'
do
it
'creates new routes'
do
expect
{
migrate!
}.
to
change
{
routes
.
count
}.
by
(
2
)
end
it
'creates new routes with the same path and name as their namespace'
do
migrate!
[
visual_review_bot
,
migration_bot
].
each
do
|
bot
|
namespace
=
namespaces
.
find_by
(
owner_id:
bot
.
id
)
route
=
route_for
(
namespace:
namespace
)
expect
(
route
.
path
).
to
eq
(
namespace
.
path
)
expect
(
route
.
name
).
to
eq
(
namespace
.
name
)
end
end
end
it
'does not create routes for bot users with existing routes'
do
create_route!
(
namespace:
visual_review_bot_namespace
)
create_route!
(
namespace:
migration_bot_namespace
)
expect
{
migrate!
}.
not_to
change
{
routes
.
count
}
end
it
'does not create routes for human users without an existing route'
do
human_namespace
=
create_human_namespace!
(
name:
'GitLab Human'
,
username:
'human'
)
expect
{
migrate!
}.
not_to
change
{
route_for
(
namespace:
human_namespace
)
}
end
it
'does not create route for a bot user with a missing route, if a human user with the same path already exists'
do
human_namespace
=
create_human_namespace!
(
name:
visual_review_bot
.
name
,
username:
visual_review_bot
.
username
)
create_route!
(
namespace:
human_namespace
)
expect
{
migrate!
}.
not_to
change
{
route_for
(
namespace:
visual_review_bot_namespace
)
}
end
private
def
create_human_namespace!
(
name
:,
username
:)
human
=
users
.
create!
(
email:
'human@gitlab.com'
,
name:
name
,
username:
username
,
user_type:
nil
,
projects_limit:
5
)
namespaces
.
create!
(
owner_id:
human
.
id
,
name:
human
.
name
,
path:
human
.
username
)
end
def
create_route!
(
namespace
:)
routes
.
create!
(
path:
namespace
.
path
,
name:
namespace
.
name
,
source_id:
namespace
.
id
,
source_type:
'Namespace'
)
end
def
route_for
(
namespace
:)
routes
.
find_by
(
source_type:
'Namespace'
,
source_id:
namespace
.
id
)
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