Welcome to mirror list, hosted at ThFree Co, Russian Federation.

joins_spec.rb « partitioning « gitlab_patches « active_record « spec « activerecord-gitlab « gems - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 038fae436448279969f24500e436af9b3d0e6ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

RSpec.describe 'ActiveRecord::GitlabPatches::Partitioning::Associations::Joins', :partitioning do
  let!(:pipeline) { Pipeline.create!(partition_id: 100) }
  let!(:job) { Job.create!(pipeline: pipeline, partition_id: pipeline.partition_id) }
  let!(:metadata) { Metadata.create!(job: job, partition_id: job.partition_id) }

  it 'joins using partition_id' do
    join_statement = <<~SQL.squish
      SELECT \"pipelines\".*
      FROM \"pipelines\"
      INNER JOIN \"jobs\" ON \"jobs\".\"pipeline_id\" = \"pipelines\".\"id\"
      AND \"jobs\".\"partition_id\" = \"pipelines\".\"partition_id\"
      WHERE \"pipelines\".\"partition_id\" = #{pipeline.partition_id}
    SQL

    result = QueryRecorder.log do
      Pipeline.where(partition_id: pipeline.partition_id).joins(:jobs).to_a
    end

    expect(result).to include(join_statement)
  end

  it 'joins other models using partition_id' do
    join_statement = <<~SQL.squish
      SELECT \"pipelines\".*
      FROM \"pipelines\"
      INNER JOIN \"jobs\" ON \"jobs\".\"pipeline_id\" = \"pipelines\".\"id\"
      AND \"jobs\".\"partition_id\" = \"pipelines\".\"partition_id\"
      INNER JOIN \"metadata\" ON \"metadata\".\"job_id\" = \"jobs\".\"id\"
      AND \"metadata\".\"partition_id\" = \"jobs\".\"partition_id\"
      WHERE \"pipelines\".\"partition_id\" = #{pipeline.partition_id}
    SQL

    result = QueryRecorder.log do
      Pipeline.where(partition_id: pipeline.partition_id).joins(jobs: :metadata).to_a
    end

    expect(result).to include(join_statement)
  end
end