Skip to content

Added a table of what filters select, by the kind of key they name - #320

Closed
rob-ghost wants to merge 1 commit into
mainfrom
chore/dya-361-filter-rule-guard
Closed

rob-ghost wants to merge 1 commit into
mainfrom
chore/dya-361-filter-rule-guard

Conversation

@rob-ghost

Copy link
Copy Markdown
Contributor

ref https://linear.app/ghost/issue/DYA-361

What it adds

A test that states what a filter means under a negation, null or a related row, for every kind of key, and records where the code disagrees today. It changes no code: every filter selects the same rows before and after this pull request.

The rule it states: every key names the set of values a row has for it, and NULL is never a value. A column or a one-to-one relation has no value or one; a to-many relation has one value per related row whose column isn't NULL; an aggregate has exactly one number. Every negation selects exactly the rows its positive form doesn't.

Filter Meaning
k:v / k:-v v is / is not one of k's values
k:null / k:-null k has no value / has a value
k:[a,b] / k:-[a,b] k:a,k:b / k:-a+k:-b
k:[null,a] / k:-[null,a] k:null,k:a / k:-null+k:-a
k:~v / k:-~v some value matches / no value matches

Each case is a filter and the posts the rule selects. Where the code selects other posts today, the case says so:

{kind: 'many-to-many', filter: 'tag:-null', rows: [1, 2, 4, 5, 6], today: [3, 7], memoryToday: [1, 2, 3, 4, 5, 6, 7]},
{kind: '$elemMatch', filter: {fields: {$elemMatch: {field_key: 'company', value_text: 'ghost'}}}, rows: [1, 5], memory: false},

The kinds of key covered: a plain column, one that can be NULL, a one-to-one relation, a many-to-many relation and one of its columns that can be NULL, a join table's own column alone and beside its relation, an aggregate, a key that stands for several conditions (primary_tag), and $elemMatch as Ghost's member custom field filters write it. The same filters are matched against the posts as JSON objects, as routes.yaml matches them. For every kind of key it checks the equalities the rule implies (k:-a selects what -(k:a) selects, k:null and k:-null split all rows, a query rendered to SQL once and then run selects the same rows, and so on), with the ones that don't hold today listed.

Scope

In: the test, its tables and fixtures. Out: any change to what a filter selects. A later change that brings a filter in line with the rule has to delete that filter's today entry, so its diff shows exactly which filters it changes. The $elemMatch cases have no deviation and pin the rows Ghost's custom field filters select today.

Two readings are decisions rather than the rule table itself, and are stated at the top of the test: conditions on different columns of one relation in one + group read the same related row, as they do today (tag:news+tags.visibility:internal selects no post here); and a join table condition beside its relation picks which related rows the condition reads.

Testing

pnpm --filter @tryghost/nql test on SQLite. The rule's rows were cross-checked against an independent prototype of the rule, which agrees on every case except the join table ones it doesn't route.

ref https://linear.app/ghost/issue/DYA-361

Nothing in NQL said what a filter means under a negation, null or a related row, so each kind of key answered differently: tag:-null selects the posts with no tags while image:-null selects the posts with an image, and image:-'a.jpg' leaves out the posts with no image at all. The new test states one rule for every kind of key and runs it against the database: a plain column, one that can be NULL, a one-to-one relation, a many-to-many relation and one of its columns that can be NULL, a join table's own column alone and beside its relation, an aggregate, a key that stands for several conditions such as primary_tag, and $elemMatch as Ghost's custom field filters write it. Each filter lists the posts the rule selects. Where the code selects other posts today, the case records them as a known deviation, so the test passes now and a change that brings a filter in line with the rule has to remove the entry, which shows in its diff exactly which filters it changes. The same filters are matched against posts as JSON objects in memory, as routes.yaml does, and the equalities the rule implies, such as k:-a selecting what -(k:a) selects, are checked for every kind of key, with the ones that fail today listed. The $elemMatch filters have no deviation, so they pin the rows Ghost's custom field filters select today.
@coderabbitai

coderabbitai Bot commented Nov 30, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.56%. Comparing base (e0689bd) to head (c4ce52f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #320   +/-   ##
=======================================
  Coverage   85.56%   85.56%           
=======================================
  Files           9        9           
  Lines        2418     2418           
  Branches      510      513    +3     
=======================================
  Hits         2069     2069           
  Misses        343      343           
  Partials        6        6           

☔ View full report in Codecov by Harness.
📢 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.

@rob-ghost rob-ghost closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants