Skip to content

Commit 1d2b24f

Browse files
authored
simple list: offload dictionary build to postgres (#17724)
1 parent ef9978c commit 1d2b24f

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

warehouse/packaging/utils.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import packaging_legacy.version
1818

1919
from pyramid_jinja2 import IJinja2Environment
20+
from sqlalchemy import String, cast, func, select
2021
from sqlalchemy.orm import joinedload
2122

2223
from warehouse.packaging.interfaces import ISimpleStorage
@@ -26,22 +27,32 @@
2627

2728

2829
def _simple_index(request, serial):
29-
# Fetch the name and normalized name for all of our projects
30-
projects = (
31-
request.db.query(Project.name, Project.normalized_name, Project.last_serial)
32-
# Exclude projects that are in the `quarantine-enter` lifecycle status.
33-
# Use `is_distinct_from` method here to ensure that we select `NULL` records,
34-
# which would otherwise be excluded by the `==` operator.
35-
.filter(
36-
Project.lifecycle_status.is_distinct_from(LifecycleStatus.QuarantineEnter)
30+
# Fetch the name and last serial name for all of our projects
31+
query = select(
32+
func.array(
33+
select(
34+
func.json_build_object(
35+
cast("name", String),
36+
Project.name,
37+
cast("_last-serial", String),
38+
Project.last_serial,
39+
)
40+
)
41+
# Exclude projects that are in the `quarantine-enter` lifecycle status.
42+
# Use `is_distinct_from` method here to ensure that we select `NULL`
43+
# records, which would otherwise be excluded by the `==` operator.
44+
.filter(
45+
Project.lifecycle_status.is_distinct_from(
46+
LifecycleStatus.QuarantineEnter
47+
)
48+
).order_by(Project.normalized_name)
3749
)
38-
.order_by(Project.normalized_name)
39-
.all()
4050
)
51+
projects = request.db.execute(query).scalar() or []
4152

4253
return {
4354
"meta": {"api-version": API_VERSION, "_last-serial": serial},
44-
"projects": [{"name": p.name, "_last-serial": p.last_serial} for p in projects],
55+
"projects": projects,
4556
}
4657

4758

0 commit comments

Comments
 (0)