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