summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/migrations/observers/query_details_spec.rb
blob: 36885a1594f212b825bde82924dec8ce96123988 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::Database::Migrations::Observers::QueryDetails do
  subject { described_class.new(observation) }

  let(:observation) { Gitlab::Database::Migrations::Observation.new(migration_version, migration_name) }
  let(:connection) { ActiveRecord::Base.connection }
  let(:query) { "select date_trunc('day', $1::timestamptz) + $2 * (interval '1 hour')" }
  let(:query_binds) { [Time.current, 3] }
  let(:directory_path) { Dir.mktmpdir }
  let(:log_file) { "#{directory_path}/#{migration_version}_#{migration_name}-query-details.json" }
  let(:query_details) { Gitlab::Json.parse(File.read(log_file)) }
  let(:migration_version) { 20210422152437 }
  let(:migration_name) { 'test' }

  before do
    stub_const('Gitlab::Database::Migrations::Instrumentation::RESULT_DIR', directory_path)
  end

  after do
    FileUtils.remove_entry(directory_path)
  end

  it 'records details of executed queries' do
    observe

    expect(query_details.size).to eq(1)

    log_entry = query_details[0]
    start_time, end_time, sql, binds = log_entry.values_at('start_time', 'end_time', 'sql', 'binds')
    start_time = DateTime.parse(start_time)
    end_time = DateTime.parse(end_time)

    aggregate_failures do
      expect(sql).to include(query)
      expect(start_time).to be_before(end_time)
      expect(binds).to eq(query_binds.map { |b| connection.type_cast(b) })
    end
  end

  it 'unsubscribes after the observation' do
    observe

    expect(subject).not_to receive(:record_sql_event)
    run_query
  end

  def observe
    subject.before
    run_query
    subject.after
    subject.record
  end

  def run_query
    connection.exec_query(query, 'SQL', query_binds)
  end
end