Optimize URL.database_available? (#16968)

This method was making a DB query every time. Chances are that if the
table is there the first time, it'll continue to exist. This is an
assumption generally baked into ActiveRecord - it caches the schema for
every table when its corresponding model is used and never checks it
again for the life of the process.
This commit is contained in:
Jamie Gaskins 2022-03-22 13:04:12 -04:00 committed by GitHub
parent 73ff4169e8
commit 2058c57274
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,11 +5,17 @@ module URL
end
def self.database_available?
ActiveRecord::Base.connected? && ActiveRecord::Base.connection.table_exists?("site_configs")
ActiveRecord::Base.connected? && has_site_configs?
end
private_class_method :database_available?
def self.has_site_configs?
@has_site_configs ||= ActiveRecord::Base.connection.table_exists?("site_configs")
end
private_class_method :has_site_configs?
def self.domain
if database_available?
Settings::General.app_domain