Commit 6b9fa52e authored by Imre Farkas's avatar Imre Farkas

Merge branch 'extract-project-ids-without-integration' into 'master'

Remove duplication of project_ids_without_integration

See merge request gitlab-org/gitlab!34763
parents a8588039 92316603
...@@ -15,5 +15,19 @@ module Integration ...@@ -15,5 +15,19 @@ module Integration
Project.where(id: custom_integration_project_ids) Project.where(id: custom_integration_project_ids)
end end
def ids_without_integration(integration, limit)
services = Service
.select('1')
.where('services.project_id = projects.id')
.where(type: integration.type)
Project
.where('NOT EXISTS (?)', services)
.where(pending_delete: false)
.where(archived: false)
.limit(limit)
.pluck(:id)
end
end end
end end
...@@ -64,7 +64,7 @@ module Admin ...@@ -64,7 +64,7 @@ module Admin
def create_integration_for_projects_without_integration def create_integration_for_projects_without_integration
loop do loop do
batch = Project.uncached { project_ids_without_integration } batch = Project.uncached { Project.ids_without_integration(integration, BATCH_SIZE) }
bulk_create_from_integration(batch) unless batch.empty? bulk_create_from_integration(batch) unless batch.empty?
...@@ -114,22 +114,6 @@ module Admin ...@@ -114,22 +114,6 @@ module Admin
integration.type == 'ExternalWikiService' integration.type == 'ExternalWikiService'
end end
# rubocop: disable CodeReuse/ActiveRecord
def project_ids_without_integration
services = Service
.select('1')
.where('services.project_id = projects.id')
.where(type: integration.type)
Project
.where('NOT EXISTS (?)', services)
.where(pending_delete: false)
.where(archived: false)
.limit(BATCH_SIZE)
.pluck(:id)
end
# rubocop: enable CodeReuse/ActiveRecord
def service_hash def service_hash
@service_hash ||= integration.to_service_hash @service_hash ||= integration.to_service_hash
.tap { |json| json['inherit_from_id'] = integration.id } .tap { |json| json['inherit_from_id'] = integration.id }
......
...@@ -26,7 +26,7 @@ module Projects ...@@ -26,7 +26,7 @@ module Projects
def propagate_projects_with_template def propagate_projects_with_template
loop do loop do
batch = Project.uncached { project_ids_without_integration } batch = Project.uncached { Project.ids_without_integration(template, BATCH_SIZE) }
bulk_create_from_template(batch) unless batch.empty? bulk_create_from_template(batch) unless batch.empty?
...@@ -50,22 +50,6 @@ module Projects ...@@ -50,22 +50,6 @@ module Projects
end end
end end
# rubocop: disable CodeReuse/ActiveRecord
def project_ids_without_integration
services = Service
.select('1')
.where('services.project_id = projects.id')
.where(type: template.type)
Project
.where('NOT EXISTS (?)', services)
.where(pending_delete: false)
.where(archived: false)
.limit(BATCH_SIZE)
.pluck(:id)
end
# rubocop: enable CodeReuse/ActiveRecord
def bulk_insert(klass, columns, values_array) def bulk_insert(klass, columns, values_array)
items_to_insert = values_array.map { |array| Hash[columns.zip(array)] } items_to_insert = values_array.map { |array| Hash[columns.zip(array)] }
......
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Integration do RSpec.describe Integration do
let(:project_1) { create(:project) } let!(:project_1) { create(:project) }
let(:project_2) { create(:project) } let!(:project_2) { create(:project) }
let!(:project_3) { create(:project) }
let(:instance_integration) { create(:jira_service, :instance) } let(:instance_integration) { create(:jira_service, :instance) }
before do before do
...@@ -18,4 +19,10 @@ RSpec.describe Integration do ...@@ -18,4 +19,10 @@ RSpec.describe Integration do
expect(Project.with_custom_integration_for(instance_integration)).to contain_exactly(project_2) expect(Project.with_custom_integration_for(instance_integration)).to contain_exactly(project_2)
end end
end end
describe '#ids_without_integration' do
it 'returns projects ids without an integration' do
expect(Project.ids_without_integration(instance_integration, 100)).to contain_exactly(project_3.id)
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment