aboutsummaryrefslogtreecommitdiff
path: root/app/func/remove_old_data.py
blob: 7380fc5d35cc0e1897a64dc9993817440411749a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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()