summaryrefslogtreecommitdiff
path: root/spec/support_specs/database/prevent_cross_database_modification_spec.rb
blob: 4fd55d59db0b7e364416a4e825984953c6151a88 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Database::PreventCrossDatabaseModification' do
  let_it_be(:pipeline, refind: true) { create(:ci_pipeline) }
  let_it_be(:project, refind: true) { create(:project) }

  shared_examples 'succeessful examples' do
    context 'outside transaction' do
      it { expect { run_queries }.not_to raise_error }
    end

    context 'within transaction' do
      it do
        Project.transaction do
          expect { run_queries }.not_to raise_error
        end
      end
    end

    context 'within nested transaction' do
      it do
        Project.transaction(requires_new: true) do
          Project.transaction(requires_new: true) do
            expect { run_queries }.not_to raise_error
          end
        end
      end
    end
  end

  context 'when CI and other tables are read in a transaction' do
    def run_queries
      pipeline.reload
      project.reload
    end

    include_examples 'succeessful examples'
  end

  context 'when only CI data is modified' do
    def run_queries
      pipeline.touch
      project.reload
    end

    include_examples 'succeessful examples'
  end

  context 'when other data is modified' do
    def run_queries
      pipeline.reload
      project.touch
    end

    include_examples 'succeessful examples'
  end

  describe 'with_cross_database_modification_prevented block' do
    it 'raises error when CI and other data is modified' do
      expect do
        with_cross_database_modification_prevented do
          Project.transaction do
            project.touch
            pipeline.touch
          end
        end
      end.to raise_error /Cross-database data modification queries/
    end
  end

  context 'when running tests with prevent_cross_database_modification', :prevent_cross_database_modification do
    context 'when both CI and other data is modified' do
      def run_queries
        project.touch
        pipeline.touch
      end

      context 'outside transaction' do
        it { expect { run_queries }.not_to raise_error }
      end

      context 'when data modification happens in a transaction' do
        it 'raises error' do
          Project.transaction do
            expect { run_queries }.to raise_error /Cross-database data modification queries/
          end
        end

        context 'when data modification happens in nested transactions' do
          it 'raises error' do
            Project.transaction(requires_new: true) do
              project.touch
              Project.transaction(requires_new: true) do
                expect { pipeline.touch }.to raise_error /Cross-database data modification queries/
              end
            end
          end
        end
      end
    end

    context 'when CI association is modified through project' do
      def run_queries
        project.variables.build(key: 'a', value: 'v')
        project.save!
      end

      include_examples 'succeessful examples'
    end

    describe '#allow_cross_database_modification_within_transaction' do
      it 'skips raising error' do
        expect do
          Gitlab::Database.allow_cross_database_modification_within_transaction(url: 'gitlab-issue') do
            Project.transaction do
              pipeline.touch
              project.touch
            end
          end
        end.not_to raise_error
      end

      it 'raises error when complex factories are built referencing both databases' do
        expect do
          ApplicationRecord.transaction do
            create(:ci_pipeline)
          end
        end.to raise_error /Cross-database data modification queries/
      end

      it 'skips raising error on factory creation' do
        expect do
          Gitlab::Database.allow_cross_database_modification_within_transaction(url: 'gitlab-issue') do
            ApplicationRecord.transaction do
              create(:ci_pipeline)
            end
          end
        end.not_to raise_error
      end
    end
  end
end