File: prevent_cross_database_modification.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (78 lines) | stat: -rw-r--r-- 2,841 bytes parent folder | download
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
# frozen_string_literal: true

module Database
  module PreventCrossDatabaseModificationSpecHelpers
    delegate :with_cross_database_modification_prevented,
      :allow_cross_database_modification_within_transaction,
      to: :'::Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification'
  end

  module AllowCrossDatabaseFactoryBotBuilt
    extend ActiveSupport::Concern

    attr_accessor :factory_bot_built

    prepended do
      around_create :_test_ignore_table_in_transaction, prepend: true, if: :factory_bot_built?

      def _test_ignore_table_in_transaction(&blk)
        Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.temporary_ignore_tables_in_transaction(
          [self.class.table_name], url: 'https://gitlab.com/gitlab-org/gitlab/-/merge_requests/130277', &blk
        )
      end
    end

    def factory_bot_built?
      return false unless Rails.env.test?

      !!factory_bot_built
    end

    private

    def ignore_cross_database_tables_if_factory_bot(tables, &blk)
      return super unless factory_bot_built?

      Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.temporary_ignore_tables_in_transaction(
        tables,
        url: 'https://gitlab.com/gitlab-org/gitlab/-/merge_requests/130277',
        &blk
      )
    end
  end
end

ActiveRecord::Base.prepend(Database::AllowCrossDatabaseFactoryBotBuilt)

CROSS_DB_MODIFICATION_ALLOW_LIST = Set.new(YAML.load_file(File.join(__dir__, 'cross-database-modification-allowlist.yml'))).freeze

RSpec.configure do |config|
  config.include(Database::PreventCrossDatabaseModificationSpecHelpers)

  # By default allow cross-modifications as we want to observe only transactions
  # within a specific block of execution which is defined be `before(:each)` and `after(:each)`
  config.before(:all) do
    ::Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.suppress_in_rspec = true
  end

  # Using before and after blocks because the around block causes problems with the let_it_be
  # record creations. It makes an extra savepoint which breaks the transaction count logic.
  config.before do |example_file|
    ::Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.suppress_in_rspec =
      CROSS_DB_MODIFICATION_ALLOW_LIST.include?(example_file.file_path_rerun_argument)
  end

  # Reset after execution to preferred state
  config.after do |_example_file|
    ::Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.suppress_in_rspec = true

    ::ApplicationRecord.gitlab_transactions_stack.clear
  end

  config.before(:suite) do
    ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |_name, _start, _finish, _id, payload|
      strategy = payload[:strategy]
      Thread.current[:factory_bot_objects] -= 1 if strategy == :create
    end
  end
end