Skip to content
Snippets Groups Projects
Commit 1fe8b7f6 authored by James Lopez's avatar James Lopez
Browse files

refactor propagate service to use batch inserts and subquery instead of left join

parent cf002738
No related branches found
No related tags found
No related merge requests found
Loading
@@ -26,7 +26,7 @@ module Projects
Loading
@@ -26,7 +26,7 @@ module Projects
loop do loop do
batch = project_ids_batch(offset) batch = project_ids_batch(offset)
   
batch.each { |project_id| create_from_template(project_id) } bulk_create_from_template(batch)
   
break if batch.size < BATCH_SIZE break if batch.size < BATCH_SIZE
   
Loading
@@ -34,14 +34,34 @@ module Projects
Loading
@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment