Commit d9823db2 authored by Stan Hu's avatar Stan Hu

Merge branch '268133-update-all-with-subquery' into 'master'

Improve update_all when FROM is a subquery

See merge request gitlab-org/gitlab!46426
parents 41360b46 220112d3
...@@ -33,15 +33,17 @@ class BulkCreateIntegrationService ...@@ -33,15 +33,17 @@ class BulkCreateIntegrationService
klass.insert_all(items_to_insert, returning: [:id]) klass.insert_all(items_to_insert, returning: [:id])
end end
# rubocop: disable CodeReuse/ActiveRecord
def run_callbacks(batch) def run_callbacks(batch)
if integration.external_issue_tracker? if integration.external_issue_tracker?
batch.update_all(has_external_issue_tracker: true) Project.where(id: batch.select(:id)).update_all(has_external_issue_tracker: true)
end end
if integration.external_wiki? if integration.external_wiki?
batch.update_all(has_external_wiki: true) Project.where(id: batch.select(:id)).update_all(has_external_wiki: true)
end end
end end
# rubocop: enable CodeReuse/ActiveRecord
def service_hash def service_hash
if integration.template? if integration.template?
......
...@@ -5,12 +5,14 @@ require 'spec_helper' ...@@ -5,12 +5,14 @@ require 'spec_helper'
RSpec.describe BulkCreateIntegrationService do RSpec.describe BulkCreateIntegrationService do
include JiraServiceHelper include JiraServiceHelper
before do before_all do
stub_jira_service_test stub_jira_service_test
end end
let_it_be(:instance_integration) { create(:jira_service, :instance) } let_it_be(:excluded_group) { create(:group) }
let_it_be(:template_integration) { create(:jira_service, :template) } let_it_be(:excluded_project) { create(:project, group: excluded_group) }
let(:instance_integration) { create(:jira_service, :instance) }
let(:template_integration) { create(:jira_service, :template) }
let(:excluded_attributes) { %w[id project_id group_id inherit_from_id instance template created_at updated_at] } let(:excluded_attributes) { %w[id project_id group_id inherit_from_id instance template created_at updated_at] }
shared_examples 'creates integration from batch ids' do shared_examples 'creates integration from batch ids' do
...@@ -46,15 +48,19 @@ RSpec.describe BulkCreateIntegrationService do ...@@ -46,15 +48,19 @@ RSpec.describe BulkCreateIntegrationService do
described_class.new(integration, batch, association).execute described_class.new(integration, batch, association).execute
expect(project.reload.has_external_issue_tracker).to eq(true) expect(project.reload.has_external_issue_tracker).to eq(true)
expect(excluded_project.reload.has_external_issue_tracker).to eq(false)
end end
context 'with an external wiki integration' do context 'with an external wiki integration' do
let(:integration) { create(:external_wiki_service, :instance) } before do
integration.update!(category: 'common', type: 'ExternalWikiService')
end
it 'updates projects#has_external_wiki for external wiki services' do it 'updates projects#has_external_wiki for external wiki services' do
described_class.new(integration, batch, association).execute described_class.new(integration, batch, association).execute
expect(project.reload.has_external_wiki).to eq(true) expect(project.reload.has_external_wiki).to eq(true)
expect(excluded_project.reload.has_external_wiki).to eq(false)
end end
end end
end end
...@@ -77,14 +83,14 @@ RSpec.describe BulkCreateIntegrationService do ...@@ -77,14 +83,14 @@ RSpec.describe BulkCreateIntegrationService do
end end
end end
context 'with an instance-level integration' do context 'passing an instance-level integration' do
let(:integration) { instance_integration } let(:integration) { instance_integration }
let(:inherit_from_id) { integration.id } let(:inherit_from_id) { integration.id }
context 'with a project association' do context 'with a project association' do
let!(:project) { create(:project) } let!(:project) { create(:project) }
let(:created_integration) { project.jira_service } let(:created_integration) { project.jira_service }
let(:batch) { Project.without_integration(integration) } let(:batch) { Project.where(id: project.id) }
let(:association) { 'project' } let(:association) { 'project' }
it_behaves_like 'creates integration from batch ids' it_behaves_like 'creates integration from batch ids'
...@@ -101,20 +107,37 @@ RSpec.describe BulkCreateIntegrationService do ...@@ -101,20 +107,37 @@ RSpec.describe BulkCreateIntegrationService do
end end
context 'with a group association' do context 'with a group association' do
let_it_be(:group) { create(:group) } let!(:group) { create(:group) }
let(:created_integration) { Service.find_by(group: group) } let(:created_integration) { Service.find_by(group: group) }
let(:batch) { Group.all } let(:batch) { Group.where(id: group.id) }
let(:association) { 'group' } let(:association) { 'group' }
it_behaves_like 'creates integration from batch ids' it_behaves_like 'creates integration from batch ids'
it_behaves_like 'updates inherit_from_id' it_behaves_like 'updates inherit_from_id'
end
end
context 'passing a group integration' do
let_it_be(:group) { create(:group) }
context 'with a project association' do
let!(:project) { create(:project, group: group) }
let(:integration) { create(:jira_service, group: group, project: nil) }
let(:created_integration) { project.jira_service }
let(:batch) { Project.where(id: Project.minimum(:id)..Project.maximum(:id)).without_integration(integration).in_namespace(integration.group.self_and_descendants) }
let(:association) { 'project' }
let(:inherit_from_id) { integration.id }
it_behaves_like 'creates integration from batch ids'
it_behaves_like 'updates inherit_from_id'
it_behaves_like 'updates project callbacks'
end
context 'with a subgroup association' do context 'with a group association' do
let_it_be(:group_integration) { create(:jira_service, group: group, project: nil, inherit_from_id: instance_integration.id) } let!(:subgroup) { create(:group, parent: group) }
let_it_be(:subgroup) { create(:group, parent: group) } let(:integration) { create(:jira_service, group: group, project: nil, inherit_from_id: instance_integration.id) }
let(:integration) { group_integration }
let(:created_integration) { Service.find_by(group: subgroup) } let(:created_integration) { Service.find_by(group: subgroup) }
let(:batch) { Group.all } let(:batch) { Group.where(id: subgroup.id) }
let(:association) { 'group' } let(:association) { 'group' }
let(:inherit_from_id) { instance_integration.id } let(:inherit_from_id) { instance_integration.id }
...@@ -122,15 +145,14 @@ RSpec.describe BulkCreateIntegrationService do ...@@ -122,15 +145,14 @@ RSpec.describe BulkCreateIntegrationService do
it_behaves_like 'updates inherit_from_id' it_behaves_like 'updates inherit_from_id'
end end
end end
end
context 'with a template integration' do context 'passing a template integration' do
let(:integration) { template_integration } let(:integration) { template_integration }
context 'with a project association' do context 'with a project association' do
let!(:project) { create(:project) } let!(:project) { create(:project) }
let(:created_integration) { project.jira_service } let(:created_integration) { project.jira_service }
let(:batch) { Project.all } let(:batch) { Project.where(id: project.id) }
let(:association) { 'project' } let(:association) { 'project' }
let(:inherit_from_id) { integration.id } let(:inherit_from_id) { integration.id }
......
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