Enhance bot functionality and refactor database interactions
- Added `ca-certificates` installation to Dockerfile for improved network security. - Updated health check command in Dockerfile to include better timeout handling. - Refactored `run_helper.py` to implement proper signal handling and logging during shutdown. - Transitioned database operations to an asynchronous model in `async_db.py`, improving performance and responsiveness. - Updated database schema to support new foreign key relationships and optimized indexing for better query performance. - Enhanced various bot handlers to utilize async database methods, improving overall efficiency and user experience. - Removed obsolete database and fix scripts to streamline the project structure.
This commit is contained in:
79
database/repository_factory.py
Normal file
79
database/repository_factory.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from typing import Optional
|
||||
from database.repositories.user_repository import UserRepository
|
||||
from database.repositories.blacklist_repository import BlacklistRepository
|
||||
from database.repositories.message_repository import MessageRepository
|
||||
from database.repositories.post_repository import PostRepository
|
||||
from database.repositories.admin_repository import AdminRepository
|
||||
from database.repositories.audio_repository import AudioRepository
|
||||
|
||||
|
||||
class RepositoryFactory:
|
||||
"""Фабрика для создания репозиториев."""
|
||||
|
||||
def __init__(self, db_path: str):
|
||||
self.db_path = db_path
|
||||
self._user_repo: Optional[UserRepository] = None
|
||||
self._blacklist_repo: Optional[BlacklistRepository] = None
|
||||
self._message_repo: Optional[MessageRepository] = None
|
||||
self._post_repo: Optional[PostRepository] = None
|
||||
self._admin_repo: Optional[AdminRepository] = None
|
||||
self._audio_repo: Optional[AudioRepository] = None
|
||||
|
||||
@property
|
||||
def users(self) -> UserRepository:
|
||||
"""Возвращает репозиторий пользователей."""
|
||||
if self._user_repo is None:
|
||||
self._user_repo = UserRepository(self.db_path)
|
||||
return self._user_repo
|
||||
|
||||
@property
|
||||
def blacklist(self) -> BlacklistRepository:
|
||||
"""Возвращает репозиторий черного списка."""
|
||||
if self._blacklist_repo is None:
|
||||
self._blacklist_repo = BlacklistRepository(self.db_path)
|
||||
return self._blacklist_repo
|
||||
|
||||
@property
|
||||
def messages(self) -> MessageRepository:
|
||||
"""Возвращает репозиторий сообщений."""
|
||||
if self._message_repo is None:
|
||||
self._message_repo = MessageRepository(self.db_path)
|
||||
return self._message_repo
|
||||
|
||||
@property
|
||||
def posts(self) -> PostRepository:
|
||||
"""Возвращает репозиторий постов."""
|
||||
if self._post_repo is None:
|
||||
self._post_repo = PostRepository(self.db_path)
|
||||
return self._post_repo
|
||||
|
||||
@property
|
||||
def admins(self) -> AdminRepository:
|
||||
"""Возвращает репозиторий администраторов."""
|
||||
if self._admin_repo is None:
|
||||
self._admin_repo = AdminRepository(self.db_path)
|
||||
return self._admin_repo
|
||||
|
||||
@property
|
||||
def audio(self) -> AudioRepository:
|
||||
"""Возвращает репозиторий аудио."""
|
||||
if self._audio_repo is None:
|
||||
self._audio_repo = AudioRepository(self.db_path)
|
||||
return self._audio_repo
|
||||
|
||||
async def create_all_tables(self):
|
||||
"""Создает все таблицы в базе данных."""
|
||||
await self.users.create_tables()
|
||||
await self.blacklist.create_tables()
|
||||
await self.messages.create_tables()
|
||||
await self.posts.create_tables()
|
||||
await self.admins.create_tables()
|
||||
await self.audio.create_tables()
|
||||
|
||||
async def check_database_integrity(self):
|
||||
"""Проверяет целостность базы данных."""
|
||||
await self.users.check_database_integrity()
|
||||
|
||||
async def cleanup_wal_files(self):
|
||||
"""Очищает WAL файлы."""
|
||||
await self.users.cleanup_wal_files()
|
||||
Reference in New Issue
Block a user