fix quality code
This commit is contained in:
@@ -2,17 +2,18 @@ import os
|
||||
from typing import Optional
|
||||
|
||||
import aiosqlite
|
||||
|
||||
from logs.custom_logger import logger
|
||||
|
||||
|
||||
class DatabaseConnection:
|
||||
"""Базовый класс для работы с базой данных."""
|
||||
|
||||
|
||||
def __init__(self, db_path: str):
|
||||
self.db_path = os.path.abspath(db_path)
|
||||
self.logger = logger
|
||||
self.logger.info(f'Инициация базы данных: {self.db_path}')
|
||||
|
||||
self.logger.info(f"Инициация базы данных: {self.db_path}")
|
||||
|
||||
async def _get_connection(self):
|
||||
"""Получение асинхронного соединения с базой данных."""
|
||||
try:
|
||||
@@ -28,7 +29,7 @@ class DatabaseConnection:
|
||||
except Exception as e:
|
||||
self.logger.error(f"Ошибка при получении соединения: {e}")
|
||||
raise
|
||||
|
||||
|
||||
async def _execute_query(self, query: str, params: tuple = ()):
|
||||
"""Выполнение запроса с автоматическим закрытием соединения."""
|
||||
conn = None
|
||||
@@ -43,7 +44,7 @@ class DatabaseConnection:
|
||||
finally:
|
||||
if conn:
|
||||
await conn.close()
|
||||
|
||||
|
||||
async def _execute_query_with_result(self, query: str, params: tuple = ()):
|
||||
"""Выполнение запроса с результатом и автоматическим закрытием соединения."""
|
||||
conn = None
|
||||
@@ -59,7 +60,7 @@ class DatabaseConnection:
|
||||
finally:
|
||||
if conn:
|
||||
await conn.close()
|
||||
|
||||
|
||||
async def _execute_transaction(self, queries: list):
|
||||
"""Выполнение транзакции с несколькими запросами."""
|
||||
conn = None
|
||||
@@ -76,7 +77,7 @@ class DatabaseConnection:
|
||||
finally:
|
||||
if conn:
|
||||
await conn.close()
|
||||
|
||||
|
||||
async def check_database_integrity(self):
|
||||
"""Проверяет целостность базы данных и очищает WAL файлы."""
|
||||
conn = None
|
||||
@@ -84,14 +85,16 @@ class DatabaseConnection:
|
||||
conn = await self._get_connection()
|
||||
result = await conn.execute("PRAGMA integrity_check")
|
||||
integrity_result = await result.fetchone()
|
||||
|
||||
|
||||
if integrity_result and integrity_result[0] == "ok":
|
||||
self.logger.info("Проверка целостности базы данных прошла успешно")
|
||||
await conn.execute("PRAGMA wal_checkpoint(TRUNCATE)")
|
||||
self.logger.info("WAL файлы очищены")
|
||||
else:
|
||||
self.logger.warning(f"Проблемы с целостностью базы данных: {integrity_result}")
|
||||
|
||||
self.logger.warning(
|
||||
f"Проблемы с целостностью базы данных: {integrity_result}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Ошибка при проверке целостности базы данных: {e}")
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user