From bc6d692f0acad620d77c9e5a0449538d0d746de1 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 13 Nov 2024 15:03:37 +0100 Subject: [PATCH] added "delete_expired" method for leases --- app/orm.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/orm.py b/app/orm.py index 53109ed..bc902df 100644 --- a/app/orm.py +++ b/app/orm.py @@ -71,6 +71,16 @@ class Origin(Base): session.close() return deletions + @staticmethod + def delete_expired(engine: Engine) -> int: + session = sessionmaker(bind=engine)() + origins = session.query(Origin).join(Lease, Origin.origin_ref == Lease.origin_ref, isouter=True).filter(Lease.lease_ref.is_(None)).all() + origin_refs = [origin.origin_ref for origin in origins] + deletions = session.query(Origin).filter(Origin.origin_ref.in_(origin_refs)).delete() + session.commit() + session.close() + return deletions + class Lease(Base): __tablename__ = "lease"