修复 rollback 失效问题
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-04-18 10:53:53 +08:00
parent 27670920f6
commit 0ba51bc9b2
3 changed files with 42 additions and 18 deletions

View File

@ -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)

View File

@ -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

View File

@ -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: