Skip to content

Commit afccdda

Browse files
authored
__iexact reduce call has default value now. (#45206)
Addresses https://sentry.my.sentry.io/organizations/sentry/issues/392893/?project=2 by allowing empty `values` arrays and letting django orm resolve to a falsey `Q`. Interestingly, there doesn't seem to be a consistent safe way to do this other than create a Q that will always be false incidentally.
1 parent bc92124 commit afccdda

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/sentry/db/models/query.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ def in_iexact(column: str, values: Any) -> Q:
170170
from operator import or_
171171

172172
query = f"{column}__iexact"
173+
# if values is empty, have a default value for the reduce call that will essentially resolve a column in []
174+
query_in = f"{column}__in"
173175

174-
return reduce(or_, [Q(**{query: v}) for v in values])
176+
return reduce(or_, [Q(**{query: v}) for v in values], Q(**{query_in: []}))
175177

176178

177179
def in_icontains(column: str, values: Any) -> Q:

tests/sentry/utils/test_query.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
from sentry.models import Team, User
1+
from sentry.db.models.query import in_iexact
2+
from sentry.models import Organization, Team, User
23
from sentry.testutils import TestCase
34
from sentry.utils.query import RangeQuerySetWrapper, bulk_delete_objects
45

56

7+
class InIexactQueryTest(TestCase):
8+
def test_basic(self):
9+
self.create_organization(slug="SlugA")
10+
self.create_organization(slug="slugB")
11+
self.create_organization(slug="slugc")
12+
13+
assert Organization.objects.filter(in_iexact("slug", ["sluga", "slugb"])).count() == 2
14+
assert Organization.objects.filter(in_iexact("slug", ["slugC"])).count() == 1
15+
assert Organization.objects.filter(in_iexact("slug", [])).count() == 0
16+
17+
618
class RangeQuerySetWrapperTest(TestCase):
719
def test_basic(self):
820
total = 10

0 commit comments

Comments
 (0)