Skip to content

Refactor FTS trigger aggregations to a shared SQL view (v0.7 candidate) #59

Open

Description

@utof

Context

V007+V008 have ~8 trigger bodies that all compute the same thing: the joined live state for a given `blake3_hash`, to populate `search_content`. The aggregation SQL is duplicated in every trigger body + `rebuild()` + `search()`. V007→V008 bug class arose because keeping these ~8 copies in sync is tribal.

Proposal

Create a SQL view:

```sql
CREATE VIEW search_content_ground_truth AS
SELECT fl.blake3_hash,
fl.relative_path AS filename,
fl.relative_path,
COALESCE(m.mime_type, '') AS mime_type,
COALESCE(m.camera_model, '') AS camera_model,
COALESCE(m.captured_at, '') AS captured_at,
COALESCE((
SELECT GROUP_CONCAT(t.name, ' ')
FROM file_tags ft JOIN tags t ON t.id = ft.tag_id
WHERE ft.blake3_hash = fl.blake3_hash
AND ft.deleted_at IS NULL
AND t.deleted_at IS NULL
), '') AS tags
FROM file_locations fl
LEFT JOIN file_metadata m
ON m.blake3_hash = fl.blake3_hash
AND m.deleted_at IS NULL
WHERE fl.deleted_at IS NULL
AND fl.id = (
SELECT id FROM file_locations
WHERE blake3_hash = fl.blake3_hash AND deleted_at IS NULL
ORDER BY first_seen ASC, id ASC LIMIT 1
);
```

Every trigger body collapses to:
```sql
INSERT OR REPLACE INTO search_content
SELECT * FROM search_content_ground_truth WHERE blake3_hash = NEW.blake3_hash;
```

Value

  • Single source of truth — fix the aggregation once, it propagates everywhere.
  • Trigger bodies shrink from ~30 lines each to ~3 lines — diff reviews become trivial.
  • Tests simplify — ground-truth proptest can directly compare against the view output.

Risks

  • SQLite planner unknowns: does the view's JOIN get materialized per call, or can the planner inline it? Test with EXPLAIN QUERY PLAN before committing.
  • Perf regression risk: the current hand-inlined triggers may be faster.
  • Test surface churn: re-running all V007/V008 tests against the refactor.

Why v0.7, not v0.6.x

Shipping the architecture refactor in the same minor as the correctness fix invites fresh instability. Let v0.6.4 harden in the wild first. Revisit in v0.7 planning if a 3rd FTS bug reoccurs (signal that the current shape is untenable) OR as a planned maintenance task.

Labels

`enhancement`, `post-v1`, `needs-brainstorm`

Activity

  1. added
    type/feature
    status/needs-brainstorm
    on Apr 18, 2026
  2. added this to the post-v1 milestone on May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/needs-brainstormtype/feature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions