diff --git a/api/features/versioning/tasks.py b/api/features/versioning/tasks.py index 5850c369d0c0..c5d8e1748ce4 100644 --- a/api/features/versioning/tasks.py +++ b/api/features/versioning/tasks.py @@ -109,7 +109,9 @@ def _create_initial_feature_versions(environment: "Environment"): change_request__isnull=False, change_request__committed_at__isnull=False, change_request__deleted_at__isnull=True, + environment=environment, ).select_related("change_request") + for feature_state in scheduled_feature_states: ef_version = EnvironmentFeatureVersion.objects.create( feature=feature, diff --git a/api/tests/unit/features/versioning/test_unit_versioning_tasks.py b/api/tests/unit/features/versioning/test_unit_versioning_tasks.py index bc701c2156b9..2ae8ce082a56 100644 --- a/api/tests/unit/features/versioning/test_unit_versioning_tasks.py +++ b/api/tests/unit/features/versioning/test_unit_versioning_tasks.py @@ -32,6 +32,7 @@ get_environment_flags_queryset, ) from features.workflows.core.models import ChangeRequest +from organisations.models import Organisation from projects.models import Project from segments.models import Segment from users.models import FFAdminUser @@ -233,6 +234,36 @@ def test_enable_v2_versioning_for_scheduled_changes( version=None, ) + # and a published, scheduled feature state associated with a different project + # Note: this additional test clause is to verify an issue found in testing + # where enabling feature versioning 'stole' scheduled feature states from other + # projects. + another_organisation = Organisation.objects.create(name="another organisation") + staff_user.add_organisation(another_organisation) + another_project = Project.objects.create( + name="another project", organisation=another_organisation + ) + another_environment = Environment.objects.create( + name="another environment", project=another_project + ) + another_feature = Feature.objects.create( + name="another_feature", project=another_project + ) + published_scheduled_cr_another_environment = ChangeRequest.objects.create( + environment=another_environment, + title="Published, scheduled change in another environment", + user=staff_user, + ) + another_environment_fs = FeatureState.objects.create( + feature=another_feature, + enabled=True, + environment=another_environment, + live_from=two_hours_from_now, + change_request=published_scheduled_cr_another_environment, + version=None, + ) + published_scheduled_cr_another_environment.commit(staff_user) + # When enable_v2_versioning(environment.id) @@ -260,6 +291,13 @@ def test_enable_v2_versioning_for_scheduled_changes( == scheduled_feature_state ) + another_environment_fs.refresh_from_db() + assert another_environment_fs.environment_feature_version is None + assert ( + another_environment_fs.change_request + == published_scheduled_cr_another_environment + ) + def test_publish_version_change_set_sends_email_to_change_request_owner_if_conflicts_when_scheduled( feature: Feature,