-
Notifications
You must be signed in to change notification settings - Fork 515
fix(Import/Export): Destructive environment-level import resets feature states in other environments #7349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix(Import/Export): Destructive environment-level import resets feature states in other environments #7349
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d0e045d
fix(Import/Export): Destructive environment-level import resets featu…
khvn26 f3938ec
fix(Import/Export): Remove unused type-ignore comments
khvn26 ae06819
refactor(Import/Export): Extract import strategy logic into services …
khvn26 3ed0ca7
fix(Import/Export): Destructive import is invisible to live state in …
khvn26 9bf3353
refactor(Import/Export): Replace per-concern unit tests with end-to-e…
khvn26 e846b5f
refactor(Import/Export): Drop new-version-on-destructive-import; muta…
khvn26 e27a0ac
test(Import/Export): Cover destructive multivariate reconciliation
khvn26 f85ac67
refactor(Import/Export): Drop create_feature_for_environment; orchest…
khvn26 df5a068
docs(Import/Export): Update destructive-import warnings to reflect en…
khvn26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from features.import_export.types import FeatureExportData | ||
| from features.models import Feature | ||
| from projects.models import Project | ||
|
|
||
|
|
||
| def map_feature_export_data_to_feature( | ||
| feature_data: FeatureExportData, project: Project | ||
| ) -> Feature: | ||
| return Feature( | ||
| name=feature_data["name"], | ||
| project=project, | ||
| initial_value=feature_data["initial_value"], | ||
| is_server_key_only=feature_data["is_server_key_only"], | ||
| default_enabled=feature_data["default_enabled"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| from django.db.models import Q | ||
|
|
||
| from environments.models import Environment | ||
| from features.import_export.types import FeatureExportData | ||
| from features.models import Feature, FeatureSegment, FeatureState | ||
| from features.multivariate.models import MultivariateFeatureOption | ||
|
|
||
|
|
||
| def overwrite_feature_for_environment( | ||
| feature_data: FeatureExportData, | ||
| existing_feature: Feature, | ||
| environment: Environment, | ||
| ) -> None: | ||
| """ | ||
| Apply a destructive feature import to a single environment without | ||
| affecting other environments' feature states or the Feature definition. | ||
| """ | ||
| FeatureSegment.objects.filter( | ||
| feature=existing_feature, environment=environment | ||
| ).delete() | ||
| existing_feature.feature_states.filter( | ||
| environment=environment, identity__isnull=False | ||
| ).delete() | ||
|
|
||
| feature_state = FeatureState.objects.get_live_feature_states( | ||
| environment=environment, | ||
| additional_filters=Q( | ||
| feature=existing_feature, | ||
| identity__isnull=True, | ||
| feature_segment__isnull=True, | ||
| ), | ||
| ).get() | ||
|
|
||
| existing_options_by_value: dict[ | ||
| tuple[str | None, str | int | bool | None], MultivariateFeatureOption | ||
| ] = { | ||
| (option.type, option.value): option | ||
| for option in existing_feature.multivariate_options.all() | ||
| } | ||
| imported_option_ids: set[int] = set() | ||
| for mv_data in feature_data["multivariate"]: | ||
| key = (mv_data["type"], mv_data["value"]) | ||
| mv_option = existing_options_by_value.get(key) | ||
| if mv_option is None: | ||
| mv_option = MultivariateFeatureOption( | ||
| feature=existing_feature, | ||
| default_percentage_allocation=mv_data["default_percentage_allocation"], | ||
| type=mv_data["type"], | ||
| ) | ||
| setattr( | ||
| mv_option, | ||
| FeatureState.get_feature_state_key_name(mv_data["type"]), | ||
| mv_data["value"], | ||
| ) | ||
| mv_option.save() | ||
| imported_option_ids.add(mv_option.pk) | ||
| mv_state_value = feature_state.multivariate_feature_state_values.get( | ||
| multivariate_feature_option=mv_option, | ||
| ) | ||
| mv_state_value.percentage_allocation = mv_data["percentage_allocation"] | ||
| mv_state_value.save() | ||
|
|
||
| for mv_state_value in feature_state.multivariate_feature_state_values.exclude( | ||
| multivariate_feature_option_id__in=imported_option_ids, | ||
| ): | ||
| if mv_state_value.percentage_allocation != 0: | ||
| mv_state_value.percentage_allocation = 0 | ||
| mv_state_value.save() | ||
|
|
||
| feature_state_value = feature_state.feature_state_value | ||
| feature_state_value.type = feature_data["type"] | ||
| setattr( | ||
| feature_state_value, | ||
| FeatureState.get_feature_state_key_name(feature_data["type"]), | ||
| feature_data["value"], | ||
| ) | ||
| feature_state_value.save() | ||
| feature_state.enabled = feature_data["enabled"] | ||
| feature_state.save() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from typing import TypedDict | ||
|
|
||
|
|
||
| class FeatureExportMultivariateData(TypedDict): | ||
| percentage_allocation: float | ||
| default_percentage_allocation: float | ||
| value: str | int | bool | None | ||
| type: str | ||
|
|
||
|
|
||
| class FeatureExportData(TypedDict): | ||
| name: str | ||
| default_enabled: bool | ||
| is_server_key_only: bool | ||
| initial_value: str | None | ||
| value: str | int | bool | None | ||
| type: str | ||
| enabled: bool | ||
| multivariate: list[FeatureExportMultivariateData] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.