upload_registry_query_builder.rb 1.24 KB
Newer Older
1 2 3 4 5 6 7
# frozen_string_literal: true

# Builder class to create composable queries using FDW to
# retrieve file registries.
#
# Basic usage:
#
8
#     Gitlab::Geo::Fdw::UploadRegistryQueryBuilder.new.for_model(project)
9 10 11 12
#
module Gitlab
  module Geo
    class Fdw
13
      class UploadRegistryQueryBuilder < BaseQueryBuilder
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
        # rubocop:disable CodeReuse/ActiveRecord
        def for_model(model)
          reflect(
            query
              .joins(fdw_inner_join_uploads)
              .where(
                fdw_upload_table[:model_id].eq(model.id)
                  .and(fdw_upload_table[:model_type].eq(model.class.name))
              )
          )
        end
        # rubocop:enable CodeReuse/ActiveRecord

        private

29
        def base
30
          ::Geo::UploadRegistry.select(file_registry_table[Arel.star])
31 32 33
        end

        def file_registry_table
34
          ::Geo::UploadRegistry.arel_table
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        end

        def fdw_upload_table
          ::Geo::Fdw::Upload.arel_table
        end

        def fdw_inner_join_uploads
          file_registry_table
            .join(fdw_upload_table, Arel::Nodes::InnerJoin)
            .on(file_registry_table[:file_id].eq(fdw_upload_table[:id]))
            .join_sources
        end
      end
    end
  end
end