Skip to content

fix(Segment membership PoC): Identity backfill broken due to lack of JSON column support#7584

Draft
khvn26 wants to merge 1 commit into
mainfrom
fix/segment-membership-backfill-staging
Draft

fix(Segment membership PoC): Identity backfill broken due to lack of JSON column support#7584
khvn26 wants to merge 1 commit into
mainfrom
fix/segment-membership-backfill-staging

Conversation

@khvn26
Copy link
Copy Markdown
Member

@khvn26 khvn26 commented May 22, 2026

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Contributes to #5663.

The daily IDENTITIES backfill + per-project refresh have never populated SegmentMembershipCount on staging. Two issues are addressed hiere:

  1. clickhouse-driver==0.2.10 has no column class for ClickHouse Cloud 25.12's first-class JSON type, so segment_membership.tasks.backfill_identities_to_clickhouse crashes with KeyError: 'JSON' while encoding the server's sample block.mymarilyn/clickhouse-driver#503 which adds JSON insert + select support. Will switch to a tagged release once that PR merges.
  2. ecs-task-definition-migration.json lackes CLICKHOUSE_URL — migration ran manually in staging, and the task definition is fixed here.

How did you test this code?

Smoke tested the exact task code path against staging ClickHouse 25.12.1 from a local Python shell using the patched driver:

  • Configured Django with the clickhouse DB alias pointing at the staging CLICKHOUSE_URL (port 9440, ?secure=true).
  • Built rows via the real map_identity_document_to_clickhouse_row mapper from synthetic Dynamo-shaped docs carrying str/Decimal/bool traits.
  • Created a throwaway table with the same DDL as clickhouse/0001_create_identities, ran cursor.executemany(_INSERT_IDENTITIES_SQL, rows) through connections["clickhouse"].cursor() — the call that was previously failing with KeyError: 'JSON'.
  • Queried back with the engine-flavoured shape SELECT environment_id, count() FROM <table> FINAL WHERE environment_id IN %(envs)s AND (traits.plan = 'enterprise' AND traits.age >= 22) GROUP BY environment_idtraits.<key> subcolumn predicates resolved correctly.
  • Round-tripped a row: traits returned as a Python dict with Decimal coerced to int and bool preserved.
  • Dropped the throwaway table.

Two issues stopped the daily IDENTITIES backfill + per-project refresh
from ever populating SegmentMembershipCount on staging:

1. clickhouse-driver 0.2.10 has no column class for the JSON type, so
   `cursor.executemany(_INSERT_IDENTITIES_SQL, rows)` crashes with
   `KeyError: 'JSON'` while encoding the sample block returned by
   ClickHouse Cloud 25.12. Pinning to `Flagsmith/clickhouse-driver`
   branch `newjson` (PR #503 upstream) which adds JSON insert/select
   support unblocks the path with no other code changes. Switch back to
   a versioned release once that PR merges.

2. The staging migration task definition was missing `CLICKHOUSE_URL`,
   so `CLICKHOUSE_ENABLED` evaluated False during `python manage.py
   migrate`, the `clickhouse` DB alias never registered, and the
   `clickhouse/0001_create_identities` migration was silently skipped.
   The `IDENTITIES` table didn't exist on staging at all until I ran a
   one-off task last week. Wire the same Secrets Manager value the
   admin-api task def already uses.

Smoke tested locally against staging CH 25.12.1: a Django shell run
through `connections["clickhouse"].cursor()` with the real
`map_identity_document_to_clickhouse_row` mapper round-trips JSON
traits (incl. Decimal/bool coercion) and matches a `traits.<key>`
predicate via the same `SELECT ... FROM IDENTITIES FINAL` shape the
refresh task uses.

beep boop
@vercel
Copy link
Copy Markdown

vercel Bot commented May 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs Ignored Ignored May 22, 2026 6:20pm
flagsmith-frontend-preview Ignored Ignored May 22, 2026 6:20pm
flagsmith-frontend-staging Ignored Ignored May 22, 2026 6:20pm

Request Review

@github-actions github-actions Bot added api Issue related to the REST API infrastructure labels May 22, 2026
@khvn26 khvn26 changed the title fix(Segment membership): Unblock backfill on staging ClickHouse 25.12 fix(Segment membership PoC): Identity backfill broken due to lack of JSON column support May 22, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds clickhouse-driver as a dependency via a specific git branch and introduces the CLICKHOUSE_URL environment variable to the staging ECS task definition. Feedback indicates that the git-based dependency may necessitate a C compiler in the build environment and could affect performance. Furthermore, the secret ARN for CLICKHOUSE_URL is likely misconfigured, as it lacks the necessary key suffix and is inconsistent with other database URL secrets.

Comment thread api/uv.lock
name = "clickhouse-driver"
version = "0.2.10"
source = { registry = "https://pypi.org/simple" }
source = { git = "https://github.com/Flagsmith/clickhouse-driver?branch=newjson#11a9bee45efec2415c5987734778313ad318af4f" }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Switching clickhouse-driver to a git source removes the pre-built wheels from the lock file. This package contains C extensions, so the build environment (e.g., the Docker build stage) will now require a C compiler and Python development headers. If these are missing, the installation will fail. Additionally, this may lead to a performance regression if the package falls back to the pure-Python implementation due to a failed extension build.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Bad bot; read the PR description before reviewing

},
{
"name": "CLICKHOUSE_URL",
"valueFrom": "arn:aws:secretsmanager:eu-west-2:302456015006:secret:clickhouse-url-ns26gC"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The valueFrom ARN for CLICKHOUSE_URL is inconsistent with the ANALYTICS_DATABASE_URL configuration and likely incorrect. While ANALYTICS_DATABASE_URL references a specific key within the shared ECS-API-heAdoB secret, this entry points to a different secret (clickhouse-url-ns26gC) and lacks the required :KEY:: syntax for JSON secrets. If clickhouse-url-ns26gC is a standard Key/Value secret, ECS will inject the entire JSON object as a string, causing the application to fail. Given the PR description states this should use the same value as admin-api, it should likely reference the same secret and key as ANALYTICS_DATABASE_URL.

Suggested change
"valueFrom": "arn:aws:secretsmanager:eu-west-2:302456015006:secret:clickhouse-url-ns26gC"
"valueFrom": "arn:aws:secretsmanager:eu-west-2:302456015006:secret:ECS-API-heAdoB:ANALYTICS_DATABASE_URL::"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

bad bot

@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.50%. Comparing base (2e16ede) to head (2cd1293).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7584   +/-   ##
=======================================
  Coverage   98.50%   98.50%           
=======================================
  Files        1430     1430           
  Lines       54254    54254           
=======================================
  Hits        53444    53444           
  Misses        810      810           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant