From 4afb4223bc7c8e4055705998b42190b35ede3e37 Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Tue, 21 Nov 2023 14:04:11 -0500 Subject: [PATCH] chore: update to bento_lib 10.1 + factor out new shared stuff --- bento_reference_service/config.py | 3 ++ bento_reference_service/db.py | 51 +++---------------------------- poetry.lock | 10 +++--- pyproject.toml | 2 +- 4 files changed, 14 insertions(+), 52 deletions(-) diff --git a/bento_reference_service/config.py b/bento_reference_service/config.py index 3251e27..300c570 100644 --- a/bento_reference_service/config.py +++ b/bento_reference_service/config.py @@ -1,5 +1,6 @@ import json +from bento_lib.logging import LogLevelLiteral from fastapi import Depends from functools import lru_cache from pathlib import Path @@ -37,6 +38,8 @@ class Config(BaseSettings): service_description: str = "Reference data (genomes & annotations) service for the Bento platform." service_url_base_path: str = "http://127.0.0.1:5000" # Base path to construct URIs from + log_level: LogLevelLiteral = "debug" + database_uri: str = "postgres://localhost:5432" data_path: Path = Path(__file__).parent / "data" diff --git a/bento_reference_service/db.py b/bento_reference_service/db.py index 9959285..609cdfe 100644 --- a/bento_reference_service/db.py +++ b/bento_reference_service/db.py @@ -1,11 +1,8 @@ -import aiofiles -import asyncpg -import contextlib - +from bento_lib.db.pg_async import PgAsyncDatabase from fastapi import Depends from functools import lru_cache from pathlib import Path -from typing import Annotated, AsyncGenerator +from typing import Annotated from .config import ConfigDependency @@ -13,48 +10,8 @@ SCHEMA_PATH = Path(__file__).parent / "sql" / "schema.sql" -class Database: - def __init__(self, db_uri: str, schema_path: Path): - self._db_uri: str = db_uri - self._schema_path: Path = schema_path - - self._pool: asyncpg.Pool | None = None - - async def initialize(self, pool_size: int = 10): - conn: asyncpg.Connection - - if not self._pool: # Initialize the connection pool if needed - self._pool = await asyncpg.create_pool(self._db_uri, min_size=pool_size, max_size=pool_size) - - # Connect to the database and execute the schema script - async with aiofiles.open(self._schema_path, "r") as sf: - async with self.connect() as conn: - async with conn.transaction(): - await conn.execute(await sf.read()) - - async def close(self): - if self._pool: - await self._pool.close() - self._pool = None - - @contextlib.asynccontextmanager - async def connect( - self, - existing_conn: asyncpg.Connection | None = None, - ) -> AsyncGenerator[asyncpg.Connection, None]: - # TODO: raise raise DatabaseError("Pool is not available") when FastAPI has lifespan dependencies - # + manage pool lifespan in lifespan fn. - - if self._pool is None: - await self.initialize() # initialize if this is the first time we're using the pool - - if existing_conn is not None: - yield existing_conn - return - - conn: asyncpg.Connection - async with self._pool.acquire() as conn: - yield conn +class Database(PgAsyncDatabase): + pass # TODO @lru_cache() diff --git a/poetry.lock b/poetry.lock index 6aa7394..7788774 100644 --- a/poetry.lock +++ b/poetry.lock @@ -264,17 +264,18 @@ tests-no-zope = ["cloudpickle", "hypothesis", "mypy (>=1.1.1)", "pympler", "pyte [[package]] name = "bento-lib" -version = "10.0.0" +version = "10.1.0" description = "A set of common utilities and helpers for Bento platform services." optional = false python-versions = ">=3.10.0" files = [ - {file = "bento_lib-10.0.0-py3-none-any.whl", hash = "sha256:f3142e2097ff470d5448697fade84d399c3c0eb8e5bfcb74b6c23565d82e60b7"}, - {file = "bento_lib-10.0.0.tar.gz", hash = "sha256:cb8b1786067eb516acfb5e0d9a4e49493928a15cff33f52ba9b8d7105332e030"}, + {file = "bento_lib-10.1.0-py3-none-any.whl", hash = "sha256:05d481ade19ea09716d7eea2c48c9d3969f728ad33af9b34fed74327f6cfb439"}, + {file = "bento_lib-10.1.0.tar.gz", hash = "sha256:3b51e507ed02cebd2c014ad52e2c2ace107510bcfacaef69d6f4498359e167a1"}, ] [package.dependencies] aiohttp = ">=3.8.4,<4" +asyncpg = {version = ">=0.29.0,<0.30.0", optional = true, markers = "extra == \"asyncpg\""} fastapi = {version = ">=0.100,<0.105", optional = true, markers = "extra == \"fastapi\""} jsonschema = ">=4.17.3,<5" psycopg2-binary = ">=2.9.5,<3.0" @@ -284,6 +285,7 @@ requests = ">=2.28.1,<3" Werkzeug = ">=2.2.3,<4" [package.extras] +asyncpg = ["asyncpg (>=0.29.0,<0.30.0)"] django = ["Django (>=4.2.1,<5)", "djangorestframework (>=3.14.0,<3.15)"] fastapi = ["fastapi (>=0.100,<0.105)"] flask = ["Flask (>=2.2.5,<4)"] @@ -2374,4 +2376,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.10.0" -content-hash = "6f91a8c4e852687d829309495bb7569c38b6d0e12b3efea96d4dfa30665eeb05" +content-hash = "6cc9fea9c0c243111d8217110599742abcb2aba7f8756a2a292480d28ba82ba9" diff --git a/pyproject.toml b/pyproject.toml index 46c9be6..dc37056 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ classifiers = [ python = "^3.10.0" fastapi = "^0.104.1" pydantic = "^2.5.1" -bento-lib = {extras = ["fastapi"], version = "^10.0.0"} +bento-lib = {extras = ["asyncpg", "fastapi"], version = "^10.1.0"} aiofiles = "^23.2.1" pysam = "^0.22.0" elasticsearch = {extras = ["async"], version = "^8.6.0"}