feat(versions): rolling-cap prune ignores pinned versions

The DELETE inside create_version now filters pin_kind IS NULL so pinned
rows (auto or manual) aren't counted toward MAX_VERSIONS=50 and aren't
candidates for deletion. Pinned versions live indefinitely regardless
of how heavy rolling autosave traffic gets on the same note.
This commit is contained in:
2026-05-13 13:55:44 -04:00
parent 17211c6e82
commit b65d736869
2 changed files with 52 additions and 2 deletions
@@ -47,13 +47,18 @@ async def create_version(
await session.commit()
await session.refresh(version)
# Prune versions beyond MAX_VERSIONS
# Prune rolling versions beyond MAX_VERSIONS. Pinned rows
# (pin_kind IS NOT NULL) are excluded from both the counted
# bucket and the deletion candidate set, so they survive
# indefinitely regardless of rolling autosave volume.
await session.execute(
text("""
DELETE FROM note_versions
WHERE id IN (
SELECT id FROM note_versions
WHERE note_id = :note_id AND user_id = :user_id
WHERE note_id = :note_id
AND user_id = :user_id
AND pin_kind IS NULL
ORDER BY created_at DESC
OFFSET :max_versions
)