Skip to content

Conversation

@gustavohstrassburger
Copy link
Contributor

Problem

If an user deletes cohorts that are used in insights or "Filter out internal and test users", those queries will fail.

Changes

Added validations to prevent deleting cohorts that are used in:

  • Test account filters (test_account_filters)
  • Insights (query field)

How did you test this code?

Manual
Unit tests

Prevent soft deletion of cohorts that are referenced in:
- Test account filters (test_account_filters)
- Active feature flags
- Insights (query field)

This ensures data integrity by blocking deletion of cohorts that are
actively being used throughout the application.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines 673 to 676
insights_using_cohort = Insight.objects.filter(
team_id=cohort.team_id,
deleted=False,
).filter(Q(query__icontains=f'"type": "cohort", "value": {cohort.id}'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This pattern won't detect cohorts used in breakdown filters. Insights can reference cohorts in breakdownFilter like {"breakdownFilter": {"breakdown_type": "cohort", "breakdown": [123]}}, which won't match this search pattern.

Consider also checking for:

  • "breakdown_type": "cohort" combined with the cohort ID in the breakdown array
  • Or use a more robust JSON traversal approach to find all cohort references
Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/api/cohort.py
Line: 673:676

Comment:
**logic:** This pattern won't detect cohorts used in breakdown filters. Insights can reference cohorts in `breakdownFilter` like `{"breakdownFilter": {"breakdown_type": "cohort", "breakdown": [123]}}`, which won't match this search pattern.

Consider also checking for:
- `"breakdown_type": "cohort"` combined with the cohort ID in the `breakdown` array
- Or use a more robust JSON traversal approach to find all cohort references

How can I resolve this? If you propose a fix, please make it concise.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense, submitting a new commit to fix this.

Add JSONB filter to detect cohorts used in insight breakdown filters.
The previous implementation only checked properties filters, missing
cohorts referenced in breakdownFilter.breakdown arrays.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2 participants