2018-10-19 08:17:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Shard < ApplicationRecord
|
2018-10-19 08:17:50 -04:00
|
|
|
# Store shard names from the configuration file in the database. This is not a
|
|
|
|
# list of active shards - we just want to assign an immutable, unique ID to
|
|
|
|
# every shard name for easy indexing / referencing.
|
|
|
|
def self.populate!
|
|
|
|
return unless table_exists?
|
|
|
|
|
|
|
|
# The GitLab config does not change for the lifecycle of the process
|
|
|
|
in_config = Gitlab.config.repositories.storages.keys.map(&:to_s)
|
2018-11-13 15:41:45 -05:00
|
|
|
in_db = all.pluck(:name)
|
2018-10-19 08:17:50 -04:00
|
|
|
|
2018-11-13 15:41:45 -05:00
|
|
|
# This may race with other processes creating shards at the same time, but
|
|
|
|
# `by_name` will handle that correctly
|
|
|
|
missing = in_config - in_db
|
|
|
|
missing.map { |name| by_name(name) }
|
2018-10-19 08:17:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.by_name(name)
|
2021-08-20 02:11:03 -04:00
|
|
|
safe_find_or_create_by(name: name)
|
2018-10-19 08:17:50 -04:00
|
|
|
end
|
|
|
|
end
|