debian-mirror-gitlab/lib/gitlab/database/shared_model.rb

41 lines
964 B
Ruby
Raw Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
module Gitlab
module Database
2021-11-18 22:05:49 +05:30
# This abstract class is used for models which need to exist in multiple de-composed databases.
2021-11-11 11:23:49 +05:30
class SharedModel < ActiveRecord::Base
self.abstract_class = true
class << self
def using_connection(connection)
raise 'cannot nest connection overrides for shared models' unless overriding_connection.nil?
self.overriding_connection = connection
yield
ensure
self.overriding_connection = nil
end
def connection
if connection = self.overriding_connection
connection
else
super
end
end
private
def overriding_connection
Thread.current[:overriding_connection]
end
def overriding_connection=(connection)
Thread.current[:overriding_connection] = connection
end
end
end
end
end