feat(streaks): add user_streaks table and get/put helpers

Adds the user_streaks table to settings.db schema and implements
get_user_streak / put_user_streak helpers with upsert semantics.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
shay
2026-04-20 11:40:13 -04:00
co-authored by Claude Sonnet 4.6
parent dd4a3d50d8
commit 7fb1ba3e01
2 changed files with 100 additions and 0 deletions
+60
View File
@@ -44,6 +44,16 @@ CREATE TABLE IF NOT EXISTS jobs (
updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_jobs_pending ON jobs(status, created_at);
CREATE TABLE IF NOT EXISTS user_streaks (
username TEXT PRIMARY KEY,
current_streak INTEGER NOT NULL DEFAULT 0,
current_start TEXT NOT NULL DEFAULT '',
last_active_date TEXT NOT NULL DEFAULT '',
max_streak INTEGER NOT NULL DEFAULT 0,
max_start TEXT NOT NULL DEFAULT '',
max_end TEXT NOT NULL DEFAULT '',
updated_at TEXT NOT NULL
);
"""
_WIDGETS_SCHEMA = """
@@ -253,6 +263,56 @@ def set_last_fetcher_hash(username: str, h: str) -> None:
c.commit()
def get_user_streak(username: str) -> Optional[dict]:
"""Return the persisted streak row for a user, or None if never written."""
with _settings_conn() as c:
row = c.execute(
"SELECT * FROM user_streaks WHERE username=?", (username,)
).fetchone()
if row is None:
return None
return {
"current_streak": row["current_streak"],
"current_start": row["current_start"],
"last_active_date": row["last_active_date"],
"max_streak": row["max_streak"],
"max_start": row["max_start"],
"max_end": row["max_end"],
"updated_at": row["updated_at"],
}
def put_user_streak(username: str, streak: dict) -> None:
"""UPSERT the streak row for a user. Caller supplies the merged values."""
now = _now()
with _settings_conn() as c:
c.execute(
"""INSERT INTO user_streaks(
username, current_streak, current_start, last_active_date,
max_streak, max_start, max_end, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(username) DO UPDATE SET
current_streak=excluded.current_streak,
current_start=excluded.current_start,
last_active_date=excluded.last_active_date,
max_streak=excluded.max_streak,
max_start=excluded.max_start,
max_end=excluded.max_end,
updated_at=excluded.updated_at""",
(
username,
int(streak.get("current_streak", 0)),
streak.get("current_start", ""),
streak.get("last_active_date", ""),
int(streak.get("max_streak", 0)),
streak.get("max_start", ""),
streak.get("max_end", ""),
now,
),
)
c.commit()
def set_github_profile(username: str, github_id: int, github_avatar_url: str) -> None:
with _settings_conn() as c:
c.execute(
+40
View File
@@ -110,3 +110,43 @@ def test_set_github_profile_round_trip(tmp_dbs):
row = c.execute("SELECT github_id, github_avatar_url FROM users WHERE username='alice'").fetchone()
assert row["github_id"] == 42
assert row["github_avatar_url"] == "https://example.com/a.png"
def test_get_user_streak_missing_returns_none(tmp_dbs):
assert dbmod.get_user_streak("nobody") is None
def test_put_and_get_user_streak_roundtrip(tmp_dbs):
dbmod.put_user_streak("alice", {
"current_streak": 12,
"current_start": "2026-04-08",
"last_active_date": "2026-04-19",
"max_streak": 42,
"max_start": "2025-11-01",
"max_end": "2025-12-12",
})
row = dbmod.get_user_streak("alice")
assert row["current_streak"] == 12
assert row["current_start"] == "2026-04-08"
assert row["last_active_date"] == "2026-04-19"
assert row["max_streak"] == 42
assert row["max_start"] == "2025-11-01"
assert row["max_end"] == "2025-12-12"
assert row["updated_at"] # set on write
def test_put_user_streak_upserts(tmp_dbs):
dbmod.put_user_streak("alice", {
"current_streak": 1, "current_start": "2026-04-19",
"last_active_date": "2026-04-19",
"max_streak": 1, "max_start": "2026-04-19", "max_end": "2026-04-19",
})
dbmod.put_user_streak("alice", {
"current_streak": 2, "current_start": "2026-04-18",
"last_active_date": "2026-04-19",
"max_streak": 5, "max_start": "2024-01-01", "max_end": "2024-01-05",
})
row = dbmod.get_user_streak("alice")
assert row["current_streak"] == 2
assert row["max_streak"] == 5
assert row["max_start"] == "2024-01-01"