Skip to content
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

fix(flags): Update feature flag insights when feature flag key changes #27340

Merged
merged 12 commits into from
Jan 9, 2025
Merged
15 changes: 15 additions & 0 deletions posthog/api/feature_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def update(self, instance: FeatureFlag, validated_data: dict, *args: Any, **kwar
request = self.context["request"]
validated_key = validated_data.get("key", None)
if validated_key:
# Delete any soft deleted feature flags with the same key to prevent conflicts
FeatureFlag.objects.filter(
key=validated_key, team__project_id=instance.team.project_id, deleted=True
).delete()
Expand All @@ -396,6 +397,8 @@ def update(self, instance: FeatureFlag, validated_data: dict, *args: Any, **kwar
for dashboard in analytics_dashboards:
FeatureFlagDashboards.objects.get_or_create(dashboard=dashboard, feature_flag=instance)

old_key = instance.key

instance = super().update(instance, validated_data)

# Propagate the new variants and aggregation group type index to the linked experiments
Expand All @@ -415,6 +418,9 @@ def update(self, instance: FeatureFlag, validated_data: dict, *args: Any, **kwar
experiment.parameters.pop("aggregation_group_type_index", None)
experiment.save()

if old_key != instance.key:
_update_feature_flag_dashboard(instance, old_key)

report_user_action(request.user, "feature flag updated", instance.get_analytics_metadata())

return instance
Expand Down Expand Up @@ -446,6 +452,15 @@ def _create_usage_dashboard(feature_flag: FeatureFlag, user):
return usage_dashboard


def _update_feature_flag_dashboard(feature_flag: FeatureFlag, old_key: str) -> None:
from posthog.helpers.dashboard_templates import update_feature_flag_dashboard

if not old_key:
return

update_feature_flag_dashboard(feature_flag, old_key)


class MinimalFeatureFlagSerializer(serializers.ModelSerializer):
filters = serializers.DictField(source="get_filters", required=False)

Expand Down
Loading
Loading