diff options
Diffstat (limited to 'api/database.py')
| -rw-r--r-- | api/database.py | 19 |
1 files changed, 18 insertions, 1 deletions
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" |
