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

track_inconsistency_spec.rb « schema_validation « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b104e40c111315c676554d36e9b1c30cfcbf7d6 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::SchemaValidation::TrackInconsistency, feature_category: :database do
  describe '#execute' do
    let(:validator) { Gitlab::Database::SchemaValidation::Validators::DifferentDefinitionIndexes }

    let(:database_statement) { 'CREATE INDEX index_name ON public.achievements USING btree (namespace_id)' }
    let(:structure_sql_statement) { 'CREATE INDEX index_name ON public.achievements USING btree (id)' }

    let(:structure_stmt) { PgQuery.parse(structure_sql_statement).tree.stmts.first.stmt.index_stmt }
    let(:database_stmt) { PgQuery.parse(database_statement).tree.stmts.first.stmt.index_stmt }

    let(:structure_sql_object) { Gitlab::Database::SchemaValidation::SchemaObjects::Index.new(structure_stmt) }
    let(:database_object) { Gitlab::Database::SchemaValidation::SchemaObjects::Index.new(database_stmt) }

    let(:inconsistency) do
      Gitlab::Database::SchemaValidation::Inconsistency.new(validator, structure_sql_object, database_object)
    end

    let_it_be(:project) { create(:project) }
    let_it_be(:user) { create(:user) }

    subject(:execute) { described_class.new(inconsistency, project, user).execute }

    context 'when is not GitLab.com' do
      it 'does not create a schema inconsistency record' do
        allow(Gitlab).to receive(:com?).and_return(false)

        expect { execute }.not_to change { Gitlab::Database::SchemaValidation::SchemaInconsistency.count }
      end
    end

    context 'when the issue creation fails' do
      let(:issue_creation) { instance_double(Mutations::Issues::Create, resolve: { errors: 'error' }) }

      let(:convert_object) do
        instance_double('Gitlab::Database::ConvertFeatureCategoryToGroupLabel', execute: 'group_label')
      end

      before do
        allow(Gitlab::Database::ConvertFeatureCategoryToGroupLabel).to receive(:new).and_return(convert_object)
        allow(Mutations::Issues::Create).to receive(:new).and_return(issue_creation)
      end

      it 'does not create a schema inconsistency record' do
        allow(Gitlab).to receive(:com?).and_return(true)

        expect { execute }.not_to change { Gitlab::Database::SchemaValidation::SchemaInconsistency.count }
      end
    end

    context 'when a new inconsistency is found' do
      let(:convert_object) do
        instance_double('Gitlab::Database::ConvertFeatureCategoryToGroupLabel', execute: 'group_label')
      end

      before do
        allow(Gitlab::Database::ConvertFeatureCategoryToGroupLabel).to receive(:new).and_return(convert_object)
        project.add_developer(user)
      end

      it 'creates a new schema inconsistency record' do
        allow(Gitlab).to receive(:com?).and_return(true)

        expect { execute }.to change { Gitlab::Database::SchemaValidation::SchemaInconsistency.count }
      end
    end

    context 'when the schema inconsistency already exists' do
      let(:diff) do
        "-#{structure_sql_statement}\n" \
          "+#{database_statement}\n"
      end

      let!(:schema_inconsistency) do
        create(:schema_inconsistency, object_name: 'index_name', table_name: 'achievements',
          valitador_name: 'different_definition_indexes', diff: diff)
      end

      before do
        project.add_developer(user)
      end

      context 'when the issue has the last schema inconsistency' do
        it 'does not add a note' do
          allow(Gitlab).to receive(:com?).and_return(true)

          expect { execute }.not_to change { schema_inconsistency.issue.notes.count }
        end
      end

      context 'when the issue is outdated' do
        let!(:schema_inconsistency) do
          create(:schema_inconsistency, object_name: 'index_name', table_name: 'achievements',
            valitador_name: 'different_definition_indexes', diff: 'old_diff')
        end

        it 'adds a note' do
          allow(Gitlab).to receive(:com?).and_return(true)

          expect { execute }.to change { schema_inconsistency.issue.notes.count }.from(0).to(1)
        end

        it 'updates the diff' do
          allow(Gitlab).to receive(:com?).and_return(true)

          execute

          expect(schema_inconsistency.reload.diff).to eq(diff)
        end
      end

      context 'when the GitLab issue is open' do
        it 'does not create a new schema inconsistency record' do
          allow(Gitlab).to receive(:com?).and_return(true)
          schema_inconsistency.issue.update!(state_id: Issue.available_states[:opened])

          expect { execute }.not_to change { Gitlab::Database::SchemaValidation::SchemaInconsistency.count }
        end
      end

      context 'when the GitLab is not open' do
        let(:convert_object) do
          instance_double('Gitlab::Database::ConvertFeatureCategoryToGroupLabel', execute: 'group_label')
        end

        before do
          allow(Gitlab::Database::ConvertFeatureCategoryToGroupLabel).to receive(:new).and_return(convert_object)
          project.add_developer(user)
        end

        it 'creates a new schema inconsistency record' do
          allow(Gitlab).to receive(:com?).and_return(true)
          schema_inconsistency.issue.update!(state_id: Issue.available_states[:closed])

          expect { execute }.to change { Gitlab::Database::SchemaValidation::SchemaInconsistency.count }
        end
      end
    end

    context 'when the dictionary file is not present' do
      before do
        allow(Gitlab::Database::GitlabSchema).to receive(:dictionary_paths).and_return(['dictionary_not_found_path/'])

        project.add_developer(user)
      end

      it 'add the default labels' do
        allow(Gitlab).to receive(:com?).and_return(true)

        inconsistency = execute

        labels = inconsistency.issue.labels.map(&:name)

        expect(labels).to eq %w[database database-inconsistency-report type::maintenance severity::4]
      end
    end

    context 'when dictionary feature_categories are available' do
      let(:convert_object) do
        instance_double('Gitlab::Database::ConvertFeatureCategoryToGroupLabel', execute: 'group_label')
      end

      before do
        allow(Gitlab::Database::ConvertFeatureCategoryToGroupLabel).to receive(:new).and_return(convert_object)

        allow(Gitlab::Database::GitlabSchema).to receive(:dictionary_paths).and_return(['spec/fixtures/'])

        project.add_developer(user)
      end

      it 'add the default labels + group labels' do
        allow(Gitlab).to receive(:com?).and_return(true)

        inconsistency = execute

        labels = inconsistency.issue.labels.map(&:name)

        expect(labels).to eq %w[database database-inconsistency-report type::maintenance severity::4 group_label]
      end
    end
  end
end