From e4e6387b2aa1e0ac3652b7ac4b313ec8846663d5 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 13 Nov 2024 14:58:55 +0100 Subject: [PATCH 1/4] ci improvements --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6181631..71b9164 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -20,6 +20,7 @@ build:docker: changes: - app/**/* - Dockerfile + - requirements.txt - if: $CI_PIPELINE_SOURCE == 'merge_request_event' tags: [ docker ] before_script: From fa2c06972e3dca9055860871bb491f7eb487e70c Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 13 Nov 2024 15:01:33 +0100 Subject: [PATCH 2/4] sql query improvements --- app/orm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/orm.py b/app/orm.py index 73828fd..b13ed38 100644 --- a/app/orm.py +++ b/app/orm.py @@ -66,7 +66,7 @@ class Origin(Base): if origin_refs is None: deletions = session.query(Origin).delete() else: - deletions = session.query(Origin).filter(Origin.origin_ref in origin_refs).delete() + deletions = session.query(Origin).filter(Origin.origin_ref.in_(origin_refs)).delete() session.commit() session.close() return deletions From 63c37c6334885bd50f0ce1128dd9de0fb6834a55 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 13 Nov 2024 15:03:12 +0100 Subject: [PATCH 3/4] fixed timezone in json response --- app/orm.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/orm.py b/app/orm.py index b13ed38..53109ed 100644 --- a/app/orm.py +++ b/app/orm.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from dateutil.relativedelta import relativedelta from sqlalchemy import Column, VARCHAR, CHAR, ForeignKey, DATETIME, update, and_, inspect, text @@ -94,10 +94,10 @@ class Lease(Base): 'lease_ref': self.lease_ref, 'origin_ref': self.origin_ref, # 'scope_ref': self.scope_ref, - 'lease_created': self.lease_created.isoformat(), - 'lease_expires': self.lease_expires.isoformat(), - 'lease_updated': self.lease_updated.isoformat(), - 'lease_renewal': lease_renewal.isoformat(), + 'lease_created': self.lease_created.replace(tzinfo=timezone.utc).isoformat(), + 'lease_expires': self.lease_expires.replace(tzinfo=timezone.utc).isoformat(), + 'lease_updated': self.lease_updated.replace(tzinfo=timezone.utc).isoformat(), + 'lease_renewal': lease_renewal.replace(tzinfo=timezone.utc).isoformat(), } @staticmethod From bc6d692f0acad620d77c9e5a0449538d0d746de1 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 13 Nov 2024 15:03:37 +0100 Subject: [PATCH 4/4] 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"