From 87f0f69ec1daa118bb459c0729ba3905abc28523 Mon Sep 17 00:00:00 2001 From: John Turner Date: Fri, 26 Sep 2025 18:32:09 -0400 Subject: make sure to try/catch around database transactions --- pypaste/server/sqlite/__init__.py | 41 +++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/pypaste/server/sqlite/__init__.py b/pypaste/server/sqlite/__init__.py index 7e490aa..be07db6 100644 --- a/pypaste/server/sqlite/__init__.py +++ b/pypaste/server/sqlite/__init__.py @@ -23,20 +23,24 @@ class Sqlite(Storage): data = await asyncio.to_thread(compress) - await self.connection.execute( - "insert into pastes values(?, ?, ?, ?, ?)", - (key.data, key.length, paste.dt.isoformat(), len(data), paste.syntax), - ) - - await self.connection.execute( - "insert into sqlite values(?, ?)", - ( - key.data, - data, - ), - ) + try: + await self.connection.execute( + "insert into pastes values(?, ?, ?, ?, ?)", + (key.data, key.length, paste.dt.isoformat(), len(data), paste.syntax), + ) + + await self.connection.execute( + "insert into sqlite values(?, ?)", + ( + key.data, + data, + ), + ) - await self.connection.commit() + await self.connection.commit() + except Exception as e: + await self.connection.rollback() + raise e async def retrieve(self, key: Key) -> Optional[Paste]: async with self.connection.execute( @@ -62,9 +66,16 @@ class Sqlite(Storage): return Paste(info.dt, info.syntax, text) async def delete(self, key: Key) -> None: - await self.connection.execute("delete from pastes where key=?", (key.data,)) + try: + await self.connection.execute("delete from pastes where key=?", (key.data,)) + + await self.connection.execute("delete from sqlite where key=?", (key.data,)) + + await self.connection.commit() + except Exception as e: + await self.connection.rollback() - await self.connection.execute("delete from sqlite where key=?", (key.data,)) + raise e async def exists(self, key: Key) -> bool: async with self.connection.execute( -- cgit v1.2.3