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`
This commit is contained in:
Alan Guo Xiang Tan 2021-06-10 08:50:17 +08:00 committed by GitHub
parent 0ae8640650
commit 2a4a20ad67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 1 deletions

View File

@ -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