Flagsmith / Flagsmith/flagsmith

Epic: Store segment rules and conditions as JSON

Open
#7,814 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
6.6k
Forks
567
Avg merge
1d 13h
Merged PRs (30d)
121

Description

Supersedes https://github.com/Flagsmith/flagsmith/issues/6946.

Migrate off the `SegmentRule` and `Condition` tables to a single JSON field on `Segment`. The goal is to simplify all segment writes, streamline segment versioning, and make segment reads lighter, and more correct.

Segment rules and conditions are stored today across two tables joined by foreign keys: `SegmentRule`, with a self-FK for nesting and an FK to `Segment`, and `Condition`, FK to `SegmentRule`. Every read for evaluation reconstructs this tree through a three-level `prefetch_related` chain. Living with it showed that there's no value in having these relations stored, and maintaining them bears burden on all code paths interacting with segments. We want to store segment rule data as readily available JSON.

We add a JSON field `Segment.rules_data` holding the list of top-level rules in the API-compatible shape:

```json
[
{
"type": "ALL",
"conditions": [],
"rules": [
{
"type": "ANY",
"conditions": [
{
"operator": "EQUAL",
"property": "country",
"value": "UK",
"description": null
}
],
"rules": []
}
]
}
]
```

After that, we gradually migrate all segment read paths to read from it, and retire the existing models.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.