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
1fe8b7f6
Commit
1fe8b7f6
authored
May 05, 2017
by
James Lopez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor propagate service to use batch inserts and subquery instead of left join
parent
cf002738
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
6 deletions
+26
-6
app/services/projects/propagate_service.rb
app/services/projects/propagate_service.rb
+26
-6
No files found.
app/services/projects/propagate_service.rb
View file @
1fe8b7f6
...
@@ -26,7 +26,7 @@ module Projects
...
@@ -26,7 +26,7 @@ module Projects
loop
do
loop
do
batch
=
project_ids_batch
(
offset
)
batch
=
project_ids_batch
(
offset
)
b
atch
.
each
{
|
project_id
|
create_from_template
(
project_id
)
}
b
ulk_create_from_template
(
batch
)
break
if
batch
.
size
<
BATCH_SIZE
break
if
batch
.
size
<
BATCH_SIZE
...
@@ -34,14 +34,34 @@ module Projects
...
@@ -34,14 +34,34 @@ module Projects
end
end
end
end
def
create_from_template
(
project_id
)
def
bulk_create_from_template
(
batch
)
Service
.
build_from_template
(
project_id
,
@template
).
save!
service_hash_list
=
batch
.
map
do
|
project_id
|
service_hash
.
merge
(
'project_id'
=>
project_id
)
end
Project
.
transaction
do
Service
.
create!
(
service_hash_list
)
end
end
end
def
project_ids_batch
(
offset
)
def
project_ids_batch
(
offset
)
Project
.
joins
(
'LEFT JOIN services ON services.project_id = projects.id'
).
Project
.
connection
.
execute
(
where
(
'services.type != ? OR services.id IS NULL'
,
@template
.
type
).
<<-
SQL
limit
(
BATCH_SIZE
).
offset
(
offset
).
pluck
(
:id
)
SELECT id
FROM projects
WHERE NOT EXISTS (
SELECT true
FROM services
WHERE services.project_id = projects.id
AND services.type = '
#{
@template
.
type
}
'
)
LIMIT
#{
BATCH_SIZE
}
OFFSET
#{
offset
}
SQL
).
to_a
.
flatten
end
def
service_hash
@service_hash
||=
@template
.
as_json
(
methods: :type
).
except
(
'id'
,
'template'
)
end
end
end
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