Skip to content

Epic: Store segment rules and conditions as JSON #7814

Description

@khvn26

Supersedes #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:

[
  {
    "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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Epic.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions