2025-06-01 23:56:11 +00:00
|
|
|
|
import builtins
|
|
|
|
|
import logging
|
2024-10-14 09:19:30 +00:00
|
|
|
|
import math
|
2024-02-29 12:15:04 +00:00
|
|
|
|
import time
|
2024-04-08 07:38:58 +00:00
|
|
|
|
import traceback
|
|
|
|
|
import warnings
|
2025-06-01 23:56:11 +00:00
|
|
|
|
from io import TextIOWrapper
|
|
|
|
|
from typing import Any, ClassVar, Type, TypeVar, Union
|
2024-10-14 09:19:30 +00:00
|
|
|
|
|
2025-03-20 08:55:21 +00:00
|
|
|
|
import orjson
|
2024-11-01 17:24:09 +00:00
|
|
|
|
import sqlalchemy
|
2025-06-01 23:56:11 +00:00
|
|
|
|
from sqlalchemy import JSON, Column, Integer, create_engine, event, exc, func, inspect
|
|
|
|
|
from sqlalchemy.dialects.sqlite import insert
|
|
|
|
|
from sqlalchemy.engine import Connection, Engine
|
2025-04-20 22:22:08 +00:00
|
|
|
|
from sqlalchemy.orm import Session, configure_mappers, declarative_base, joinedload
|
2025-06-01 23:56:11 +00:00
|
|
|
|
from sqlalchemy.pool import StaticPool
|
2024-04-26 22:41:47 +00:00
|
|
|
|
|
2024-10-14 09:19:30 +00:00
|
|
|
|
from settings import DB_URL
|
|
|
|
|
from utils.logger import root_logger as logger
|
2024-04-26 22:41:47 +00:00
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
# Global variables
|
|
|
|
|
REGISTRY: dict[str, type["BaseModel"]] = {}
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
# Database configuration
|
|
|
|
|
engine = create_engine(DB_URL, echo=False, poolclass=StaticPool if "sqlite" in DB_URL else None)
|
|
|
|
|
ENGINE = engine # Backward compatibility alias
|
2024-10-14 06:12:20 +00:00
|
|
|
|
|
2024-02-25 13:43:04 +00:00
|
|
|
|
inspector = inspect(engine)
|
2024-02-25 15:08:02 +00:00
|
|
|
|
configure_mappers()
|
2024-04-17 15:32:23 +00:00
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
FILTERED_FIELDS = ["_sa_instance_state", "search_vector"]
|
2023-12-24 14:25:57 +00:00
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
# Создаем Base для внутреннего использования
|
|
|
|
|
_Base = declarative_base()
|
2024-02-21 15:07:02 +00:00
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
# Create proper type alias for Base
|
|
|
|
|
BaseType = Type[_Base] # type: ignore[valid-type]
|
2023-11-22 16:38:39 +00:00
|
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
class BaseModel(_Base): # type: ignore[valid-type,misc]
|
2023-01-31 07:44:06 +00:00
|
|
|
|
__abstract__ = True
|
2025-06-01 23:56:11 +00:00
|
|
|
|
__allow_unmapped__ = True
|
|
|
|
|
__table_args__: ClassVar[Union[dict[str, Any], tuple]] = {"extend_existing": True}
|
2023-01-31 07:44:06 +00:00
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def __init_subclass__(cls, **kwargs: Any) -> None:
|
2022-09-03 10:50:14 +00:00
|
|
|
|
REGISTRY[cls.__name__] = cls
|
2025-06-01 23:56:11 +00:00
|
|
|
|
super().__init_subclass__(**kwargs)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def dict(self, access: bool = False) -> builtins.dict[str, Any]:
|
2025-05-16 06:23:48 +00:00
|
|
|
|
"""
|
|
|
|
|
Конвертирует ORM объект в словарь.
|
|
|
|
|
|
|
|
|
|
Пропускает атрибуты, которые отсутствуют в объекте, но присутствуют в колонках таблицы.
|
|
|
|
|
Преобразует JSON поля в словари.
|
|
|
|
|
Добавляет синтетическое поле .stat, если оно существует.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Dict[str, Any]: Словарь с атрибутами объекта
|
|
|
|
|
"""
|
2024-05-30 04:12:00 +00:00
|
|
|
|
column_names = filter(lambda x: x not in FILTERED_FIELDS, self.__table__.columns.keys())
|
2024-08-07 07:30:51 +00:00
|
|
|
|
data = {}
|
2023-11-03 10:10:22 +00:00
|
|
|
|
try:
|
2024-08-07 07:30:51 +00:00
|
|
|
|
for column_name in column_names:
|
2025-05-16 06:23:48 +00:00
|
|
|
|
try:
|
|
|
|
|
# Проверяем, существует ли атрибут в объекте
|
|
|
|
|
if hasattr(self, column_name):
|
|
|
|
|
value = getattr(self, column_name)
|
|
|
|
|
# Проверяем, является ли значение JSON и декодируем его при необходимости
|
|
|
|
|
if isinstance(value, (str, bytes)) and isinstance(
|
|
|
|
|
self.__table__.columns[column_name].type, JSON
|
|
|
|
|
):
|
|
|
|
|
try:
|
|
|
|
|
data[column_name] = orjson.loads(value)
|
|
|
|
|
except (TypeError, orjson.JSONDecodeError) as e:
|
2025-06-01 23:56:11 +00:00
|
|
|
|
logger.exception(f"Error decoding JSON for column '{column_name}': {e}")
|
2025-05-16 06:23:48 +00:00
|
|
|
|
data[column_name] = value
|
|
|
|
|
else:
|
|
|
|
|
data[column_name] = value
|
|
|
|
|
else:
|
|
|
|
|
# Пропускаем атрибут, если его нет в объекте (может быть добавлен после миграции)
|
2025-05-29 09:37:39 +00:00
|
|
|
|
logger.debug(f"Skipping missing attribute '{column_name}' for {self.__class__.__name__}")
|
2025-05-16 06:23:48 +00:00
|
|
|
|
except AttributeError as e:
|
|
|
|
|
logger.warning(f"Attribute error for column '{column_name}': {e}")
|
|
|
|
|
# Добавляем синтетическое поле .stat если оно существует
|
2024-04-17 15:32:23 +00:00
|
|
|
|
if hasattr(self, "stat"):
|
|
|
|
|
data["stat"] = self.stat
|
2023-11-03 10:10:22 +00:00
|
|
|
|
except Exception as e:
|
2025-06-01 23:56:11 +00:00
|
|
|
|
logger.exception(f"Error occurred while converting object to dictionary: {e}")
|
2024-08-07 07:30:51 +00:00
|
|
|
|
return data
|
2023-11-22 16:38:39 +00:00
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def update(self, values: builtins.dict[str, Any]) -> None:
|
2023-11-22 16:38:39 +00:00
|
|
|
|
for key, value in values.items():
|
|
|
|
|
if hasattr(self, key):
|
|
|
|
|
setattr(self, key, value)
|
2024-02-19 10:16:44 +00:00
|
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
|
|
2024-04-26 22:41:47 +00:00
|
|
|
|
# make_searchable(Base.metadata)
|
2024-10-13 23:05:20 +00:00
|
|
|
|
# Base.metadata.create_all(bind=engine)
|
2024-02-25 17:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Функция для вывода полного трейсбека при предупреждениях
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def warning_with_traceback(
|
|
|
|
|
message: Warning | str,
|
|
|
|
|
category: type[Warning],
|
|
|
|
|
filename: str,
|
|
|
|
|
lineno: int,
|
|
|
|
|
file: TextIOWrapper | None = None,
|
|
|
|
|
line: str | None = None,
|
|
|
|
|
) -> None:
|
2024-02-25 17:58:48 +00:00
|
|
|
|
tb = traceback.format_stack()
|
2024-04-17 15:32:23 +00:00
|
|
|
|
tb_str = "".join(tb)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
print(f"{message} ({filename}, {lineno}): {category.__name__}\n{tb_str}")
|
2024-02-25 17:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Установка функции вывода трейсбека для предупреждений SQLAlchemy
|
2025-06-01 23:56:11 +00:00
|
|
|
|
warnings.showwarning = warning_with_traceback # type: ignore[assignment]
|
2024-04-17 15:32:23 +00:00
|
|
|
|
warnings.simplefilter("always", exc.SAWarning)
|
2024-02-25 17:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
2024-08-12 08:00:01 +00:00
|
|
|
|
# Функция для извлечения SQL-запроса из контекста
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def get_statement_from_context(context: Connection) -> str | None:
|
2024-10-14 09:19:30 +00:00
|
|
|
|
query = ""
|
2025-06-01 23:56:11 +00:00
|
|
|
|
compiled = getattr(context, "compiled", None)
|
2024-10-14 06:12:20 +00:00
|
|
|
|
if compiled:
|
2025-06-01 23:56:11 +00:00
|
|
|
|
compiled_statement = getattr(compiled, "string", None)
|
|
|
|
|
compiled_parameters = getattr(compiled, "params", None)
|
2024-10-14 06:12:20 +00:00
|
|
|
|
if compiled_statement:
|
|
|
|
|
if compiled_parameters:
|
|
|
|
|
try:
|
|
|
|
|
# Безопасное форматирование параметров
|
|
|
|
|
query = compiled_statement % compiled_parameters
|
|
|
|
|
except Exception as e:
|
2025-06-01 23:56:11 +00:00
|
|
|
|
logger.exception(f"Error formatting query: {e}")
|
2024-10-14 06:12:20 +00:00
|
|
|
|
else:
|
|
|
|
|
query = compiled_statement
|
|
|
|
|
if query:
|
|
|
|
|
query = query.replace("\n", " ").replace(" ", " ").replace(" ", " ").strip()
|
|
|
|
|
return query
|
2024-08-12 08:00:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Обработчик события перед выполнением запроса
|
2024-04-17 15:32:23 +00:00
|
|
|
|
@event.listens_for(Engine, "before_cursor_execute")
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def before_cursor_execute(
|
|
|
|
|
conn: Connection,
|
|
|
|
|
cursor: Any,
|
|
|
|
|
statement: str,
|
|
|
|
|
parameters: dict[str, Any] | None,
|
|
|
|
|
context: Connection,
|
|
|
|
|
executemany: bool,
|
|
|
|
|
) -> None:
|
|
|
|
|
conn.query_start_time = time.time() # type: ignore[attr-defined]
|
|
|
|
|
conn.cursor_id = id(cursor) # type: ignore[attr-defined]
|
2024-02-25 17:58:48 +00:00
|
|
|
|
|
2024-03-28 16:08:55 +00:00
|
|
|
|
|
2024-08-12 08:00:01 +00:00
|
|
|
|
# Обработчик события после выполнения запроса
|
2024-04-17 15:32:23 +00:00
|
|
|
|
@event.listens_for(Engine, "after_cursor_execute")
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def after_cursor_execute(
|
|
|
|
|
conn: Connection,
|
|
|
|
|
cursor: Any,
|
|
|
|
|
statement: str,
|
|
|
|
|
parameters: dict[str, Any] | None,
|
|
|
|
|
context: Connection,
|
|
|
|
|
executemany: bool,
|
|
|
|
|
) -> None:
|
2024-10-14 06:12:20 +00:00
|
|
|
|
if hasattr(conn, "cursor_id") and conn.cursor_id == id(cursor):
|
|
|
|
|
query = get_statement_from_context(context)
|
|
|
|
|
if query:
|
2025-06-01 23:56:11 +00:00
|
|
|
|
elapsed = time.time() - getattr(conn, "query_start_time", time.time())
|
2024-10-14 06:12:20 +00:00
|
|
|
|
if elapsed > 1:
|
|
|
|
|
query_end = query[-16:]
|
|
|
|
|
query = query.split(query_end)[0] + query_end
|
|
|
|
|
logger.debug(query)
|
|
|
|
|
elapsed_n = math.floor(elapsed)
|
2024-10-14 09:19:30 +00:00
|
|
|
|
logger.debug("*" * (elapsed_n))
|
2024-10-14 06:12:20 +00:00
|
|
|
|
logger.debug(f"{elapsed:.3f} s")
|
2025-06-01 23:56:11 +00:00
|
|
|
|
if hasattr(conn, "cursor_id"):
|
|
|
|
|
delattr(conn, "cursor_id") # Удаление идентификатора курсора после выполнения
|
2024-11-01 17:11:58 +00:00
|
|
|
|
|
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def get_json_builder() -> tuple[Any, Any, Any]:
|
2024-11-01 17:11:58 +00:00
|
|
|
|
"""
|
|
|
|
|
Возвращает подходящие функции для построения JSON объектов в зависимости от драйвера БД
|
|
|
|
|
"""
|
|
|
|
|
dialect = engine.dialect.name
|
2024-11-01 17:24:09 +00:00
|
|
|
|
json_cast = lambda x: x # noqa: E731
|
|
|
|
|
if dialect.startswith("postgres"):
|
|
|
|
|
json_cast = lambda x: func.cast(x, sqlalchemy.Text) # noqa: E731
|
|
|
|
|
return func.json_build_object, func.json_agg, json_cast
|
2025-06-01 23:56:11 +00:00
|
|
|
|
if dialect.startswith(("sqlite", "mysql")):
|
2024-11-01 17:24:09 +00:00
|
|
|
|
return func.json_object, func.json_group_array, json_cast
|
2025-06-01 23:56:11 +00:00
|
|
|
|
msg = f"JSON builder not implemented for dialect {dialect}"
|
|
|
|
|
raise NotImplementedError(msg)
|
2024-11-01 17:11:58 +00:00
|
|
|
|
|
2024-11-01 17:24:09 +00:00
|
|
|
|
|
2024-11-01 17:11:58 +00:00
|
|
|
|
# Используем их в коде
|
2024-11-01 17:24:09 +00:00
|
|
|
|
json_builder, json_array_builder, json_cast = get_json_builder()
|
2025-03-05 20:32:34 +00:00
|
|
|
|
|
2025-04-20 22:22:08 +00:00
|
|
|
|
# Fetch all shouts, with authors preloaded
|
|
|
|
|
# This function is used for search indexing
|
|
|
|
|
|
2025-05-29 09:37:39 +00:00
|
|
|
|
|
2025-06-02 18:50:58 +00:00
|
|
|
|
def fetch_all_shouts(session: Session | None = None) -> list[Any]:
|
2025-04-20 22:22:08 +00:00
|
|
|
|
"""Fetch all published shouts for search indexing with authors preloaded"""
|
2025-03-05 20:32:34 +00:00
|
|
|
|
from orm.shout import Shout
|
2025-05-29 09:37:39 +00:00
|
|
|
|
|
2025-03-05 20:32:34 +00:00
|
|
|
|
close_session = False
|
|
|
|
|
if session is None:
|
|
|
|
|
session = local_session()
|
|
|
|
|
close_session = True
|
2025-05-29 09:37:39 +00:00
|
|
|
|
|
2025-03-05 20:32:34 +00:00
|
|
|
|
try:
|
2025-04-20 22:22:08 +00:00
|
|
|
|
# Fetch only published and non-deleted shouts with authors preloaded
|
2025-05-29 09:37:39 +00:00
|
|
|
|
query = (
|
|
|
|
|
session.query(Shout)
|
|
|
|
|
.options(joinedload(Shout.authors))
|
2025-06-01 23:56:11 +00:00
|
|
|
|
.filter(Shout.published_at is not None, Shout.deleted_at is None)
|
2025-03-05 20:32:34 +00:00
|
|
|
|
)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
return query.all()
|
2025-03-05 20:32:34 +00:00
|
|
|
|
except Exception as e:
|
2025-06-01 23:56:11 +00:00
|
|
|
|
logger.exception(f"Error fetching shouts for search indexing: {e}")
|
2025-03-05 20:32:34 +00:00
|
|
|
|
return []
|
|
|
|
|
finally:
|
|
|
|
|
if close_session:
|
2025-06-02 18:50:58 +00:00
|
|
|
|
# Подавляем SQLAlchemy deprecated warning для синхронной сессии
|
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
|
|
|
|
session.close()
|
2025-06-01 23:56:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_column_names_without_virtual(model_cls: type[BaseModel]) -> list[str]:
|
|
|
|
|
"""Получает имена колонок модели без виртуальных полей"""
|
|
|
|
|
try:
|
|
|
|
|
column_names: list[str] = [
|
|
|
|
|
col.name for col in model_cls.__table__.columns if not getattr(col, "_is_virtual", False)
|
|
|
|
|
]
|
|
|
|
|
return column_names
|
|
|
|
|
except AttributeError:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_primary_key_columns(model_cls: type[BaseModel]) -> list[str]:
|
|
|
|
|
"""Получает имена первичных ключей модели"""
|
|
|
|
|
try:
|
|
|
|
|
return [col.name for col in model_cls.__table__.primary_key.columns]
|
|
|
|
|
except AttributeError:
|
|
|
|
|
return ["id"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_table_if_not_exists(engine: Engine, model_cls: type[BaseModel]) -> None:
|
|
|
|
|
"""Creates table for the given model if it doesn't exist"""
|
|
|
|
|
if hasattr(model_cls, "__tablename__"):
|
|
|
|
|
inspector = inspect(engine)
|
|
|
|
|
if not inspector.has_table(model_cls.__tablename__):
|
|
|
|
|
model_cls.__table__.create(engine)
|
|
|
|
|
logger.info(f"Created table: {model_cls.__tablename__}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_sql_warning(
|
|
|
|
|
message: str | Warning,
|
|
|
|
|
category: type[Warning],
|
|
|
|
|
filename: str,
|
|
|
|
|
lineno: int,
|
|
|
|
|
file: TextIOWrapper | None = None,
|
|
|
|
|
line: str | None = None,
|
|
|
|
|
) -> str:
|
|
|
|
|
"""Custom warning formatter for SQL warnings"""
|
|
|
|
|
return f"SQL Warning: {message}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Apply the custom warning formatter
|
|
|
|
|
def _set_warning_formatter() -> None:
|
|
|
|
|
"""Set custom warning formatter"""
|
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
|
|
original_formatwarning = warnings.formatwarning
|
|
|
|
|
|
|
|
|
|
def custom_formatwarning(
|
|
|
|
|
message: Warning | str,
|
|
|
|
|
category: type[Warning],
|
|
|
|
|
filename: str,
|
|
|
|
|
lineno: int,
|
|
|
|
|
file: TextIOWrapper | None = None,
|
|
|
|
|
line: str | None = None,
|
|
|
|
|
) -> str:
|
|
|
|
|
return format_sql_warning(message, category, filename, lineno, file, line)
|
|
|
|
|
|
|
|
|
|
warnings.formatwarning = custom_formatwarning # type: ignore[assignment]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_set_warning_formatter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upsert_on_duplicate(table: sqlalchemy.Table, **values: Any) -> sqlalchemy.sql.Insert:
|
|
|
|
|
"""
|
|
|
|
|
Performs an upsert operation (insert or update on conflict)
|
|
|
|
|
"""
|
|
|
|
|
if engine.dialect.name == "sqlite":
|
|
|
|
|
return insert(table).values(**values).on_conflict_do_update(index_elements=["id"], set_=values)
|
|
|
|
|
# For other databases, implement appropriate upsert logic
|
|
|
|
|
return table.insert().values(**values)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_sql_functions() -> dict[str, Any]:
|
|
|
|
|
"""Returns database-specific SQL functions"""
|
|
|
|
|
if engine.dialect.name == "sqlite":
|
|
|
|
|
return {
|
|
|
|
|
"now": sqlalchemy.func.datetime("now"),
|
|
|
|
|
"extract_epoch": lambda x: sqlalchemy.func.strftime("%s", x),
|
|
|
|
|
"coalesce": sqlalchemy.func.coalesce,
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
"now": sqlalchemy.func.now(),
|
|
|
|
|
"extract_epoch": sqlalchemy.func.extract("epoch", sqlalchemy.text("?")),
|
|
|
|
|
"coalesce": sqlalchemy.func.coalesce,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
|
def local_session(src: str = "") -> Session:
|
|
|
|
|
"""Create a new database session"""
|
|
|
|
|
return Session(bind=engine, expire_on_commit=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Export Base for backward compatibility
|
|
|
|
|
Base = _Base
|
|
|
|
|
# Also export the type for type hints
|
|
|
|
|
__all__ = ["Base", "BaseModel", "BaseType", "engine", "local_session"]
|