-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from EduardSchwarzkopf/fix-isolate-db-sessions
Fix isolate db sessions
- Loading branch information
Showing
25 changed files
with
279 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,6 @@ | ||
from typing import Optional | ||
|
||
from fastapi_users.db import SQLAlchemyUserDatabase | ||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine | ||
from sqlalchemy.orm import sessionmaker | ||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine | ||
|
||
from app.config import settings | ||
|
||
|
||
class Database: | ||
def __init__(self, url: str) -> None: | ||
""" | ||
Initialize the database connection. | ||
Args: | ||
url (str): The URL of the database. | ||
Raises: | ||
None | ||
""" | ||
|
||
self.url = url | ||
self.engine = create_async_engine(url, future=True) | ||
self._session: Optional[AsyncSession] = None | ||
|
||
async def init(self): | ||
""" | ||
Initialize the database connection. | ||
Raises: | ||
None | ||
""" | ||
|
||
self._session = self.get_session() | ||
|
||
def get_session(self) -> AsyncSession: | ||
""" | ||
Get the asynchronous session for the database. | ||
Returns: | ||
AsyncSession: The asynchronous session for the database. | ||
Raises: | ||
RuntimeError: If the engine has not been initialized. | ||
""" | ||
|
||
if self.engine is None: | ||
raise RuntimeError("Engine has not been initialized") | ||
|
||
session_factory = sessionmaker( | ||
self.engine, expire_on_commit=False, class_=AsyncSession | ||
) | ||
|
||
return session_factory() | ||
|
||
@property | ||
def session(self) -> AsyncSession: | ||
""" | ||
Get the asynchronous session for the database. | ||
Returns: | ||
AsyncSession: The asynchronous session for the database. | ||
Raises: | ||
RuntimeError: If the database session has not been initialized. | ||
""" | ||
|
||
if self._session is None: | ||
raise RuntimeError("Database session has not been initialized.") | ||
return self._session | ||
|
||
|
||
async def get_user_db(): | ||
"""Get the user database. | ||
Args: | ||
None | ||
Returns: | ||
SQLAlchemyUserDatabase: The user database. | ||
Raises: | ||
None | ||
""" | ||
from app.models import OAuthAccount, User # pylint: disable=import-outside-toplevel | ||
|
||
yield SQLAlchemyUserDatabase(db.session, User, OAuthAccount) | ||
|
||
|
||
db: Database = Database(settings.db_url) | ||
engine = create_async_engine(settings.db_url, future=True) | ||
SessionLocal = async_sessionmaker(expire_on_commit=False, bind=engine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.