diff --git a/konabot/common/database/__init__.py b/konabot/common/database/__init__.py index 521f597..c771b8b 100644 --- a/konabot/common/database/__init__.py +++ b/konabot/common/database/__init__.py @@ -131,7 +131,7 @@ class DatabaseManager: await conn.execute(command, params or ()) await conn.commit() except Exception as e: - # 记录错误但重新抛出,让调用者处理 + await conn.rollback() raise Exception(f"数据库执行失败: {str(e)}") from e finally: await self._return_connection(conn) @@ -143,7 +143,7 @@ class DatabaseManager: await conn.executescript(script) await conn.commit() except Exception as e: - # 记录错误但重新抛出,让调用者处理 + await conn.rollback() raise Exception(f"数据库脚本执行失败: {str(e)}") from e finally: await self._return_connection(conn) @@ -197,7 +197,7 @@ class DatabaseManager: await conn.executemany(command, seq_of_params) await conn.commit() except Exception as e: - # 记录错误但重新抛出,让调用者处理 + await conn.rollback() raise Exception(f"数据库批量执行失败: {str(e)}") from e finally: await self._return_connection(conn) diff --git a/konabot/common/permsys/migrates/__init__.py b/konabot/common/permsys/migrates/__init__.py index 67da6fd..3b6a057 100644 --- a/konabot/common/permsys/migrates/__init__.py +++ b/konabot/common/permsys/migrates/__init__.py @@ -52,7 +52,11 @@ async def get_current_version(conn: aiosqlite.Connection) -> int: if count[0] < 1: logger.info("权限系统数据表不存在,现在创建表") await conn.executescript(SQL_CREATE_TABLE) - await conn.commit() + try: + await conn.commit() + except Exception: + await conn.rollback() + raise return 0 cursor = await conn.execute(SQL_GET_MIGRATE_VERSION) row = await cursor.fetchone() @@ -72,10 +76,18 @@ async def execute_migration( await conn.executescript(migration.get_upgrade_script()) now_version += 1 await conn.execute(SQL_UPDATE_VERSION, (now_version,)) - await conn.commit() + try: + await conn.commit() + except Exception: + await conn.rollback() + raise while now_version > version: migration = migrations[now_version - 1] await conn.executescript(migration.get_downgrade_script()) now_version -= 1 await conn.execute(SQL_UPDATE_VERSION, (now_version,)) - await conn.commit() + try: + await conn.commit() + except Exception: + await conn.rollback() + raise diff --git a/konabot/common/permsys/repo.py b/konabot/common/permsys/repo.py index 4c944b7..22c1924 100644 --- a/konabot/common/permsys/repo.py +++ b/konabot/common/permsys/repo.py @@ -43,11 +43,15 @@ class PermRepo: Raises: AssertionError: 如果创建后无法获取实体 ID。 """ - await self.conn.execute( - s("create_entity.sql"), - (entity.platform, entity.entity_type, entity.external_id), - ) - await self.conn.commit() + try: + await self.conn.execute( + s("create_entity.sql"), + (entity.platform, entity.entity_type, entity.external_id), + ) + await self.conn.commit() + except Exception: + await self.conn.rollback() + raise eid = await self._get_entity_id_or_none(entity) assert eid is not None return eid @@ -115,8 +119,12 @@ class PermRepo: value: 要设置的配置值(True/False/None)。 """ eid = await self.get_entity_id(entity) - await self.conn.execute(s("update_perm_info.sql"), (eid, config_key, value)) - await self.conn.commit() + try: + await self.conn.execute(s("update_perm_info.sql"), (eid, config_key, value)) + await self.conn.commit() + except Exception: + await self.conn.rollback() + raise async def get_entity_id_batch( self, entities: list[PermEntity] @@ -135,11 +143,15 @@ class PermRepo: # s("create_entity.sql"), # (entity.platform, entity.entity_type, entity.external_id), # ) - await self.conn.executemany( - s("create_entity.sql"), - [(e.platform, e.entity_type, e.external_id) for e in entities], - ) - await self.conn.commit() + try: + await self.conn.executemany( + s("create_entity.sql"), + [(e.platform, e.entity_type, e.external_id) for e in entities], + ) + await self.conn.commit() + except Exception: + await self.conn.rollback() + raise val_placeholders = ", ".join(["(?, ?, ?)"] * len(entities)) params = [] for e in entities: