-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix isolate db sessions #150
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f3ab2a6
no longer needed
EduardSchwarzkopf 1f26a19
simplify database connections
EduardSchwarzkopf 27495cc
refactore database setup
EduardSchwarzkopf de3253f
implement session logic
EduardSchwarzkopf f0b8a84
update for sessions
EduardSchwarzkopf 2bf9ed7
update for concurrent session
EduardSchwarzkopf 783a51f
update dependency
EduardSchwarzkopf a5694ce
update for correct schema
EduardSchwarzkopf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider implementing a helper method for session management.
While the changes introduce more explicit session management, they also add unnecessary repetition. Consider introducing a helper method to encapsulate the session creation and execution pattern. This will maintain explicit control over sessions while reducing code duplication. Here's an example:
This approach:
Apply this pattern across all methods in the Repository class to significantly reduce complexity while preserving the benefits of explicit session management.