Skip to content

Commit ec13266

Browse files
Refactor import order and improve database connection error handling
1 parent 7faa78c commit ec13266

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

stac_fastapi/pgstac/app.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from contextlib import asynccontextmanager
1010

1111
from brotli_asgi import BrotliMiddleware
12-
from fastapi import FastAPI, Request, APIRouter
13-
from fastapi.responses import ORJSONResponse, JSONResponse
12+
from fastapi import APIRouter, FastAPI, Request
13+
from fastapi.responses import JSONResponse, ORJSONResponse
1414
from stac_fastapi.api.app import StacApi
1515
from stac_fastapi.api.middleware import CORSMiddleware, ProxyHeaderMiddleware
1616
from stac_fastapi.api.models import (
@@ -175,18 +175,26 @@ async def ping(request: Request):
175175
async with request.app.state.get_connection(request, "r") as conn:
176176
# Execute a simple query to verify pgstac is ready
177177
# Check if we can query the migrations table which should exist in pgstac
178-
result = await conn.fetchval("SELECT 1 FROM pgstac.migrations LIMIT 1")
178+
result = await conn.fetchval(
179+
"SELECT 1 FROM pgstac.migrations LIMIT 1"
180+
)
179181
if result is not None:
180182
return {"message": "PONG", "database": "OK"}
181183
else:
182184
return JSONResponse(
183185
status_code=503,
184-
content={"message": "Database tables not found", "database": "ERROR"}
186+
content={
187+
"message": "Database tables not found",
188+
"database": "ERROR",
189+
},
185190
)
186191
except Exception as e:
187192
return JSONResponse(
188193
status_code=503,
189-
content={"message": f"Database connection failed: {str(e)}", "database": "ERROR"}
194+
content={
195+
"message": f"Database connection failed: {str(e)}",
196+
"database": "ERROR",
197+
},
190198
)
191199

192200
self.app.include_router(mgmt_router, tags=["Liveliness/Readiness"])

tests/api/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pypgstac.db import PgstacDB
1111
from pypgstac.load import Loader
1212
from pystac import Collection, Extent, Item, SpatialExtent, TemporalExtent
13-
from stac_fastapi.pgstac.app import PgStacApi
1413
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
1514
from stac_fastapi.extensions.core import (
1615
CollectionSearchExtension,
@@ -20,6 +19,7 @@
2019
from stac_fastapi.extensions.core.fields import FieldsConformanceClasses
2120
from stac_fastapi.types import stac as stac_types
2221

22+
from stac_fastapi.pgstac.app import PgStacApi
2323
from stac_fastapi.pgstac.config import PostgresSettings
2424
from stac_fastapi.pgstac.core import CoreCrudClient, Settings
2525
from stac_fastapi.pgstac.db import close_db_connection, connect_to_db

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from pypgstac.db import PgstacDB
1616
from pypgstac.migrate import Migrate
1717
from pytest_postgresql.janitor import DatabaseJanitor
18-
from stac_fastapi.pgstac.app import PgStacApi
1918
from stac_fastapi.api.models import (
2019
ItemCollectionUri,
2120
create_get_request_model,
@@ -41,6 +40,7 @@
4140
from stac_fastapi.extensions.third_party import BulkTransactionExtension
4241
from stac_pydantic import Collection, Item
4342

43+
from stac_fastapi.pgstac.app import PgStacApi
4444
from stac_fastapi.pgstac.config import PostgresSettings, Settings
4545
from stac_fastapi.pgstac.core import CoreCrudClient
4646
from stac_fastapi.pgstac.db import close_db_connection, connect_to_db

0 commit comments

Comments
 (0)