Commit 557f8fef authored by Fabio Pitino's avatar Fabio Pitino

Merge branch...

Merge branch 'mo-crossdatabasemodificationacrossunsupportedtableserror-in-ci-updatebuildqueueservice' into 'master'

Fix cross modification table in Ci::UpdateBuildQueueService

See merge request gitlab-org/gitlab!80161
parents 1a25854e 5f21e7cc
......@@ -6,6 +6,7 @@ module Ci
# This class ensures that we keep 1 record per namespace per month.
class NamespaceMonthlyUsage < Ci::ApplicationRecord
include Ci::NamespacedModelName
include AfterCommitQueue
belongs_to :namespace
......@@ -29,10 +30,17 @@ module Ci
current_usage = unsafe_find_current(namespace_id)
return current_usage if current_usage
current_month.for_namespace(namespace_id).create!.tap do
Namespace.find_by_id(namespace_id).try do |namespace|
Ci::Minutes::Limit.new(namespace).recalculate_remaining_purchased_minutes!
current_month.for_namespace(namespace_id).new.tap do |new_usage|
# TODO: Remove in https://gitlab.com/gitlab-org/gitlab/-/issues/350617
# Avoid cross-database modifications in transaction since
# recalculation of purchased minutes touches `namespaces` table.
new_usage.run_after_commit do
Namespace.find_by_id(namespace_id).try do |namespace|
Ci::Minutes::Limit.new(namespace).recalculate_remaining_purchased_minutes!
end
end
new_usage.save!
end
rescue ActiveRecord::RecordNotUnique
unsafe_find_current(namespace_id)
......
......@@ -133,6 +133,33 @@ RSpec.describe Ci::Minutes::NamespaceMonthlyUsage do
it_behaves_like 'attempts recalculation of additional minutes'
end
context 'when inside a transaction in ci database' do
let!(:previous_usage) do
create(:ci_namespace_monthly_usage,
namespace: namespace,
date: described_class.beginning_of_month(3.months.ago))
end
let(:project) { create(:project, namespace: namespace) }
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, :created, pipeline: pipeline) }
before do
namespace.clear_memoization(:ci_minutes_quota)
create(:ci_runner, :instance_type)
end
subject do
pipeline.transaction do
pipeline.touch
Ci::Minutes::NamespaceMonthlyUsage.find_or_create_current(namespace_id: namespace.id)
end
end
it_behaves_like 'creates usage record'
it_behaves_like 'attempts recalculation of additional minutes'
end
context 'when last known usage is more than 1 month ago' do
let!(:previous_usage) do
create(:ci_namespace_monthly_usage,
......
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