Files
tern-server/migrations/0007_rule_options.sql
Mateusz PstruchaandClaude Opus 5.5 3f1d9227c6
CI / test (push) Successful in 46s
Add trash retention, stop-after-match and run-now
Mail in Trash is purged after TRASH_RETENTION_DAYS (default 30, 0
disables), hourly. Stored files are content-addressed and shared, so a
file is only removed once nothing references it — and not if written or
reused in the last hour, since ingest writes a file before inserting the
row that points at it. Reusing an existing blob now refreshes its mtime
so that grace period protects it too. Trash rows report when they purge.

"Stop processing further rules" moves from an action to a per-rule
option (stop_after_match): it's a property of the rule, not something
done to a message. Migration 0007 converts existing stop actions, and a
stop submitted as an action is now rejected.

Rules can be run now against existing mail outside Trash, disabled ones
included so a rule can be tried before it's switched on. It pages by
key, one transaction per page, and only re-reads stored raw messages
when the rule tests a header.

All JSON bodies now go through an AppJson extractor so malformed or
mistyped bodies return the API's {"error": …} shape. Axum's default was
a plain-text 422 the webapp could only show as "request failed: 422" —
losing the useful part, e.g. "unknown variant `stop`".

Verified live: the stop flag blocks a lower rule on arriving mail; run
now on a disabled header rule archived 2 of 4 messages; a purge removed
an expired message and its unique raw file while keeping an attachment
file still shared with a live copy, with no orphaned rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 12:00:04 +02:00

14 lines
625 B
SQL

-- "Stop processing further rules" is a property of a rule, not something
-- it does to a message, so it moves out of the action list into a column.
ALTER TABLE rules ADD COLUMN stop_after_match BOOLEAN NOT NULL DEFAULT false;
UPDATE rules SET
stop_after_match = true,
actions = COALESCE(
(SELECT jsonb_agg(a) FROM jsonb_array_elements(actions) AS a WHERE a->>'type' <> 'stop'),
'[]'::jsonb)
WHERE actions @> '[{"type": "stop"}]';
-- Trashed mail is purged after a retention period; the purge scans by age.
CREATE INDEX messages_deleted_at_idx ON messages (deleted_at) WHERE deleted_at IS NOT NULL;