Skip to content

MAINT-172: pagination/queries — space-filter SQL fragment inlined across 13+ files #139

@jfolcini

Description

@jfolcini

Problem: The fragment

(?N IS NULL OR COALESCE(b.page_id, b.id) IN
    (SELECT bp.block_id FROM block_properties bp
     WHERE bp.key = 'space' AND bp.value_ref = ?N))

is duplicated across pagination/{hierarchy,tags,links,undated,agenda,trash,properties}.rs, backlink/{query,grouped}.rs, fts/search.rs, tag_query/query.rs, and commands/{pages,agenda}.rs. The space_filter_clause! macro is referenced in inline comments but unusable because sqlx::query_as! requires a string literal and rejects concat!(). Comments at the call sites instruct future maintainers to "mirror any change" — convention enforcement, not single-source-of-truth.

Why it matters: Real maintenance hotspot. Any change to the filter semantics requires N coordinated edits. A subtle bug (one site forgets the COALESCE) would only be caught by per-site tests.

Fix (design space):

  1. build.rs text substitution — generate per-query SQL strings into OUT_DIR from a single canonical fragment, keep query_as! consuming the generated literal.
  2. prek hook — fail commit if the canonical fragment text drifts across the 13 sites. Cheap; does not consolidate the source.
  3. Migrate the queries off query_as! to runtime sqlx — loses compile-time validation; not preferred.

Cost: M — design + implementation + verifying the 13 sites still produce identical query plans.
Risk: Medium — touching every list query is high blast-radius; needs careful test coverage.
Impact: Medium — eliminates a recurring drift hotspot; closes a long-tail correctness foot-gun.
Decision: Defer until the cost of drift becomes visible (a real bug shipped because one site got out of sync). Until then, the comment-based "mirror any change" convention is acceptable.
Status: Deferred.


Migrated from pending/REVIEW-LATER.md (MAINT-172).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions