diff options
Diffstat (limited to 'api/util/clean_db.py')
-rw-r--r-- | api/util/clean_db.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/api/util/clean_db.py b/api/util/clean_db.py new file mode 100644 index 0000000..11438aa --- /dev/null +++ b/api/util/clean_db.py @@ -0,0 +1,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) |