credits: an adjustment below the drawn amount must not eat the next pack
ci/woodpecker/push/woodpecker Pipeline was canceled

credits_drawn could exceed credit_balance after an admin set/correction (or a
negative adjustment), so a later pack was only partly usable: available =
max(0, purchased - drawn) swallowed the difference. Cap credits_drawn at the
row's balance on every add_credits movement; ledger history is unchanged.
Tests cover reduction-then-repurchase.
This commit is contained in:
inference-bot committed 2026-11-27 22:05:19 +00:00
1 parent 066a6fc2cf
commit d10b36ed34
2 files changed
+20 -2

No files matched your search

+9
View File
@@ -1708,6 +1708,15 @@ def add_credits(email: str, delta: float, reason: str, reference: str | None = N
"INSERT INTO credit_ledger (email, delta, reason, reference) VALUES (?, ?, ?, ?)",
(email, delta, reason, reference),
)
# Keep `credits_drawn <= credit_balance`. An adjustment that lowers the pack
# below what was already drawn must not swallow FUTURE credits: a new pack
# adds headroom, while consumption beyond the reduced pack is simply
# unbacked. The ledger history itself is never rewritten.
conn.execute(
"UPDATE members SET credits_drawn = MIN(COALESCE(credits_drawn, 0), "
"MAX(0, COALESCE(credit_balance, 0))) WHERE email = ?",
(email,),
)
row = conn.execute(
"SELECT credit_balance FROM members WHERE email = ?", (email,)
).fetchone()