sa-warning-fix
Some checks failed
Deploy on push / deploy (push) Failing after 1m4s

This commit is contained in:
Untone 2024-02-25 20:58:48 +03:00
parent c7fe7f458c
commit 314c54969b
3 changed files with 34 additions and 38 deletions

View File

@ -45,13 +45,14 @@ async def get_author(_, _info, slug='', author_id=None):
if slug:
with local_session() as session:
aliased_author = aliased(Author)
q = select(aliased_author).filter(Author.slug == slug)
q = select(aliased_author).filter(aliased_author.slug == slug)
[author] = session.execute(q)
author_id = aliased_author.id
if author_id:
cache = await redis.execute('GET', f'id:{author_id}:author')
if not cache:
q = select(Author).where(Author.id == author_id)
aliased_author = aliased(Author)
q = select(aliased_author).where(aliased_author.id == author_id)
[author] = get_with_stat(q)
if author:
await update_author_cache(author)
@ -219,6 +220,6 @@ def get_author_followers(_, _info, slug: str):
@query.field('search_authors')
def search_authors(_, info, text: str):
q = search(select(Author), text)
def search_authors(_, info, t: str):
q = search(select(Author), t)
return get_with_stat(q)

View File

@ -50,7 +50,7 @@ def add_author_stat_columns(q):
)
.outerjoin(aliased_author_authors, aliased_author_authors.follower == Author.id)
.add_columns(
func.count(distinct(aliased_shout_author.author)).label('authors_stat')
func.count(distinct(aliased_author_authors.author)).label('authors_stat')
)
.outerjoin(
aliased_author_followers, aliased_author_followers.author == Author.id

View File

@ -14,23 +14,6 @@ from settings import DB_URL
import warnings
import traceback
# Функция для вывода полного трейсбека при предупреждениях
def warning_with_traceback(message, category, filename, lineno, line=None):
tb = traceback.format_stack()
tb_str = ''.join(tb)
return f'{message} ({filename}, {lineno}): {category.__name__}\n{tb_str}'
# Установка функции вывода трейсбека для предупреждений SQLAlchemy
warnings.formatwarning = warning_with_traceback
warnings.simplefilter('always', exc.SAWarning)
# Установка функции вывода трейсбека для предупреждений SQLAlchemy
warnings.showwarning = warning_with_traceback
warnings.simplefilter('always', exc.SAWarning)
# Подключение к базе данных SQLAlchemy
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)
inspector = inspect(engine)
@ -39,22 +22,6 @@ T = TypeVar('T')
REGISTRY: Dict[str, type] = {}
# Перехватчики для журнала запросов SQLAlchemy
@event.listens_for(Engine, 'before_cursor_execute')
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
conn.query_start_time = time.time()
# noinspection PyUnusedLocal
@event.listens_for(Engine, 'after_cursor_execute')
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
if hasattr(conn, '_query_start_time'):
elapsed = time.time() - conn.query_start_time
conn.query_start_time = None
query = f'{statement}'.replace('\n', ' ')
logger.debug(f"\n{query}\n{'*' * math.floor(elapsed)} {elapsed:.3f} s\n")
# noinspection PyUnusedLocal
def local_session(src=''):
return Session(bind=engine, expire_on_commit=False)
@ -92,3 +59,31 @@ class Base(declarative_base()):
make_searchable(Base.metadata)
Base.metadata.create_all(engine)
# Функция для вывода полного трейсбека при предупреждениях
def warning_with_traceback(message: Warning | str, category, filename: str, lineno: int, file=None, line=None):
tb = traceback.format_stack()
tb_str = ''.join(tb)
return f'{message} ({filename}, {lineno}): {category.__name__}\n{tb_str}'
# Установка функции вывода трейсбека для предупреждений SQLAlchemy
warnings.showwarning = warning_with_traceback
warnings.simplefilter('always', exc.SAWarning)
# Перехватчики для журнала запросов SQLAlchemy
@event.listens_for(Engine, 'before_cursor_execute')
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
conn.query_start_time = time.time()
# noinspection PyUnusedLocal
@event.listens_for(Engine, 'after_cursor_execute')
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
if hasattr(conn, '_query_start_time'):
elapsed = time.time() - conn.query_start_time
conn.query_start_time = None
query = f'{statement}'.replace('\n', ' ')
logger.debug(f"\n{query}\n{'*' * math.floor(elapsed)} {elapsed:.3f} s\n")