-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
58 lines (45 loc) · 1.71 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
import asyncio
import logging
import asyncpg.connect_utils
from config import config
from config import DefaultConfig
log = logging.getLogger(__name__)
class NoResetConnection(asyncpg.connection.Connection):
def __init__(
self,
protocol: asyncpg.protocol.protocol.BaseProtocol,
transport: object,
loop: asyncio.AbstractEventLoop,
addr: tuple[str, int] | str,
config: asyncpg.connect_utils._ClientConfiguration,
params: asyncpg.connect_utils._ConnectionParameters,
) -> None:
super().__init__(protocol, transport, loop, addr, config, params)
self._reset_query: list[str] = []
class DatabaseLifecycleHandler:
def __init__(
self,
conf: DefaultConfig,
):
self._pool: asyncpg.Pool | None = None
self._config: DefaultConfig = conf
async def connect(self):
log.debug("connecting to database")
self._pool = await asyncpg.create_pool(
dsn=self._config.DATABASE_URL,
server_settings={"application_name": "notiteams-activity-api"},
connection_class=NoResetConnection,
min_size=self._config.DATABASE_POOL_MIN_SIZE,
max_size=self._config.DATABASE_POOL_MAX_SIZE,
)
# Simple check at startup, will validate database resolution and creds
async with await self.acquire() as connection:
await connection.fetchval("SELECT 1")
async def disconnect(self):
if self._pool:
await self._pool.close()
async def acquire(self) -> asyncpg.pool.PoolAcquireContext:
assert self._pool is not None
return self._pool.acquire()
database = DatabaseLifecycleHandler(config)