summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/transaction/observer_spec.rb
blob: e5cc0106c9bd8654aa2f03da87a7b243ea510504 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::Transaction::Observer do
  # Use the delete DB strategy so that the test won't be wrapped in a transaction
  describe '.instrument_transactions', :delete do
    let(:transaction_context) { ActiveRecord::Base.connection.transaction_manager.transaction_context }
    let(:context) { transaction_context.context }

    around do |example|
      # Emulate production environment when SQL comments come first to avoid truncation
      Marginalia::Comment.prepend_comment = true
      subscriber = described_class.register!

      example.run

      ActiveSupport::Notifications.unsubscribe(subscriber)
      Marginalia::Comment.prepend_comment = false
    end

    it 'tracks transaction data', :aggregate_failures do
      ActiveRecord::Base.transaction do
        ActiveRecord::Base.transaction(requires_new: true) do
          User.first

          expect(transaction_context).to be_a(::Gitlab::Database::Transaction::Context)
          expect(context.keys).to match_array(%i(start_time depth savepoints queries backtraces))
          expect(context[:depth]).to eq(2)
          expect(context[:savepoints]).to eq(1)
          expect(context[:queries].length).to eq(1)
        end
      end

      expect(context[:depth]).to eq(2)
      expect(context[:savepoints]).to eq(1)
      expect(context[:releases]).to eq(1)
      expect(context[:backtraces].length).to eq(1)
    end

    describe '.extract_sql_command' do
      using RSpec::Parameterized::TableSyntax

      where(:sql, :expected) do
        'SELECT 1' | 'SELECT 1'
        '/* test comment */ SELECT 1' | 'SELECT 1'
        '/* test comment */ ROLLBACK TO SAVEPOINT point1' | 'ROLLBACK TO SAVEPOINT '
        'SELECT 1 /* trailing comment */' | 'SELECT 1 /* trailing comment */'
      end

      with_them do
        it do
          expect(described_class.extract_sql_command(sql)).to eq(expected)
        end
      end
    end
  end
end