Commit b5eb5b38 authored by Robert Speicher's avatar Robert Speicher

Merge branch '273176-use-active-record-or-instead-of-from-union' into 'master'

Fix update_all when FROM is a subquery

See merge request gitlab-org/gitlab!46260
parents cf65b8a1 66897176
...@@ -9,7 +9,7 @@ class BulkUpdateIntegrationService ...@@ -9,7 +9,7 @@ class BulkUpdateIntegrationService
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def execute def execute
Service.transaction do Service.transaction do
batch.update_all(service_hash) Service.where(id: batch.select(:id)).update_all(service_hash)
if integration.data_fields_present? if integration.data_fields_present?
integration.data_fields.class.where(service_id: batch.select(:id)).update_all(data_fields_hash) integration.data_fields.class.where(service_id: batch.select(:id)).update_all(data_fields_hash)
......
...@@ -10,60 +10,69 @@ RSpec.describe BulkUpdateIntegrationService do ...@@ -10,60 +10,69 @@ RSpec.describe BulkUpdateIntegrationService do
end end
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] }
let(:batch) do
Service.inherited_descendants_from_self_or_ancestors_from(subgroup_integration).where(id: group_integration.id..integration.id)
end
let_it_be(:group) { create(:group) } let_it_be(:group) { create(:group) }
let_it_be(:subgroup) { create(:group, parent: group) }
let_it_be(:group_integration) do let_it_be(:group_integration) do
JiraService.create!( JiraService.create!(
group: group, group: group,
active: true, url: 'http://group.jira.com'
push_events: true,
url: 'http://update-jira.instance.com',
username: 'user',
password: 'secret'
) )
end end
let_it_be(:subgroup_integration) do let_it_be(:subgroup_integration) do
JiraService.create!( JiraService.create!(
inherit_from_id: group_integration.id, inherit_from_id: group_integration.id,
group: create(:group, parent: group), group: subgroup,
active: true, url: 'http://subgroup.jira.com',
push_events: true, push_events: true
url: 'http://update-jira.instance.com', )
username: 'user', end
password: 'secret'
let_it_be(:excluded_integration) do
JiraService.create!(
group: create(:group),
url: 'http://another.jira.com',
push_events: false
) )
end end
let_it_be(:integration) do let_it_be(:integration) do
JiraService.create!( JiraService.create!(
project: create(:project), project: create(:project, group: subgroup),
instance: false, inherit_from_id: subgroup_integration.id,
active: true, url: 'http://project.jira.com',
push_events: false, push_events: false
url: 'http://jira.instance.com',
username: 'user',
password: 'secret'
) )
end end
context 'with inherited integration' do context 'with inherited integration' do
it 'updates the integration' do it 'updates the integration', :aggregate_failures do
described_class.new(subgroup_integration, Service.where.not(project: nil)).execute described_class.new(subgroup_integration, batch).execute
expect(integration.reload.inherit_from_id).to eq(group_integration.id) expect(integration.reload.inherit_from_id).to eq(group_integration.id)
expect(integration.attributes.except(*excluded_attributes)) expect(integration.reload.attributes.except(*excluded_attributes))
.to eq(subgroup_integration.attributes.except(*excluded_attributes)) .to eq(subgroup_integration.attributes.except(*excluded_attributes))
expect(excluded_integration.reload.inherit_from_id).not_to eq(group_integration.id)
expect(excluded_integration.reload.attributes.except(*excluded_attributes))
.not_to eq(subgroup_integration.attributes.except(*excluded_attributes))
end end
context 'with integration with data fields' do context 'with integration with data fields' do
let(:excluded_attributes) { %w[id service_id created_at updated_at] } let(:excluded_attributes) { %w[id service_id created_at updated_at] }
it 'updates the data fields from the integration' do it 'updates the data fields from the integration', :aggregate_failures do
described_class.new(subgroup_integration, Service.where.not(project: nil)).execute described_class.new(subgroup_integration, batch).execute
expect(integration.reload.data_fields.attributes.except(*excluded_attributes)) expect(integration.data_fields.attributes.except(*excluded_attributes))
.to eq(subgroup_integration.data_fields.attributes.except(*excluded_attributes)) .to eq(subgroup_integration.data_fields.attributes.except(*excluded_attributes))
expect(integration.data_fields.attributes.except(*excluded_attributes))
.not_to eq(excluded_integration.data_fields.attributes.except(*excluded_attributes))
end end
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