🌊 Волна 19: backend — 6 фиксов (hints + N+1 + logger)

This commit is contained in:
2026-09-25 10:09:13 +00:00
parent 3d75a00046
commit 555bf44db2
5 changed files with 115 additions and 79 deletions
+40 -37
View File
@@ -1,7 +1,11 @@
import logging
from fastapi import APIRouter, Request, HTTPException
from auth import require_auth
from database import get_pool
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/achievements", tags=["achievements"])
# ============ Данные 52 достижений ============
@@ -281,46 +285,45 @@ ACHIEVEMENTS_DATA = [
async def init_achievements():
"""Добавляет новые достижения, не трогая существующие"""
"""Добавляет/обновляет достижения одним bulk upsert."""
pool = await get_pool()
async with pool.acquire() as conn:
for a in ACHIEVEMENTS_DATA:
exists = await conn.fetchval(
"SELECT 1 FROM achievements WHERE code = $1", a["code"]
# 🔧 ФИКС (25.09.2026): init_achievements_n_plus_1 —
# 52 SELECT + 52 INSERT/UPDATE (104 запроса) → 1 bulk upsert.
# Требует UNIQUE constraint на achievements.code (achievements_code_key).
rows = [
(
a["code"], a["category"], a["icon"],
a["name_pl"], a["name_ru"], a["name_uk"], a["name_en"],
a["desc_pl"], a["desc_ru"], a["desc_uk"], a["desc_en"],
a["type"], a["value"],
)
if not exists:
await conn.execute(
"""INSERT INTO achievements (code, category, icon,
name_pl, name_ru, name_uk, name_en,
description_pl, description_ru, description_uk, description_en,
requirement_type, requirement_value)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)""",
a["code"], a["category"], a["icon"],
a["name_pl"], a["name_ru"], a["name_uk"], a["name_en"],
a["desc_pl"], a["desc_ru"], a["desc_uk"], a["desc_en"],
a["type"], a["value"]
)
else:
# 🔧 НОВОЕ: Обновляем существующие достижения (на случай изменения названий)
await conn.execute("""
UPDATE achievements SET
category = $2,
icon = $3,
name_pl = $4,
name_ru = $5,
name_uk = $6,
name_en = $7,
description_pl = $8,
description_ru = $9,
description_uk = $10,
description_en = $11,
requirement_type = $12,
requirement_value = $13
WHERE code = $1
""", a["code"], a["category"], a["icon"],
a["name_pl"], a["name_ru"], a["name_uk"], a["name_en"],
a["desc_pl"], a["desc_ru"], a["desc_uk"], a["desc_en"],
a["type"], a["value"])
for a in ACHIEVEMENTS_DATA
]
await conn.executemany("""
INSERT INTO achievements
(code, category, icon,
name_pl, name_ru, name_uk, name_en,
description_pl, description_ru, description_uk, description_en,
requirement_type, requirement_value)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
ON CONFLICT (code) DO UPDATE SET
category = EXCLUDED.category,
icon = EXCLUDED.icon,
name_pl = EXCLUDED.name_pl,
name_ru = EXCLUDED.name_ru,
name_uk = EXCLUDED.name_uk,
name_en = EXCLUDED.name_en,
description_pl = EXCLUDED.description_pl,
description_ru = EXCLUDED.description_ru,
description_uk = EXCLUDED.description_uk,
description_en = EXCLUDED.description_en,
requirement_type = EXCLUDED.requirement_type,
requirement_value = EXCLUDED.requirement_value
""", rows)
logger.info(f"[achievements] init: upserted {len(rows)} achievements")
@router.get("")