"""a delivery of a rule the session had already opened counts as opened Revision ID: 0135 Revises: 0134 Create Date: 2026-11-24 From now on a line for a rule the session has opened in the last six hours is stamped opened as it is written (`rule_deliveries._read_already`). This stamps the deliveries already recorded the same way, so the open rates, the adherence readout and the weighing pass read one definition across old and new rows: a rule read early in a session is not counted unread each time it arrives again. The stamp is the delivery's own time. Not reversible: the downgrade leaves the stamps, which the earlier code reads as ordinary opens. """ from alembic import op revision = "0135" down_revision = "0134" branch_labels = None depends_on = None def upgrade() -> None: op.execute( """ UPDATE rule_deliveries d SET opened_at = d.delivered_at WHERE d.opened_at IS NULL AND EXISTS ( SELECT 1 FROM rule_deliveries e WHERE e.user_id = d.user_id AND e.session_id = d.session_id AND e.rule_id = d.rule_id AND e.opened_at IS NOT NULL AND e.opened_at <= d.delivered_at AND e.opened_at >= d.delivered_at - INTERVAL '6 hours' ) """ ) def downgrade() -> None: pass