Commit 246709ea authored by David Fernandez's avatar David Fernandez

Add new packages build infos finder

This is a required piece for
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82496

Changelog: other
parent 0c8f6123
# frozen_string_literal: true
module Packages
# TODO rename to BuildInfosFinder when cleaning up packages_graphql_pipelines_resolver
# https://gitlab.com/gitlab-org/gitlab/-/issues/358432
class BuildInfosForManyPackagesFinder
include ActiveRecord::ConnectionAdapters::Quoting
MAX_PAGE_SIZE = 100
def initialize(package_ids, params)
@package_ids = package_ids
@params = params
end
def execute
return Packages::BuildInfo.none if @package_ids.blank?
# This is a highly custom query that
# will not be re-used elsewhere
# rubocop: disable CodeReuse/ActiveRecord
query = Packages::Package.id_in(@package_ids)
.select('build_infos.*')
.from([Packages::Package.arel_table, lateral_query.arel.lateral.as('build_infos')])
.order('build_infos.id DESC')
# We manually select build_infos fields from the lateral query.
# Thus, we need to instruct ActiveRecord that returned rows are
# actually Packages::BuildInfo objects
Packages::BuildInfo.find_by_sql(query.to_sql)
# rubocop: enable CodeReuse/ActiveRecord
end
private
def lateral_query
order_direction = last ? :asc : :desc
# This is a highly custom query that
# will not be re-used elsewhere
# rubocop: disable CodeReuse/ActiveRecord
where_condition = Packages::BuildInfo.arel_table[:package_id]
.eq(Arel.sql("#{Packages::Package.table_name}.id"))
build_infos = ::Packages::BuildInfo.without_empty_pipelines
.where(where_condition)
.order(id: order_direction)
.limit(max_rows_per_package_id)
# rubocop: enable CodeReuse/ActiveRecord
apply_cursor(build_infos)
end
def max_rows_per_package_id
limit = [first, last, max_page_size, MAX_PAGE_SIZE].compact.min
limit += 1 if support_next_page
limit
end
def apply_cursor(build_infos)
if before
build_infos.with_pipeline_id_greater_than(before)
elsif after
build_infos.with_pipeline_id_less_than(after)
else
build_infos
end
end
def first
@params[:first]
end
def last
@params[:last]
end
def max_page_size
@params[:max_page_size]
end
def before
@params[:before]
end
def after
@params[:after]
end
def support_next_page
@params[:support_next_page]
end
end
end
# frozen_string_literal: true
class UpdateIndexOnPackagesBuildInfos < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
NEW_INDEX_NAME = 'index_packages_build_infos_package_id_pipeline_id_id'
OLD_INDEX_NAME = 'index_packages_build_infos_package_id_pipeline_id'
def up
add_concurrent_index :packages_build_infos,
[:package_id, :pipeline_id, :id],
name: NEW_INDEX_NAME
remove_concurrent_index_by_name :packages_build_infos, OLD_INDEX_NAME
end
def down
add_concurrent_index :packages_build_infos,
[:package_id, :pipeline_id],
name: OLD_INDEX_NAME
remove_concurrent_index_by_name :packages_build_infos, NEW_INDEX_NAME
end
end
b3e580387d56847039c4030fbbbda1131016ef6b068ff60f2e4e48563a331051
\ No newline at end of file
......@@ -28471,7 +28471,7 @@ CREATE UNIQUE INDEX index_organizations_on_unique_name_per_group ON customer_rel
CREATE INDEX index_packages_build_infos_on_pipeline_id ON packages_build_infos USING btree (pipeline_id);
CREATE INDEX index_packages_build_infos_package_id_pipeline_id ON packages_build_infos USING btree (package_id, pipeline_id);
CREATE INDEX index_packages_build_infos_package_id_pipeline_id_id ON packages_build_infos USING btree (package_id, pipeline_id, id);
CREATE UNIQUE INDEX index_packages_composer_cache_namespace_and_sha ON packages_composer_cache_files USING btree (namespace_id, file_sha256);
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Packages::BuildInfosForManyPackagesFinder do
using RSpec::Parameterized::TableSyntax
let_it_be(:package) { create(:package) }
let_it_be(:build_infos) { create_list(:package_build_info, 5, :with_pipeline, package: package) }
let_it_be(:build_info_with_empty_pipeline) { create(:package_build_info, package: package) }
let_it_be(:other_package) { create(:package) }
let_it_be(:other_build_infos) { create_list(:package_build_info, 5, :with_pipeline, package: other_package) }
let_it_be(:other_build_info_with_empty_pipeline) { create(:package_build_info, package: other_package) }
let_it_be(:all_build_infos) { build_infos + other_build_infos }
let(:finder) { described_class.new(packages, params) }
let(:packages) { nil }
let(:first) { nil }
let(:last) { nil }
let(:after) { nil }
let(:before) { nil }
let(:max_page_size) { nil }
let(:support_next_page) { false }
let(:params) do
{
first: first,
last: last,
after: after,
before: before,
max_page_size: max_page_size,
support_next_page: support_next_page
}
end
describe '#execute' do
subject { finder.execute }
shared_examples 'returning the expected build infos' do
let(:expected_build_infos) do
expected_build_infos_indexes.map do |idx|
all_build_infos[idx]
end
end
let(:after) do
all_build_infos[after_index].pipeline_id if after_index
end
let(:before) do
all_build_infos[before_index].pipeline_id if before_index
end
it { is_expected.to eq(expected_build_infos) }
end
context 'with nil packages' do
let(:packages) { nil }
it { is_expected.to be_empty }
end
context 'with [] packages' do
let(:packages) { [] }
it { is_expected.to be_empty }
end
context 'with empy scope packages' do
let(:packages) { Packages::Package.none }
it { is_expected.to be_empty }
end
context 'with a single package' do
let(:packages) { package.id }
# rubocop: disable Layout/LineLength
where(:first, :last, :after_index, :before_index, :max_page_size, :support_next_page, :expected_build_infos_indexes) do
# F L AI BI MPS SNP
nil | nil | nil | nil | nil | false | [4, 3, 2, 1, 0]
nil | nil | nil | nil | 10 | false | [4, 3, 2, 1, 0]
nil | nil | nil | nil | 2 | false | [4, 3]
2 | nil | nil | nil | nil | false | [4, 3]
2 | nil | nil | nil | nil | true | [4, 3, 2]
2 | nil | 3 | nil | nil | false | [2, 1]
2 | nil | 3 | nil | nil | true | [2, 1, 0]
3 | nil | 4 | nil | 2 | false | [3, 2]
3 | nil | 4 | nil | 2 | true | [3, 2, 1]
nil | 2 | nil | nil | nil | false | [1, 0]
nil | 2 | nil | nil | nil | true | [2, 1, 0]
nil | 2 | nil | 1 | nil | false | [3, 2]
nil | 2 | nil | 1 | nil | true | [4, 3, 2]
nil | 3 | nil | 0 | 2 | false | [2, 1]
nil | 3 | nil | 0 | 2 | true | [3, 2, 1]
end
# rubocop: enable Layout/LineLength
with_them do
it_behaves_like 'returning the expected build infos'
end
end
context 'with many packages' do
let(:packages) { [package.id, other_package.id] }
# using after_index/before_index when receiving multiple packages doesn't
# make sense but we still verify here that the behavior is coherent.
# rubocop: disable Layout/LineLength
where(:first, :last, :after_index, :before_index, :max_page_size, :support_next_page, :expected_build_infos_indexes) do
# F L AI BI MPS SNP
nil | nil | nil | nil | nil | false | [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
nil | nil | nil | nil | 10 | false | [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
nil | nil | nil | nil | 2 | false | [9, 8, 4, 3]
2 | nil | nil | nil | nil | false | [9, 8, 4, 3]
2 | nil | nil | nil | nil | true | [9, 8, 7, 4, 3, 2]
2 | nil | 3 | nil | nil | false | [2, 1]
2 | nil | 3 | nil | nil | true | [2, 1, 0]
3 | nil | 4 | nil | 2 | false | [3, 2]
3 | nil | 4 | nil | 2 | true | [3, 2, 1]
nil | 2 | nil | nil | nil | false | [6, 5, 1, 0]
nil | 2 | nil | nil | nil | true | [7, 6, 5, 2, 1, 0]
nil | 2 | nil | 1 | nil | false | [6, 5, 3, 2]
nil | 2 | nil | 1 | nil | true | [7, 6, 5, 4, 3, 2]
nil | 3 | nil | 0 | 2 | false | [6, 5, 2, 1]
nil | 3 | nil | 0 | 2 | true | [7, 6, 5, 3, 2, 1]
end
with_them do
it_behaves_like 'returning the expected build infos'
end
# rubocop: enable Layout/LineLength
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