From 2058c572743cde4451b2bd57892cf089c8808c0e Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Tue, 22 Mar 2022 13:04:12 -0400 Subject: [PATCH] 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. --- app/lib/url.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/lib/url.rb b/app/lib/url.rb index 39ac4b654..5d4810572 100644 --- a/app/lib/url.rb +++ b/app/lib/url.rb @@ -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