Skip to content

Commit 43176f9

Browse files
Revert "feat: implement postgres database connection pooling"
This reverts commit 7cf67cd.
1 parent 7cf67cd commit 43176f9

4 files changed

Lines changed: 26 additions & 99 deletions

File tree

ohsome_quality_api/api/api.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import json
22
import os
3-
from collections.abc import AsyncIterator
43
from typing import Any, Union
54

65
from fastapi import Depends, FastAPI, HTTPException, Request, status
7-
from fastapi.concurrency import asynccontextmanager
86
from fastapi.encoders import jsonable_encoder
97
from fastapi.exceptions import RequestValidationError
108
from fastapi.middleware.cors import CORSMiddleware
@@ -48,7 +46,6 @@
4846
)
4947
from ohsome_quality_api.attributes.definitions import get_attributes, load_attributes
5048
from ohsome_quality_api.definitions import ATTRIBUTION_URL
51-
from ohsome_quality_api.geodatabase.client import create_pool
5249
from ohsome_quality_api.indicators.definitions import (
5350
IndicatorEnum,
5451
IndicatorEnumRequest,
@@ -106,12 +103,6 @@
106103
)
107104

108105

109-
@asynccontextmanager
110-
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
111-
async with create_pool():
112-
yield
113-
114-
115106
app = FastAPI(
116107
title=__title__,
117108
description=description,
@@ -123,7 +114,6 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
123114
openapi_tags=TAGS_METADATA,
124115
docs_url=None,
125116
redoc_url=None,
126-
lifespan=lifespan,
127117
dependencies=[
128118
Depends(set_request_context),
129119
Depends(i18n),
@@ -370,7 +360,6 @@ async def _post_indicator(
370360
if topic.key == "custom-topic":
371361
topic.filter = topic_filter
372362
topic.name = topic_name
373-
# TODO: Run topic validator again
374363

375364
indicators = await main.create_indicator(key=key, topic=topic, **parameters_)
376365

ohsome_quality_api/geodatabase/client.py

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
import logging
1818
import os
1919
from contextlib import asynccontextmanager
20-
from contextvars import ContextVar
2120
from typing import Literal
2221

2322
import asyncpg
2423
import geojson
25-
from asyncpg import Pool, Record
24+
from asyncpg import Record
2625
from geojson import Feature, FeatureCollection, MultiPolygon
2726

2827
from ohsome_quality_api.config import get_config_value
@@ -32,61 +31,40 @@
3231
WORKING_DIR = os.path.dirname(os.path.abspath(__file__))
3332

3433

35-
OQAPIDB_POOL: ContextVar[Pool] = ContextVar("OQAPIDB_POOL")
36-
OHSOMEDB_POOL: ContextVar[Pool] = ContextVar("OHSOMEDB_POOL")
37-
38-
3934
def log_query(record):
4035
logger.debug("Query:\n" + record.query)
4136
logger.debug("Args:\n" + str(record.args))
4237

4338

44-
@asynccontextmanager
45-
async def create_pool():
46-
# DSN in libpq connection URI format
47-
oqapidb_dsn = "postgres://{user}:{password}@{host}:{port}/{database}".format(
48-
host=get_config_value("postgres_host"),
49-
port=get_config_value("postgres_port"),
50-
database=get_config_value("postgres_db"),
51-
user=get_config_value("postgres_user"),
52-
password=get_config_value("postgres_password"),
53-
)
54-
ohsomedb_dsn = "postgres://{user}:{password}@{host}:{port}/{database}".format(
55-
host=get_config_value("ohsomedb_host"),
56-
port=get_config_value("ohsomedb_port"),
57-
database=get_config_value("ohsomedb_db"),
58-
user=get_config_value("ohsomedb_user"),
59-
password=get_config_value("ohsomedb_password"),
60-
)
61-
async with (
62-
asyncpg.create_pool(oqapidb_dsn) as oqapidb_pool,
63-
asyncpg.create_pool(ohsomedb_dsn) as ohsomedb_pool,
64-
):
65-
sql = 'set search_path to "global_2026-04-13",public'
66-
await oqapidb_pool.execute(sql)
67-
await ohsomedb_pool.execute(sql)
68-
oqapidb_pool_token = OQAPIDB_POOL.set(oqapidb_pool)
69-
ohsomedb_pool_token = OHSOMEDB_POOL.set(ohsomedb_pool)
70-
try:
71-
yield
72-
finally:
73-
OQAPIDB_POOL.reset(oqapidb_pool_token)
74-
OHSOMEDB_POOL.reset(ohsomedb_pool_token)
75-
76-
7739
@asynccontextmanager
7840
async def get_connection(database: Literal["oqapidb", "ohsomedb"] = "oqapidb"):
41+
# DNS in libpq connection URI format
7942
match database:
8043
case "oqapidb":
81-
pool = OQAPIDB_POOL.get()
44+
dns = "postgres://{user}:{password}@{host}:{port}/{database}".format(
45+
host=get_config_value("postgres_host"),
46+
port=get_config_value("postgres_port"),
47+
database=get_config_value("postgres_db"),
48+
user=get_config_value("postgres_user"),
49+
password=get_config_value("postgres_password"),
50+
)
8251
case "ohsomedb":
83-
pool = OHSOMEDB_POOL.get()
84-
async with pool.acquire() as conn:
85-
try:
86-
with conn.query_logger(log_query):
87-
yield conn
88-
finally:
89-
await conn.close()
52+
dns = "postgres://{user}:{password}@{host}:{port}/{database}".format(
53+
host=get_config_value("ohsomedb_host"),
54+
port=get_config_value("ohsomedb_port"),
55+
database=get_config_value("ohsomedb_db"),
56+
user=get_config_value("ohsomedb_user"),
57+
password=get_config_value("ohsomedb_password"),
58+
)
59+
case _:
60+
raise ValueError()
61+
conn = await asyncpg.connect(dns)
62+
await conn.execute('set search_path to "global_2026-04-13",public')
63+
try:
64+
with conn.query_logger(log_query):
65+
yield conn
66+
finally:
67+
await conn.close()
9068

9169

9270
async def fetch(

tests/conftest.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import json
22
import logging
33
import os
4-
from contextlib import asynccontextmanager
54
from pathlib import Path
65

7-
import asyncpg
86
import geojson
97
import pytest
108
from _pytest.monkeypatch import MonkeyPatch
119
from fastapi_i18n.main import Translator, translator
1210
from geojson import Feature, FeatureCollection, Polygon
1311

1412
from ohsome_quality_api.attributes.models import Attribute
15-
from ohsome_quality_api.config import get_config_value
1613
from ohsome_quality_api.indicators.currentness.indicator import Bin
1714
from ohsome_quality_api.indicators.definitions import (
1815
get_indicator,
@@ -39,43 +36,6 @@
3936
# https://github.com/pytest-dev/pytest/issues/5502
4037
logger = logging.getLogger("rpy2")
4138
logger.propagate = False
42-
vcr_logger = logging.getLogger("vcr")
43-
vcr_logger.setLevel(logging.DEBUG)
44-
vcr_logger.propagate = False
45-
46-
47-
# TODO: remove once ohsomedb has been replaced by ohsome-api
48-
@pytest.fixture(autouse=True)
49-
def get_connection(monkeypatch):
50-
@asynccontextmanager
51-
async def get_connection_(database="oqapidb"):
52-
match database:
53-
case "oqapidb":
54-
dsn = "postgres://{user}:{password}@{host}:{port}/{database}".format(
55-
host=get_config_value("postgres_host"),
56-
port=get_config_value("postgres_port"),
57-
database=get_config_value("postgres_db"),
58-
user=get_config_value("postgres_user"),
59-
password=get_config_value("postgres_password"),
60-
)
61-
case "ohsomedb":
62-
dsn = "postgres://{user}:{password}@{host}:{port}/{database}".format(
63-
host=get_config_value("ohsomedb_host"),
64-
port=get_config_value("ohsomedb_port"),
65-
database=get_config_value("ohsomedb_db"),
66-
user=get_config_value("ohsomedb_user"),
67-
password=get_config_value("ohsomedb_password"),
68-
)
69-
connection = await asyncpg.connect(dsn)
70-
try:
71-
yield connection
72-
finally:
73-
await connection.close()
74-
75-
monkeypatch.setattr(
76-
"ohsome_quality_api.geodatabase.client.get_connection",
77-
get_connection_,
78-
)
7939

8040

8141
@pytest.fixture(scope="class")

tests/integrationtests/api/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ohsome_quality_api.api.api import app
66

77

8-
@pytest.fixture(scope="module")
8+
@pytest.fixture
99
def client():
1010
return TestClient(app)
1111

0 commit comments

Comments
 (0)