aboutsummaryrefslogtreecommitdiff
path: root/app/func
diff options
context:
space:
mode:
authorParker <contact@pkrm.dev>2024-02-25 18:13:03 -0600
committerParker <contact@pkrm.dev>2024-02-25 18:13:03 -0600
commit93d1dab63abff74573841ec71f33edec8790d860 (patch)
treef39ea93652e6ac000b2f67f9f40d06efab136030 /app/func
parent236f050f54bf3fb47fe74630b3fa985242e6784a (diff)
Remove data for old links
Diffstat (limited to 'app/func')
-rw-r--r--app/func/remove_old_data.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/app/func/remove_old_data.py b/app/func/remove_old_data.py
new file mode 100644
index 0000000..7380fc5
--- /dev/null
+++ b/app/func/remove_old_data.py
@@ -0,0 +1,23 @@
+import sqlalchemy
+import datetime
+
+from db import engine
+
+"""
+Remove all links and associated records when the expire date has passed
+"""
+def remove_old_data():
+ with engine.begin() as conn:
+ today = datetime.datetime.date(datetime.datetime.now())
+ old_links = conn.execute(sqlalchemy.text('SELECT link FROM links WHERE expire_date < :today'), [{'today': today}])
+
+ delete_links = []
+
+ for row in old_links:
+ link = row.link
+ delete_links.append({'link': link})
+
+ with engine.begin() as conn:
+ conn.execute(sqlalchemy.text('DELETE FROM links WHERE link = :link'), delete_links)
+ conn.execute(sqlalchemy.text('DELETE FROM records WHERE link = :link'), delete_links)
+ conn.commit()