Skip to content

Commit

Permalink
Add delete_old query
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Oct 8, 2023
1 parent 91eed93 commit 535d98a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions database/repositories/usercount.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

from app.common.database.objects import DBUserCount

from sqlalchemy import desc, and_
from datetime import datetime, timedelta
from typing import List, Optional
from datetime import datetime
from sqlalchemy import desc, and_

import app

Expand All @@ -27,3 +27,13 @@ def fetch_last() -> Optional[DBUserCount]:
return app.session.database.session.query(DBUserCount) \
.order_by(desc(DBUserCount.time)) \
.first()

def delete_old(delta: timedelta = timedelta(weeks=5)) -> int:
"""Delete usercount entries that are older than the given delta (default ~1 month)"""
with app.session.database.managed_session() as session:
rows = session.query(DBUserCount) \
.filter(DBUserCount.time <= (datetime.now() - delta)) \
.delete()
session.commit()

return rows

0 comments on commit 535d98a

Please sign in to comment.