blob: 11438aa59785b6c96f1083b78452744bd8b7b260 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import datetime
from api.util.db_dependency import get_db
from models import Link, Log
"""
Remove expired short links and their associated logs
"""
def clean_db():
db = next(get_db())
# Get all expired short links
expired_links = (
db.query(Link)
.filter(Link.expire_date < datetime.datetime.today())
.all()
)
# Delete all expired short links and their logs
for link in expired_links:
logs = db.query(Log).filter(Log.link == link.link).all()
for log in logs:
db.delete(log)
db.delete(link)
|