From 74e0f33bd5ff740c51a3c46d607c4aa2856528a6 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Mon, 8 Sep 2025 16:37:31 -0700 Subject: handle session clearing edge case where session may expire prematurely --- api/database.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'api/database.py') diff --git a/api/database.py b/api/database.py index 14ac611..0f31a7d 100644 --- a/api/database.py +++ b/api/database.py @@ -20,12 +20,29 @@ class PostgresHandler: self._connection.commit() cursor.close() - def clear_table(self, name: str): + def clear_table_by_date(self, name: str): cursor = self._connection.cursor() cursor.execute(f"DELETE FROM {name}") self._connection.commit() cursor.close() + def clear_old_sessions(self, table_name: str): + """Delete sessions older than 15 minutes""" + try: + cursor = self._connection.cursor() + query = f"DELETE FROM {table_name} WHERE created_at <= NOW() - INTERVAL '15 minutes'" + cursor.execute(query) + deleted_count = cursor.rowcount + self._connection.commit() + cursor.close() + print(f"Deleted {deleted_count} old sessions") + return deleted_count + except Error as e: + self._connection.rollback() + print(f"Failed to delete old sessions from {table_name}") + print(e) + return False + def check_row_exists(self, table_name: str, column_name: str, value: str): cursor = self._connection.cursor() query = f"SELECT 1 FROM {table_name} WHERE {column_name} = %s" -- cgit v1.2.3