Spaces:
Paused
Paused
fix: remove drop_all from init_db, add pool_pre_ping for PostgreSQL
Browse filesThe drop_all on every startup caused InvalidCachedStatementError due to
stale prepared statements in the connection pool. Use create_all only
(idempotent) and enable pool_pre_ping to detect stale connections.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- outlook2api/admin_routes.py +8 -13
- outlook2api/database.py +5 -4
outlook2api/admin_routes.py
CHANGED
|
@@ -145,19 +145,14 @@ async def create_account(
|
|
| 145 |
password = body.get("password", "").strip()
|
| 146 |
if not email or not password:
|
| 147 |
raise HTTPException(status_code=400, detail="Email and password required")
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
return account.to_dict()
|
| 157 |
-
except HTTPException:
|
| 158 |
-
raise
|
| 159 |
-
except Exception as e:
|
| 160 |
-
raise HTTPException(status_code=500, detail=f"{type(e).__name__}: {e}")
|
| 161 |
|
| 162 |
|
| 163 |
@admin_router.post("/accounts/bulk")
|
|
|
|
| 145 |
password = body.get("password", "").strip()
|
| 146 |
if not email or not password:
|
| 147 |
raise HTTPException(status_code=400, detail="Email and password required")
|
| 148 |
+
existing = (await db.execute(select(Account).where(Account.email == email))).scalar_one_or_none()
|
| 149 |
+
if existing:
|
| 150 |
+
raise HTTPException(status_code=409, detail="Account already exists")
|
| 151 |
+
account = Account(email=email, password=password, source="manual")
|
| 152 |
+
db.add(account)
|
| 153 |
+
await db.commit()
|
| 154 |
+
await db.refresh(account)
|
| 155 |
+
return account.to_dict()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
|
| 158 |
@admin_router.post("/accounts/bulk")
|
outlook2api/database.py
CHANGED
|
@@ -78,12 +78,13 @@ async def init_db() -> None:
|
|
| 78 |
connect_args: dict = {"check_same_thread": False} if is_sqlite else {}
|
| 79 |
if not is_sqlite and _needs_ssl():
|
| 80 |
connect_args["ssl"] = _ssl.create_default_context()
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
_session_factory = async_sessionmaker(_engine, expire_on_commit=False)
|
| 83 |
async with _engine.begin() as conn:
|
| 84 |
-
# Drop and recreate to handle column type changes (safe when DB is empty)
|
| 85 |
-
if not is_sqlite:
|
| 86 |
-
await conn.run_sync(Base.metadata.drop_all)
|
| 87 |
await conn.run_sync(Base.metadata.create_all)
|
| 88 |
|
| 89 |
|
|
|
|
| 78 |
connect_args: dict = {"check_same_thread": False} if is_sqlite else {}
|
| 79 |
if not is_sqlite and _needs_ssl():
|
| 80 |
connect_args["ssl"] = _ssl.create_default_context()
|
| 81 |
+
# Disable prepared statement cache to avoid stale plan errors after schema changes
|
| 82 |
+
engine_kwargs: dict = {"echo": False, "connect_args": connect_args}
|
| 83 |
+
if not is_sqlite:
|
| 84 |
+
engine_kwargs["pool_pre_ping"] = True
|
| 85 |
+
_engine = create_async_engine(url, **engine_kwargs)
|
| 86 |
_session_factory = async_sessionmaker(_engine, expire_on_commit=False)
|
| 87 |
async with _engine.begin() as conn:
|
|
|
|
|
|
|
|
|
|
| 88 |
await conn.run_sync(Base.metadata.create_all)
|
| 89 |
|
| 90 |
|