Commit dcc81644 authored by Maxime Orefice's avatar Maxime Orefice

Fix cross database query for RefreshCacheDataService

This commit fixes a cross database query between our 2 new
databases.
parent b829a07d
......@@ -3,6 +3,8 @@
module Ci
module Minutes
class RefreshCachedDataService
BATCH_SIZE = 1_000
def initialize(root_namespace)
@root_namespace = root_namespace
end
......@@ -28,10 +30,10 @@ module Ci
return unless ::Feature.enabled?(:ci_pending_builds_maintain_ci_minutes_data, @root_namespace, type: :development, default_enabled: :yaml)
minutes_exceeded = @root_namespace.ci_minutes_quota.minutes_used_up?
all_namespaces = @root_namespace.self_and_descendant_ids
all_namespace_ids = @root_namespace.self_and_descendant_ids.ids
::Gitlab::Database.allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/345867') do
::Ci::PendingBuild.where(namespace: all_namespaces).update_all(minutes_exceeded: minutes_exceeded)
all_namespace_ids.in_groups_of(BATCH_SIZE, minutes_exceeded) do |namespace_ids|
::Ci::PendingBuild.where(namespace: namespace_ids).update_all(minutes_exceeded: minutes_exceeded)
end
end
# rubocop: enable CodeReuse/ActiveRecord
......
......@@ -41,6 +41,24 @@ RSpec.describe Ci::Minutes::RefreshCachedDataService do
expect(pending_build_2.reload.minutes_exceeded).to be_truthy
end
context 'when running multiple updates' do
before do
stub_const("#{described_class}::BATCH_SIZE", 1)
end
it 'runs 2 SQL update queries' do
sql_queries = ActiveRecord::QueryRecorder.new { subject }.log
update_queries_number = sql_queries.inject(0) do |result, query|
result += 1 if query.start_with?("UPDATE")
result
end
expect(update_queries_number).to eq(2)
expect(pending_build_1.reload.minutes_exceeded).to be_falsey
expect(pending_build_2.reload.minutes_exceeded).to be_truthy
end
end
context 'when ci_pending_builds_maintain_ci_minutes_data is disabled' do
before do
stub_feature_flags(ci_pending_builds_maintain_ci_minutes_data: false)
......
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