From 628b941f20088a606f161f70678e77d80a3cc15d Mon Sep 17 00:00:00 2001 From: Kusaeni Date: Wed, 2 Oct 2024 19:49:30 +0700 Subject: [PATCH] tambah fungsi hapus data biar database ga gendut --- hapus_data.py | 24 ++++++++++++++++++++++++ main.py | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 hapus_data.py diff --git a/hapus_data.py b/hapus_data.py new file mode 100644 index 0000000..e8fdb1f --- /dev/null +++ b/hapus_data.py @@ -0,0 +1,24 @@ +from sqlalchemy import desc + +from models import Notifikasi, Session + +session = Session() + + +def hapus_notif(): + lima = session.query(Notifikasi).order_by(desc(Notifikasi.id)).limit(5) + + aman = [] + + for item in lima: + id = item.id + + aman.append(id) + + session.query(Notifikasi).filter(Notifikasi.id.not_in(aman)).delete( + synchronize_session="fetch" + ) + session.commit() + + +hapus_notif() diff --git a/main.py b/main.py index d088a51..8ea1e68 100644 --- a/main.py +++ b/main.py @@ -168,8 +168,8 @@ async def jalan(): await getNotif() await asyncio.sleep(5) await kirim_ke_tele() - # await asyncio.sleep(5) - # await tandai_notif() + await asyncio.sleep(5) + await tandai_notif() #