backend optimizations + redundant commits cleanup

This commit is contained in:
Vladless
2026-04-19 22:53:14 +00:00
parent ed30a21842
commit bfd628b24e
67 changed files with 1157 additions and 1137 deletions
+24
View File
@@ -1225,6 +1225,28 @@ async def _migration_v23_add_identity_onboarding_stage(conn: AsyncConnection) ->
await _exec_ignore(conn, "ALTER TABLE identities ADD COLUMN onboarding_stage VARCHAR(32)")
async def _migration_v26_add_keys_indexes(conn: AsyncConnection) -> None:
logger.info("[schema_upgrade] v26: индексы keys(expiry_time/server_id/tariff_id)")
if not await _table_exists(conn, "keys"):
return
if not await _index_exists(conn, "keys", "ix_keys_expiry_time"):
await _exec_ignore(conn, "CREATE INDEX ix_keys_expiry_time ON keys(expiry_time)")
if not await _index_exists(conn, "keys", "ix_keys_server_id"):
await _exec_ignore(conn, "CREATE INDEX ix_keys_server_id ON keys(server_id)")
if not await _index_exists(conn, "keys", "ix_keys_tariff_id"):
await _exec_ignore(conn, "CREATE INDEX ix_keys_tariff_id ON keys(tariff_id)")
async def _migration_v25_add_partners_indexes(conn: AsyncConnection) -> None:
logger.info("[schema_upgrade] v25: индексы на partners(partner_tg_id/joined_tg_id)")
if not await _table_exists(conn, "partners"):
return
if not await _index_exists(conn, "partners", "ix_partners_partner_tg_id"):
await _exec_ignore(conn, "CREATE INDEX ix_partners_partner_tg_id ON partners(partner_tg_id)")
if not await _index_exists(conn, "partners", "ix_partners_joined_tg_id"):
await _exec_ignore(conn, "CREATE INDEX ix_partners_joined_tg_id ON partners(joined_tg_id)")
async def _migration_v24_add_identity_sessions(conn: AsyncConnection) -> None:
logger.info("[schema_upgrade] v24: таблица identity_sessions + перенос существующих токенов")
if not await _table_exists(conn, "identities"):
@@ -1302,6 +1324,8 @@ _MIGRATIONS = [
(22, "identities.onboarding_completed_at", _migration_v22_add_identity_onboarding_completed_at),
(23, "identities.onboarding_stage", _migration_v23_add_identity_onboarding_stage),
(24, "таблица identity_sessions (мультидевайс)", _migration_v24_add_identity_sessions),
(25, "индексы на partners(partner_tg_id/joined_tg_id)", _migration_v25_add_partners_indexes),
(26, "индексы keys(expiry_time/server_id/tariff_id)", _migration_v26_add_keys_indexes),
]
+3 -3
View File
@@ -13,11 +13,11 @@ class Key(DictLikeMixin, Base):
tg_id = Column(BigInteger, ForeignKey("users.tg_id"), nullable=True, index=True)
email = Column(String, unique=True)
created_at = Column(BigInteger)
expiry_time = Column(BigInteger)
expiry_time = Column(BigInteger, index=True)
key = Column(String)
server_id = Column(String)
server_id = Column(String, index=True)
remnawave_link = Column(String)
tariff_id = Column(Integer, ForeignKey("tariffs.id", ondelete="SET NULL"))
tariff_id = Column(Integer, ForeignKey("tariffs.id", ondelete="SET NULL"), index=True)
is_frozen = Column(Boolean, default=False)
alias = Column(String)
notified = Column(Boolean, default=False)
+13 -2
View File
@@ -1,21 +1,30 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from core.redis_cache import cache_delete, cache_get, cache_set
from database.models import Setting
_KEY = "CONTENT_REVISION"
_CACHE_KEY = "site_revision:value"
_CACHE_TTL_SEC = 30
async def get_site_revision(session: AsyncSession) -> int:
cached = await cache_get(_CACHE_KEY)
if isinstance(cached, int):
return cached
result = await session.execute(select(Setting).where(Setting.key == _KEY))
setting = result.scalar_one_or_none()
if setting is None:
await cache_set(_CACHE_KEY, 0, _CACHE_TTL_SEC)
return 0
try:
return int(setting.value or 0)
value = int(setting.value or 0)
except (TypeError, ValueError):
return 0
value = 0
await cache_set(_CACHE_KEY, value, _CACHE_TTL_SEC)
return value
async def bump_site_revision(session: AsyncSession) -> int:
@@ -23,10 +32,12 @@ async def bump_site_revision(session: AsyncSession) -> int:
setting = result.scalar_one_or_none()
if setting is None:
session.add(Setting(key=_KEY, value=1))
await cache_delete(_CACHE_KEY)
return 1
try:
current = int(setting.value or 0)
except (TypeError, ValueError):
current = 0
setting.value = current + 1
await cache_delete(_CACHE_KEY)
return current + 1
+11 -1
View File
@@ -1,16 +1,24 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from core.redis_cache import cache_delete, cache_get, cache_set
from database.models import Setting
_KEY = "SITE_INITIALIZED"
_CACHE_KEY = "site_state:initialized"
_CACHE_TTL_SEC = 300
async def is_site_initialized(session: AsyncSession) -> bool:
cached = await cache_get(_CACHE_KEY)
if isinstance(cached, bool):
return cached
result = await session.execute(select(Setting).where(Setting.key == _KEY))
setting = result.scalar_one_or_none()
return bool(setting and setting.value is True)
value = bool(setting and setting.value is True)
await cache_set(_CACHE_KEY, value, _CACHE_TTL_SEC)
return value
async def mark_site_initialized(session: AsyncSession) -> None:
@@ -21,6 +29,7 @@ async def mark_site_initialized(session: AsyncSession) -> None:
session.add(Setting(key=_KEY, value=True, description="Сайт прошёл первую настройку админом"))
elif setting.value is not True:
setting.value = True
await cache_delete(_CACHE_KEY)
async def reset_site_initialized(session: AsyncSession) -> None:
@@ -29,3 +38,4 @@ async def reset_site_initialized(session: AsyncSession) -> None:
setting = result.scalar_one_or_none()
if setting is not None:
setting.value = False
await cache_delete(_CACHE_KEY)