Исправлены ошибки в ходе раскатки
This commit is contained in:
@@ -24,7 +24,7 @@ from pydantic import BaseModel, ConfigDict, Field, model_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
from sqlalchemy import func, or_, select, text
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from app.models import (
|
||||
ConnectorSetup,
|
||||
@@ -36,6 +36,7 @@ from app.models import (
|
||||
PortalInstallation,
|
||||
now,
|
||||
)
|
||||
from app.postgres import create_postgres_engine
|
||||
|
||||
logger = logging.getLogger("bitrix-local-app")
|
||||
|
||||
@@ -135,10 +136,6 @@ class OutboundDto(BaseModel):
|
||||
message: MessageDto
|
||||
|
||||
|
||||
def pg_url(value: str) -> str:
|
||||
return value.replace("postgresql://", "postgresql+asyncpg://", 1)
|
||||
|
||||
|
||||
def canonical_fingerprint(value: dict[str, Any]) -> str:
|
||||
clean = json.loads(json.dumps(value, sort_keys=True, default=str))
|
||||
for file in clean.get("message", {}).get("files", []):
|
||||
@@ -416,8 +413,8 @@ def create_app(settings: Settings | None = None) -> FastAPI:
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
engine = create_async_engine(
|
||||
pg_url(cfg.bitrix_database_url),
|
||||
engine = create_postgres_engine(
|
||||
cfg.bitrix_database_url,
|
||||
pool_size=5,
|
||||
max_overflow=0,
|
||||
pool_pre_ping=True,
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import asyncpg
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
|
||||
|
||||
|
||||
def asyncpg_dsn(url: str) -> str:
|
||||
if url.startswith("postgresql+asyncpg://"):
|
||||
return url.replace("postgresql+asyncpg://", "postgresql://", 1)
|
||||
return url
|
||||
|
||||
|
||||
def create_postgres_engine(url: str, **engine_options: Any) -> AsyncEngine:
|
||||
dsn = asyncpg_dsn(url)
|
||||
|
||||
async def connect():
|
||||
return await asyncpg.connect(dsn=dsn)
|
||||
|
||||
return create_async_engine(
|
||||
"postgresql+asyncpg://",
|
||||
async_creator=connect,
|
||||
**engine_options,
|
||||
)
|
||||
Reference in New Issue
Block a user