Class: Gitlab::Database::TablesLocker

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/tables_locker.rb

Constant Summary collapse

GITLAB_SCHEMAS_TO_IGNORE =
i[gitlab_embedding gitlab_geo gitlab_jh].freeze

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, dry_run: false, include_partitions: true, options: {}) ⇒ TablesLocker

Returns a new instance of TablesLocker.



8
9
10
11
12
13
14
15
16
# File 'lib/gitlab/database/tables_locker.rb', line 8

def initialize(logger: nil, dry_run: false, include_partitions: true, options: {})
  @logger = logger
  @dry_run = dry_run
  @result = []
  @include_partitions = include_partitions
  @scope_to_database = options[:scope_to_database].to_s

  validate_scopes
end

Instance Method Details

#lock_writesObject

It locks the tables on the database where they don't belong. Also it unlocks the tables on the database where they belong



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitlab/database/tables_locker.rb', line 35

def lock_writes
  Gitlab::Database::EachDatabase.each_connection(include_shared: false) do |connection, database_name|
    next if skip_connection?(database_name)

    schemas_for_connection = Gitlab::Database.gitlab_schemas_for_connection(connection)

    tables_to_lock(connection) do |table_name, schema_name|
      # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/366834
      next if schema_name.in? GITLAB_SCHEMAS_TO_IGNORE

      if schemas_for_connection.include?(schema_name)
        unlock_writes_on_table(table_name, connection, database_name)
      else
        lock_writes_on_table(table_name, connection, database_name)
      end
    end
  end

  @result
end

#unlock_writesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/database/tables_locker.rb', line 18

def unlock_writes
  Gitlab::Database::EachDatabase.each_connection do |connection, database_name|
    next if skip_connection?(database_name)

    tables_to_lock(connection) do |table_name, schema_name|
      # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/366834
      next if schema_name.in? GITLAB_SCHEMAS_TO_IGNORE

      unlock_writes_on_table(table_name, connection, database_name)
    end
  end

  @result
end