Skip to main content

Ads domain decisions

This page records the ratified design decisions for the Ads domain. Read it before you design anything in this domain.

Each decision states what we decided, why, and where the evidence is. Decisions live only on this page. Other docs must link here instead of repeating the text. When a decision changes, update this page the same day.

Schema truth is jodapp-api/docs/db/ads.dbml. Verify columns there before you rely on this page.

D1 — The ads tracking tables carry no foreign keys (2026-10-14)​

The tables ads_impressions, ads_clicks and ads_campaign_reviews point at other tables without a foreign key in the database.

  • The design file jodapp-api/docs/db/ads.dbml keeps the ref: on each column, so a reader sees the association.
  • Each of those columns carries a note that starts with NO FOREIGN KEY:, and bin/dbml_check reads it.

Ali decided this on 2026-10-14, when PR jodapp-api#2129 was about to add the four keys.

What we decided​

PartDecision
ads_campaign_reviews.admin_idpoints at identities_admins.id, no foreign key
ads_impressions.viewer_idpoints at identities_users.id, no foreign key, NULL for a visitor who is not logged in
ads_clicks.clicker_idpoints at identities_users.id, no foreign key, NULL for a visitor who is not logged in
ads_clicks.ads_impression_idpoints at ads_impressions.id, no foreign key, NULL when the view was not tracked
The rule for a new column in this domaina foreign key earns its place when the parent row can be deleted, or when the id comes from outside the app, such as a form or a sync; a tracking column that writes an id from the current session gets none
The design filethe ref: stays, and the column note starts with NO FOREIGN KEY: followed by the reason

Why​

  • A foreign key stops two things.
    • A child row that points at a parent row that does not exist.
    • A delete of a parent row while child rows still point at it.
  • Neither can happen on these four columns today.
    • The three ids that point at a person come from the current session.
    • So a wrong id can only come from our own code, never from a form or a sync.
    • A user row and an admin row are never deleted.
    • The record of that rule is Identities D8 — A user row is never hard-deleted.
    • On 2026-10-05, production had 0 rows that broke any of the four keys, across about 2 million impressions.
  • The one parent that can go away is ads_impressions, and we want it to.
    • A planned daily job rolls impressions up into counts and deletes the old raw rows.
    • A foreign key from ads_clicks.ads_impression_id would block that delete, so it would be dropped again within weeks.
    • Without the key, an old click keeps the impression id as a plain value.
    • The unique click count groups on that value, so it still works after the delete.
  • A foreign key costs one index lookup on every ad view and every click.
    • That is the busiest write path in the app.
  • The first migration for ads_clicks set foreign_key: false on purpose.
    • The housekeeping check reported the gap as a miss.
    • The fix would have overridden that choice without anyone asking why it was there.

Where the evidence is​

EvidenceWhere
The closed PR and the reasoningjodapp-api#2129, the last comment, 2026-10-14
The original choicejodapp-api/db/migrate/20260219052550_create_ads_clicks.rb: t.references :ads_impression, type: :uuid, foreign_key: false
How the ids are writtenAds::CampaignPlacements::ShowManager and Ads::Clicks::ShowManager take viewer_id and clicker_id from the current session; Ads::Campaigns::RejectService and TeamApproveManager take admin_id from the admin session
How unique clicks are countedAds::CampaignService.compute_clicks_stats groups on COALESCE(ads_impression_id::text, 'orphan_' || id::text)
The rollup designdocs/scratchpad/design-ads-impressions-daily-rollup.md in this repo, 2026-10-13
The housekeeping issue that found the gapjodapp-api#2120, rows D23 to D26