Skip to content

Commit

Permalink
fix: connection fixes (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin authored Aug 30, 2024
1 parent dac76e2 commit 8889740
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
18 changes: 9 additions & 9 deletions examples/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from typing import TYPE_CHECKING

import msgspec
from litestar import Controller, Litestar, get
from litestar.exceptions import InternalServerException
from litestar import Controller, Litestar, Request, get

from litestar_oracledb import AsyncDatabaseConfig, AsyncPoolConfig, OracleDatabasePlugin

Expand All @@ -18,14 +17,15 @@ class HealthCheck(msgspec.Struct):

class SampleController(Controller):
@get(path="/sample")
async def sample_route(self, db_connection: AsyncConnection) -> HealthCheck:
async def sample_route(self, request: Request, db_connection: AsyncConnection) -> HealthCheck:
"""Check database available and returns app config info."""
cursor = db_connection.cursor()
await cursor.execute("select 1 from dual")
result = await cursor.fetchone()
if result:
return HealthCheck(status="online")
raise InternalServerException
with db_connection.cursor() as cursor:
await cursor.execute("select 'a database value' a_column from dual")
result = await cursor.fetchone()
request.logger.info(result)
if result:
return HealthCheck(status="online")
return HealthCheck(status="offline")


oracledb = OracleDatabasePlugin(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ license = { text = "MIT" }
name = "litestar-oracledb"
readme = "README.md"
requires-python = ">=3.8"
version = "0.1.0"
version = "0.1.1"

[project.urls]
Changelog = "https://litestar-org.github.io/litesatr-oracledb/latest/changelog"
Expand Down
9 changes: 4 additions & 5 deletions src/litestar_oracledb/config/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from litestar.constants import HTTP_RESPONSE_START
from litestar.exceptions import ImproperlyConfiguredException
from litestar.types import Empty, EmptyType
from litestar.utils.dataclass import simple_asdict
from oracledb import create_pool_async as oracledb_create_pool
from oracledb.connection import AsyncConnection
Expand All @@ -26,7 +25,7 @@

from litestar import Litestar
from litestar.datastructures.state import State
from litestar.types import EmptyType, Message, Scope
from litestar.types import Message, Scope


def default_handler_maker(
Expand Down Expand Up @@ -122,7 +121,7 @@ class AsyncPoolConfig(GenericPoolConfig[AsyncConnectionPool, AsyncConnection]):
class AsyncDatabaseConfig(GenericDatabaseConfig[AsyncConnectionPool, AsyncConnection]):
"""Async Oracle database Configuration."""

pool_config: AsyncPoolConfig | None | EmptyType = Empty
pool_config: AsyncPoolConfig | None = None
"""Oracle Pool configuration"""

def __post_init__(self) -> None:
Expand All @@ -145,7 +144,7 @@ def pool_config_dict(self) -> dict[str, Any]:
A string keyed dict of config kwargs for the Asyncpg :func:`create_pool <oracledb.pool.create_pool>`
function.
"""
if self.pool_config is not None and self.pool_config != Empty:
if self.pool_config is not None:
return simple_asdict(self.pool_config, exclude_empty=True, convert_nested=False)
msg = "'pool_config' methods can not be used when a 'pool_instance' is provided."
raise ImproperlyConfiguredException(msg)
Expand Down Expand Up @@ -192,7 +191,7 @@ async def lifespan(
try:
yield
finally:
await db_pool.close(force=True)
await db_pool.close()

async def provide_connection(
self,
Expand Down
6 changes: 3 additions & 3 deletions src/litestar_oracledb/config/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ class GenericDatabaseConfig(Generic[PoolT, ConnectionT]):
If set, the plugin will use the provided pool rather than instantiate one.
"""
before_send_handler: (
BeforeMessageSendHookHandler | None | Literal["autocommit", "autocommit_include_redirects"] | EmptyType
) = Empty
before_send_handler: BeforeMessageSendHookHandler | None | Literal["autocommit", "autocommit_include_redirects"] = (
None
)
"""Handler to call before the ASGI message is sent.
The handler should handle closing the session stored in the ASGI scope, if it's still open, and committing and
Expand Down
9 changes: 4 additions & 5 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ async def test_lifespan(
@get("/")
async def health_check(db_connection: AsyncConnection) -> float:
"""Check database available and returns random number."""
cursor = db_connection.cursor()
await cursor.execute("select 1 as the_one from dual")
r = await cursor.fetchall()
cursor.close()
return r[0]["the_one"] # type: ignore
with db_connection.cursor() as cursor:
await cursor.execute("select 1 as the_one from dual")
r = await cursor.fetchall()
return r[0]["the_one"] # type: ignore

@asynccontextmanager
async def lifespan(_app: Litestar) -> AsyncGenerator[None, Any]:
Expand Down

0 comments on commit 8889740

Please sign in to comment.