Skip to content

Commit 8b4be6c

Browse files
authored
fix(n+1): fix environment-document n+1 for env_feature_version (#5332)
1 parent de1c66a commit 8b4be6c

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

api/environments/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ def _get_environment_document_from_db(
405405
"feature",
406406
"feature_state_value",
407407
"identity",
408+
"environment_feature_version",
408409
"identity__environment",
409410
).prefetch_related(
410411
Prefetch(

api/tests/unit/environments/test_unit_environments_views_sdk_environment.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import TYPE_CHECKING
22

3+
import pytest
34
from django.urls import reverse
45
from flag_engine.segments.constants import EQUAL
56
from rest_framework import status
@@ -17,6 +18,7 @@
1718
FeatureStateValue,
1819
)
1920
from features.multivariate.models import MultivariateFeatureOption
21+
from features.versioning.tasks import enable_v2_versioning
2022
from projects.models import Project
2123
from segments.models import Condition, Segment, SegmentRule
2224

@@ -26,19 +28,30 @@
2628
from organisations.models import Organisation
2729

2830

31+
@pytest.mark.parametrize(
32+
"use_v2_feature_versioning, total_queries", [(True, 16), (False, 15)]
33+
)
2934
def test_get_environment_document(
3035
organisation_one: "Organisation",
3136
organisation_two: "Organisation",
3237
organisation_one_project_one: "Project",
3338
django_assert_num_queries: "DjangoAssertNumQueries",
39+
use_v2_feature_versioning: bool,
40+
total_queries: int,
3441
) -> None:
3542
# Given
3643
project = organisation_one_project_one
3744
project2 = Project.objects.create(
3845
name="standin_project", organisation=organisation_two
3946
)
4047

41-
environment = Environment.objects.create(name="Test Environment", project=project)
48+
environment = Environment.objects.create(
49+
name="Test Environment",
50+
project=project,
51+
)
52+
if use_v2_feature_versioning:
53+
enable_v2_versioning(environment.id)
54+
4255
api_key = EnvironmentAPIKey.objects.create(environment=environment)
4356
client = APIClient()
4457
client.credentials(HTTP_X_ENVIRONMENT_KEY=api_key.key)
@@ -126,7 +139,7 @@ def test_get_environment_document(
126139
url = reverse("api-v1:environment-document")
127140

128141
# When
129-
with django_assert_num_queries(15):
142+
with django_assert_num_queries(total_queries):
130143
response = client.get(url)
131144

132145
# Then

0 commit comments

Comments
 (0)