perf(captcha): simplify count() query for user ids

Using .count() isn't great as it runs a count query on a subquery which
selects all fields in the Users table. This rewrites it into a simple
SELECT count(ID) from USers query.
This commit is contained in:
Jelle van der Waa 2024-08-16 22:21:37 +02:00 committed by Leonidas Spyropoulos
parent 97cc6196eb
commit edc1ab949a

View file

@ -3,6 +3,7 @@
import hashlib import hashlib
from jinja2 import pass_context from jinja2 import pass_context
from sqlalchemy import func
from aurweb.db import query from aurweb.db import query
from aurweb.models import User from aurweb.models import User
@ -11,7 +12,8 @@ from aurweb.templates import register_filter
def get_captcha_salts(): def get_captcha_salts():
"""Produce salts based on the current user count.""" """Produce salts based on the current user count."""
count = query(User).count() count = query(func.count(User.ID)).scalar()
salts = [] salts = []
for i in range(0, 6): for i in range(0, 6):
salts.append(f"aurweb-{count - i}") salts.append(f"aurweb-{count - i}")