diff options
author | Parker <contact@pkrm.dev> | 2024-12-19 16:35:18 -0600 |
---|---|---|
committer | Parker <contact@pkrm.dev> | 2024-12-19 16:35:18 -0600 |
commit | 55e7ee145c470e832b56c9540f520aedf8792c15 (patch) | |
tree | d334be8a1138e238c6b9a5cc4e01cc740320a34a /api/util | |
parent | cfbaf8a0b2afa86cd06ca10932e59cc27071f714 (diff) |
Add database cleanup schedulerdev
Diffstat (limited to 'api/util')
-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) |