summaryrefslogtreecommitdiff
path: root/spec/migrations/add_pipeline_build_foreign_key_spec.rb
blob: 7358b1d265d6f68820559994ca5eb0331b0ccd3e (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
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20180420010016_add_pipeline_build_foreign_key.rb')

describe AddPipelineBuildForeignKey, :migration do
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:pipelines) { table(:ci_pipelines) }
  let(:builds) { table(:ci_builds) }

  before do
    namespaces.create(id: 10, name: 'gitlab-org', path: 'gitlab-org')
    projects.create!(id: 11, namespace_id: 10, name: 'gitlab', path: 'gitlab')
    pipelines.create!(id: 12, project_id: 11, ref: 'master', sha: 'adf43c3a')

    builds.create!(id: 101, commit_id: 12, project_id: 11)
    builds.create!(id: 102, commit_id: 222, project_id: 11)
    builds.create!(id: 103, commit_id: 333, project_id: 11)
    builds.create!(id: 104, commit_id: 12, project_id: 11)
  end

  it 'adds foreign key after removing orphans' do
    expect(builds.all.count).to eq 4
    expect(foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)).to be_falsey

    migrate!

    expect(builds.all.pluck(:id)).to eq [101, 104]
    expect(foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)).to be_truthy
  end
end