Spaces:
Sleeping
Sleeping
fix: disable asyncpg prepared statement caching and use NullPool for PgBouncer compatibility
Browse files- app/database.py +6 -4
- apply_schema_updates.py +13 -1
app/database.py
CHANGED
|
@@ -17,6 +17,7 @@ from typing import AsyncGenerator, Optional
|
|
| 17 |
from loguru import logger
|
| 18 |
from sqlalchemy import text
|
| 19 |
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
|
|
| 20 |
|
| 21 |
from app.config.settings import settings
|
| 22 |
|
|
@@ -39,11 +40,12 @@ else:
|
|
| 39 |
# Production PostgreSQL
|
| 40 |
engine = create_async_engine(
|
| 41 |
DATABASE_URL,
|
| 42 |
-
|
| 43 |
-
max_overflow=int(os.environ.get("DB_MAX_OVERFLOW", "20")),
|
| 44 |
-
pool_pre_ping=True,
|
| 45 |
echo=os.environ.get("SQL_ECHO", "false").lower() == "true",
|
| 46 |
-
connect_args={
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
logger.info("Connected to PostgreSQL (URL hidden for security)")
|
| 49 |
|
|
|
|
| 17 |
from loguru import logger
|
| 18 |
from sqlalchemy import text
|
| 19 |
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
| 20 |
+
from sqlalchemy.pool import NullPool
|
| 21 |
|
| 22 |
from app.config.settings import settings
|
| 23 |
|
|
|
|
| 40 |
# Production PostgreSQL
|
| 41 |
engine = create_async_engine(
|
| 42 |
DATABASE_URL,
|
| 43 |
+
poolclass=NullPool,
|
|
|
|
|
|
|
| 44 |
echo=os.environ.get("SQL_ECHO", "false").lower() == "true",
|
| 45 |
+
connect_args={
|
| 46 |
+
"statement_cache_size": 0,
|
| 47 |
+
"prepared_statement_cache_size": 0
|
| 48 |
+
}
|
| 49 |
)
|
| 50 |
logger.info("Connected to PostgreSQL (URL hidden for security)")
|
| 51 |
|
apply_schema_updates.py
CHANGED
|
@@ -3,6 +3,8 @@ import os
|
|
| 3 |
from sqlalchemy import text
|
| 4 |
from sqlalchemy.ext.asyncio import create_async_engine
|
| 5 |
|
|
|
|
|
|
|
| 6 |
# Load settings from environment/env file
|
| 7 |
def get_env_db_urls():
|
| 8 |
db_url_async = None
|
|
@@ -34,7 +36,17 @@ def get_env_db_urls():
|
|
| 34 |
|
| 35 |
async def update_database(db_url, is_sqlite=False):
|
| 36 |
print(f"\nChecking database: {db_url.split('@')[-1] if '@' in db_url else db_url}")
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
async with engine.connect() as conn:
|
| 40 |
# Check if talents table exists
|
|
|
|
| 3 |
from sqlalchemy import text
|
| 4 |
from sqlalchemy.ext.asyncio import create_async_engine
|
| 5 |
|
| 6 |
+
from sqlalchemy.pool import NullPool
|
| 7 |
+
|
| 8 |
# Load settings from environment/env file
|
| 9 |
def get_env_db_urls():
|
| 10 |
db_url_async = None
|
|
|
|
| 36 |
|
| 37 |
async def update_database(db_url, is_sqlite=False):
|
| 38 |
print(f"\nChecking database: {db_url.split('@')[-1] if '@' in db_url else db_url}")
|
| 39 |
+
if is_sqlite:
|
| 40 |
+
engine = create_async_engine(db_url)
|
| 41 |
+
else:
|
| 42 |
+
engine = create_async_engine(
|
| 43 |
+
db_url,
|
| 44 |
+
poolclass=NullPool,
|
| 45 |
+
connect_args={
|
| 46 |
+
"statement_cache_size": 0,
|
| 47 |
+
"prepared_statement_cache_size": 0
|
| 48 |
+
}
|
| 49 |
+
)
|
| 50 |
try:
|
| 51 |
async with engine.connect() as conn:
|
| 52 |
# Check if talents table exists
|