PERF: Avoid running a pointless PG query when theme has no variables. (#13342)

PERF: Avoid running a pointless PG query when theme has no variables. (#13342)

When Theme#all_theme_variables returns an empty array, we were running a pointless query in StyleSheet::Manager#uploads_digest.

SELECT "sha1" FROM "theme_fields" INNER JOIN "uploads" ON "uploads"."id" = "theme_fields"."upload_id" WHERE 1=0

diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb
index f40e212..9c719ef 100644
--- a/lib/stylesheet/manager.rb
+++ b/lib/stylesheet/manager.rb
@@ -434,7 +434,18 @@ class Stylesheet::Manager
   end
 
   def uploads_digest
-    Digest::SHA1.hexdigest(ThemeField.joins(:upload).where(id: theme&.all_theme_variables).pluck(:sha1).join(","))
+    sha1s =
+      if (theme_ids = theme&.all_theme_variables).present?
+        ThemeField
+          .joins(:upload)
+          .where(id: theme_ids)
+          .pluck(:sha1)
+          .join(",")
+      else
+        ""
+      end
+
+      Digest::SHA1.hexdigest(sha1s)
   end
 
   def color_scheme_digest

GitHub sha: 2a4a20ad67e90055d66cf3680da1a03afbe19728

This commit appears in #13342 which was approved by eviltrout. It was merged by SamSaffron.