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

cleanup_projects_with_bad_has_external_wiki_data_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee1f718849fd09f113cf0bb952f4fc9ff602c344 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

require 'spec_helper'

require_migration!

RSpec.describe CleanupProjectsWithBadHasExternalWikiData, :migration do
  let(:namespace) { table(:namespaces).create!(name: 'foo', path: 'bar') }
  let(:projects) { table(:projects) }
  let(:services) { table(:services) }

  def create_projects!(num)
    Array.new(num) do
      projects.create!(namespace_id: namespace.id)
    end
  end

  def create_active_external_wiki_integrations!(*projects)
    projects.each do |project|
      services.create!(type: 'ExternalWikiService', project_id: project.id, active: true)
    end
  end

  def create_disabled_external_wiki_integrations!(*projects)
    projects.each do |project|
      services.create!(type: 'ExternalWikiService', project_id: project.id, active: false)
    end
  end

  def create_active_other_integrations!(*projects)
    projects.each do |project|
      services.create!(type: 'NotAnExternalWikiService', project_id: project.id, active: true)
    end
  end

  it 'sets `projects.has_external_wiki` correctly' do
    allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false)

    project_with_external_wiki_1,
      project_with_external_wiki_2,
      project_with_disabled_external_wiki_1,
      project_with_disabled_external_wiki_2,
      project_without_external_wiki_1,
      project_without_external_wiki_2 = create_projects!(6)

    create_active_external_wiki_integrations!(
      project_with_external_wiki_1,
      project_with_external_wiki_2
    )

    create_disabled_external_wiki_integrations!(
      project_with_disabled_external_wiki_1,
      project_with_disabled_external_wiki_2
    )

    create_active_other_integrations!(
      project_without_external_wiki_1,
      project_without_external_wiki_2
    )

    # PG triggers on the services table added in a previous migration
    # will have set the `has_external_wiki` columns to correct data when
    # the services records were created above.
    #
    # We set the `has_external_wiki` columns for projects to incorrect
    # data manually below to emulate projects in a state before the PG
    # triggers were added.
    project_with_external_wiki_2.update!(has_external_wiki: false)
    project_with_disabled_external_wiki_2.update!(has_external_wiki: true)
    project_without_external_wiki_2.update!(has_external_wiki: true)

    migrate!

    expected_true = [
      project_with_external_wiki_1,
      project_with_external_wiki_2
    ].each(&:reload).map(&:has_external_wiki)

    expected_not_true = [
      project_without_external_wiki_1,
      project_without_external_wiki_2,
      project_with_disabled_external_wiki_1,
      project_with_disabled_external_wiki_2
    ].each(&:reload).map(&:has_external_wiki)

    expect(expected_true).to all(eq(true))
    expect(expected_not_true).to all(be_falsey)
  end
end