Skip to content

Commit

Permalink
IdentityOverridesV2MigrationStatus -> EdgeV2MigrationStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 committed May 3, 2024
1 parent 08d4046 commit 92dd0fa
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 83 deletions.
5 changes: 2 additions & 3 deletions api/environments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from features.models import Feature, FeatureSegment, FeatureState
from features.versioning.exceptions import FeatureVersioningError
from metadata.models import Metadata
from projects.models import IdentityOverridesV2MigrationStatus, Project
from projects.models import EdgeV2MigrationStatus, Project
from segments.models import Segment
from util.mappers import map_environment_to_environment_document
from webhooks.models import AbstractBaseExportableWebhookModel
Expand Down Expand Up @@ -262,8 +262,7 @@ def write_environments_to_dynamodb(
environment_wrapper.write_environments(environments)

if (
project.identity_overrides_v2_migration_status
== IdentityOverridesV2MigrationStatus.COMPLETE
project.edge_v2_migration_status == EdgeV2MigrationStatus.COMPLETE
and environment_v2_wrapper.is_enabled
):
environment_v2_wrapper.write_environments(environments)
Expand Down
6 changes: 3 additions & 3 deletions api/features/features_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)
from features.dataclasses import EnvironmentFeatureOverridesData
from features.versioning.versioning_service import get_environment_flags_list
from projects.models import IdentityOverridesV2MigrationStatus
from projects.models import EdgeV2MigrationStatus

if typing.TYPE_CHECKING:
from environments.models import Environment
Expand All @@ -25,8 +25,8 @@ def get_overrides_data(
:return: overrides data getter
"""
project = environment.project
match project.enable_dynamo_db, project.identity_overrides_v2_migration_status:
case True, IdentityOverridesV2MigrationStatus.COMPLETE:
match project.enable_dynamo_db, project.edge_v2_migration_status:
case True, EdgeV2MigrationStatus.COMPLETE:
# If v2 migration is complete, count segment overrides from Core
# and identity overrides from DynamoDB.
return get_edge_overrides_data(environment)
Expand Down
2 changes: 1 addition & 1 deletion api/projects/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ProjectAdmin(admin.ModelAdmin):
"max_segments_allowed",
"max_features_allowed",
"max_segment_overrides_allowed",
"identity_overrides_v2_migration_status",
"edge_v2_migration_status",
)

@admin.action(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor

from projects.models import IdentityOverridesV2MigrationStatus
from projects.models import EdgeV2MigrationStatus


def apply_defaults(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
apps.get_model("projects", "Project").objects.all().update(
identity_overrides_v2_migration_status=IdentityOverridesV2MigrationStatus.NOT_STARTED
identity_overrides_v2_migration_status=EdgeV2MigrationStatus.NOT_STARTED
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.25 on 2024-05-02 12:07

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("projects", "0022_add_stale_flags_threshold_to_project"),
]

operations = [
migrations.RenameField(
model_name="project",
old_name="identity_overrides_v2_migration_status",
new_name="edge_v2_migration_status",
),
]
23 changes: 9 additions & 14 deletions api/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
environment_cache = caches[settings.ENVIRONMENT_CACHE_NAME]


class IdentityOverridesV2MigrationStatus(models.TextChoices):
class EdgeV2MigrationStatus(models.TextChoices):
NOT_STARTED = "NOT_STARTED", "Not Started"
IN_PROGRESS = "IN_PROGRESS", "In Progress"
COMPLETE = "COMPLETE", "Complete"
Expand Down Expand Up @@ -82,12 +82,12 @@ class Project(LifecycleModelMixin, SoftDeleteExportableModel):
default=100,
help_text="Max segments overrides allowed for any (one) environment within this project",
)
identity_overrides_v2_migration_status = models.CharField(
edge_v2_migration_status = models.CharField(
max_length=50,
choices=IdentityOverridesV2MigrationStatus.choices,
choices=EdgeV2MigrationStatus.choices,
# Note that the default is actually set dynamically by a lifecycle hook on create
# since we need to know whether edge is enabled or not.
default=IdentityOverridesV2MigrationStatus.NOT_STARTED,
default=EdgeV2MigrationStatus.NOT_STARTED,
)
stale_flags_limit_days = models.IntegerField(
default=30,
Expand Down Expand Up @@ -139,11 +139,9 @@ def set_enable_dynamo_db(self):
self.enable_dynamo_db = self.enable_dynamo_db or settings.EDGE_ENABLED

@hook(BEFORE_CREATE)
def set_identity_overrides_v2_migration_status(self):
def set_edge_v2_migration_status(self):
if settings.EDGE_ENABLED:
self.identity_overrides_v2_migration_status = (
IdentityOverridesV2MigrationStatus.COMPLETE
)
self.edge_v2_migration_status = EdgeV2MigrationStatus.COMPLETE

@hook(AFTER_SAVE)
def clear_environments_cache(self):
Expand All @@ -153,9 +151,9 @@ def clear_environments_cache(self):

@hook(
AFTER_SAVE,
when="identity_overrides_v2_migration_status",
when="edge_v2_migration_status",
has_changed=True,
is_now=IdentityOverridesV2MigrationStatus.IN_PROGRESS,
is_now=EdgeV2MigrationStatus.IN_PROGRESS,
)
def trigger_environments_v2_migration(self) -> None:
migrate_project_environments_to_v2.delay(kwargs={"project_id": self.id})
Expand Down Expand Up @@ -194,10 +192,7 @@ def is_feature_name_valid(self, feature_name: str) -> bool:

@property
def show_edge_identity_overrides_for_feature(self) -> bool:
return (
self.identity_overrides_v2_migration_status
== IdentityOverridesV2MigrationStatus.COMPLETE
)
return self.edge_v2_migration_status == EdgeV2MigrationStatus.COMPLETE


class ProjectPermissionManager(models.Manager):
Expand Down
6 changes: 2 additions & 4 deletions api/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ def write_environments_to_dynamodb(project_id: int) -> None:
@register_task_handler()
def migrate_project_environments_to_v2(project_id: int) -> None:
from environments.dynamodb.services import migrate_environments_to_v2
from projects.models import IdentityOverridesV2MigrationStatus, Project
from projects.models import EdgeV2MigrationStatus, Project

with transaction.atomic():
project = Project.objects.select_for_update().get(id=project_id)
if migrate_environments_to_v2(project_id=project_id):
project.identity_overrides_v2_migration_status = (
IdentityOverridesV2MigrationStatus.COMPLETE
)
project.edge_v2_migration_status = EdgeV2MigrationStatus.COMPLETE
project.save()


Expand Down
22 changes: 8 additions & 14 deletions api/tests/unit/environments/test_unit_environments_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from features.multivariate.models import MultivariateFeatureOption
from features.versioning.models import EnvironmentFeatureVersion
from organisations.models import Organisation, OrganisationRole
from projects.models import IdentityOverridesV2MigrationStatus, Project
from projects.models import EdgeV2MigrationStatus, Project
from segments.models import Segment
from util.mappers import map_environment_to_environment_document

Expand Down Expand Up @@ -500,9 +500,7 @@ def test_write_environments_to_dynamodb__project_environments_v2_migrated__call_
mock_dynamo_env_v2_wrapper: Mock,
) -> None:
# Given
dynamo_enabled_project.identity_overrides_v2_migration_status = (
IdentityOverridesV2MigrationStatus.COMPLETE
)
dynamo_enabled_project.edge_v2_migration_status = EdgeV2MigrationStatus.COMPLETE
dynamo_enabled_project.save()
mock_dynamo_env_v2_wrapper.is_enabled = True

Expand All @@ -527,9 +525,7 @@ def test_write_environments_to_dynamodb__project_environments_v2_migrated__wrapp
) -> None:
# Given
mock_dynamo_env_v2_wrapper.is_enabled = False
dynamo_enabled_project.identity_overrides_v2_migration_status = (
IdentityOverridesV2MigrationStatus.COMPLETE
)
dynamo_enabled_project.edge_v2_migration_status = EdgeV2MigrationStatus.COMPLETE
dynamo_enabled_project.save()

# When
Expand All @@ -540,10 +536,10 @@ def test_write_environments_to_dynamodb__project_environments_v2_migrated__wrapp


@pytest.mark.parametrize(
"identity_overrides_v2_migration_status",
"edge_v2_migration_status",
(
IdentityOverridesV2MigrationStatus.NOT_STARTED,
IdentityOverridesV2MigrationStatus.IN_PROGRESS,
EdgeV2MigrationStatus.NOT_STARTED,
EdgeV2MigrationStatus.IN_PROGRESS,
),
)
def test_write_environments_to_dynamodb__project_environments_v2_not_migrated__wrapper_not_called(
Expand All @@ -552,12 +548,10 @@ def test_write_environments_to_dynamodb__project_environments_v2_not_migrated__w
dynamo_enabled_project_environment_two: Environment,
mock_dynamo_env_wrapper: Mock,
mock_dynamo_env_v2_wrapper: Mock,
identity_overrides_v2_migration_status: str,
edge_v2_migration_status: str,
) -> None:
# Given
dynamo_enabled_project.identity_overrides_v2_migration_status = (
identity_overrides_v2_migration_status
)
dynamo_enabled_project.edge_v2_migration_status = edge_v2_migration_status
dynamo_enabled_project.save()
mock_dynamo_env_v2_wrapper.is_enabled = True

Expand Down
28 changes: 12 additions & 16 deletions api/tests/unit/features/test_unit_features_features_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
get_overrides_data,
)
from features.models import Feature, FeatureSegment, FeatureState
from projects.models import IdentityOverridesV2MigrationStatus
from projects.models import EdgeV2MigrationStatus
from util.mappers.engine import (
map_feature_state_to_engine,
map_identity_to_engine,
Expand Down Expand Up @@ -60,23 +60,23 @@ def distinct_identity_featurestate(


@pytest.mark.parametrize(
"enable_dynamo_db, identity_overrides_v2_migration_status, expected_overrides_getter_name, expected_kwargs",
"enable_dynamo_db, edge_v2_migration_status, expected_overrides_getter_name, expected_kwargs",
[
(
True,
IdentityOverridesV2MigrationStatus.NOT_STARTED,
EdgeV2MigrationStatus.NOT_STARTED,
"get_core_overrides_data",
{"skip_identity_overrides": True},
),
(
True,
IdentityOverridesV2MigrationStatus.IN_PROGRESS,
EdgeV2MigrationStatus.IN_PROGRESS,
"get_core_overrides_data",
{"skip_identity_overrides": True},
),
(
True,
IdentityOverridesV2MigrationStatus.COMPLETE,
EdgeV2MigrationStatus.COMPLETE,
"get_edge_overrides_data",
{},
),
Expand All @@ -92,7 +92,7 @@ def test_feature_get_overrides_data__call_expected(
mocker: "MockerFixture",
environment: "Environment",
enable_dynamo_db: bool,
identity_overrides_v2_migration_status: str,
edge_v2_migration_status: str,
expected_overrides_getter_name: str,
expected_kwargs: dict[str, bool],
) -> None:
Expand All @@ -108,9 +108,7 @@ def test_feature_get_overrides_data__call_expected(
),
}
environment.project.enable_dynamo_db = enable_dynamo_db
environment.project.identity_overrides_v2_migration_status = (
identity_overrides_v2_migration_status
)
environment.project.edge_v2_migration_status = edge_v2_migration_status

# When
get_overrides_data(environment)
Expand All @@ -125,23 +123,21 @@ def test_feature_get_overrides_data__call_expected(


@pytest.mark.parametrize(
"identity_overrides_v2_migration_status",
"edge_v2_migration_status",
[
IdentityOverridesV2MigrationStatus.NOT_STARTED,
IdentityOverridesV2MigrationStatus.IN_PROGRESS,
EdgeV2MigrationStatus.NOT_STARTED,
EdgeV2MigrationStatus.IN_PROGRESS,
],
)
def test_feature_get_overrides_data__edge_project_not_migrated_to_v2__return_expected(
environment: "Environment",
distinct_identity_featurestate: FeatureState,
distinct_segment_featurestate: FeatureState,
identity_overrides_v2_migration_status: str,
edge_v2_migration_status: str,
) -> None:
# Given
environment.project.enable_dynamo_db = True
environment.project.identity_overrides_v2_migration_status = (
identity_overrides_v2_migration_status
)
environment.project.edge_v2_migration_status = edge_v2_migration_status

# When
overrides_data = get_overrides_data(environment)
Expand Down
21 changes: 9 additions & 12 deletions api/tests/unit/projects/test_unit_projects_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pytest_django.fixtures import SettingsWrapper

from organisations.models import Organisation
from projects.models import IdentityOverridesV2MigrationStatus, Project
from projects.models import EdgeV2MigrationStatus, Project

now = timezone.now()
tomorrow = now + timedelta(days=1)
Expand Down Expand Up @@ -139,26 +139,26 @@ def test_environments_are_updated_in_dynamodb_when_project_id_updated(


@pytest.mark.parametrize(
"identity_overrides_v2_migration_status, expected_value",
"edge_v2_migration_status, expected_value",
(
(IdentityOverridesV2MigrationStatus.NOT_STARTED, False),
(IdentityOverridesV2MigrationStatus.IN_PROGRESS, False),
(IdentityOverridesV2MigrationStatus.COMPLETE, True),
(EdgeV2MigrationStatus.NOT_STARTED, False),
(EdgeV2MigrationStatus.IN_PROGRESS, False),
(EdgeV2MigrationStatus.COMPLETE, True),
),
)
def test_show_edge_identity_overrides_for_feature(
identity_overrides_v2_migration_status: IdentityOverridesV2MigrationStatus,
edge_v2_migration_status: EdgeV2MigrationStatus,
expected_value: bool,
):
assert (
Project(
identity_overrides_v2_migration_status=identity_overrides_v2_migration_status
edge_v2_migration_status=edge_v2_migration_status
).show_edge_identity_overrides_for_feature
== expected_value
)


def test_create_project_sets_identity_overrides_v2_migration_status_if_edge_enabled(
def test_create_project_sets_edge_v2_migration_status_if_edge_enabled(
settings: SettingsWrapper, organisation: Organisation
) -> None:
# Given
Expand All @@ -168,7 +168,4 @@ def test_create_project_sets_identity_overrides_v2_migration_status_if_edge_enab
project = Project.objects.create(name="test", organisation=organisation)

# Then
assert (
project.identity_overrides_v2_migration_status
== IdentityOverridesV2MigrationStatus.COMPLETE
)
assert project.edge_v2_migration_status == EdgeV2MigrationStatus.COMPLETE
Loading

0 comments on commit 92dd0fa

Please sign in to comment.