fix(api): store async postgresql pool as a fastapi state. (#1763)
* fix(api): store async postgresql pool as a fastapi state. * fix(api): avoid circular import
This commit is contained in:
parent
101b8ea62b
commit
2119761aa3
6 changed files with 9 additions and 34 deletions
|
|
@ -6,7 +6,7 @@ For the record, the expected psycopg3's async api looks like the
|
|||
following pseudo code:
|
||||
|
||||
```python
|
||||
async with orpy.get().database.connection() as cnx:
|
||||
async with app.state.postgresql.connection() as cnx:
|
||||
async with cnx.transaction():
|
||||
row = await cnx.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
row = await row.fetchone()
|
||||
|
|
@ -15,7 +15,7 @@ following pseudo code:
|
|||
|
||||
Minding the following:
|
||||
|
||||
- Where `orpy.get().database` is the postgresql connection pooler.
|
||||
- Where `app.state.postgresql` is the postgresql connection pooler.
|
||||
- Wrap explicit transaction with `async with cnx.transaction():
|
||||
foobar()`
|
||||
- Most of the time the transaction object is not used;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ from routers.subs import insights, metrics, v1_api, health, usability_tests
|
|||
loglevel = config("LOGLEVEL", default=logging.WARNING)
|
||||
print(f">Loglevel set to: {loglevel}")
|
||||
logging.basicConfig(level=loglevel)
|
||||
import orpy
|
||||
from psycopg.rows import dict_row
|
||||
|
||||
|
||||
|
|
@ -58,9 +57,7 @@ async def lifespan(app: FastAPI):
|
|||
}
|
||||
|
||||
database = psycopg_pool.AsyncConnectionPool(kwargs=database, connection_class=ORPYAsyncConnection)
|
||||
orpy.set(orpy.Application(
|
||||
database,
|
||||
))
|
||||
app.state.postgresql = database
|
||||
|
||||
# App listening
|
||||
yield
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import orpy
|
||||
from chalicelib.core import license
|
||||
from chalicelib.utils import helper
|
||||
from chalicelib.utils import pg_client
|
||||
|
|
@ -61,7 +60,8 @@ def tenants_exists_sync(use_pool=True):
|
|||
|
||||
|
||||
async def tenants_exists(use_pool=True):
|
||||
async with orpy.get().database.connection() as cnx:
|
||||
from app import app
|
||||
async with app.state.postgresql.connection() as cnx:
|
||||
async with cnx.transaction() as txn:
|
||||
row = await cnx.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
row = await row.fetchone()
|
||||
|
|
|
|||
20
api/orpy.py
20
api/orpy.py
|
|
@ -1,20 +0,0 @@
|
|||
from collections import namedtuple
|
||||
|
||||
Application = namedtuple(
|
||||
"Application",
|
||||
(
|
||||
"database",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
APPLICATION = None
|
||||
|
||||
|
||||
def set(application):
|
||||
global APPLICATION
|
||||
APPLICATION = application
|
||||
|
||||
|
||||
def get():
|
||||
return APPLICATION
|
||||
|
|
@ -29,7 +29,7 @@ from routers.subs import v1_api_ee
|
|||
loglevel = config("LOGLEVEL", default=logging.WARNING)
|
||||
print(f">Loglevel set to: {loglevel}")
|
||||
logging.basicConfig(level=loglevel)
|
||||
import orpy
|
||||
|
||||
from psycopg.rows import dict_row
|
||||
|
||||
|
||||
|
|
@ -69,9 +69,7 @@ async def lifespan(app: FastAPI):
|
|||
}
|
||||
|
||||
database = psycopg_pool.AsyncConnectionPool(kwargs=database, connection_class=ORPYAsyncConnection)
|
||||
orpy.set(orpy.Application(
|
||||
database,
|
||||
))
|
||||
app.state.postgresql = database
|
||||
|
||||
# App listening
|
||||
yield
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import orpy
|
||||
from chalicelib.core import license
|
||||
from chalicelib.utils import helper
|
||||
from chalicelib.utils import pg_client
|
||||
|
|
@ -87,7 +86,8 @@ def tenants_exists_sync(use_pool=True):
|
|||
|
||||
|
||||
async def tenants_exists(use_pool=True):
|
||||
async with orpy.get().database.connection() as cnx:
|
||||
from app import app
|
||||
async with app.state.postgresql.connection() as cnx:
|
||||
async with cnx.transaction() as txn:
|
||||
row = await cnx.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
row = await row.fetchone()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue