This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
forked from MDGrey33/Nur
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #17 from toptal/devx-3855-refactor-DBs
Refactor Database folder
- Loading branch information
Showing
31 changed files
with
522 additions
and
627 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
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,41 +1,32 @@ | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
from sqlalchemy.exc import SQLAlchemyError | ||
from configuration import sql_file_path | ||
from database.bookmarked_conversation import BookmarkedConversation, Base | ||
from models.bookmarked_conversation import BookmarkedConversation | ||
from datetime import datetime, timezone | ||
from database.database import Database | ||
|
||
|
||
class BookmarkedConversationManager: | ||
def __init__(self): | ||
self.engine = create_engine('sqlite:///' + sql_file_path) | ||
Base.metadata.create_all(self.engine) | ||
self.Session = sessionmaker(bind=self.engine) | ||
self.db = Database() | ||
|
||
def add_bookmarked_conversation(self, title, body, thread_id): | ||
try: | ||
with self.Session() as session: | ||
new_conversation = BookmarkedConversation(title=title, body=body, thread_id=thread_id) | ||
session.add(new_conversation) | ||
session.commit() | ||
return new_conversation.id | ||
except SQLAlchemyError as e: | ||
print(f"Error adding bookmarked conversation: {e}") | ||
return None | ||
new_conversation = BookmarkedConversation(title=title, body=body, thread_id=thread_id) | ||
self.db.add_object(new_conversation) | ||
|
||
def update_posted_on_confluence(self, thread_id): | ||
try: | ||
with self.Session() as session: | ||
with self.db.get_session() as session: | ||
conversation = session.query(BookmarkedConversation).filter_by(thread_id=thread_id).first() | ||
if conversation: | ||
conversation.posted_on_confluence = datetime.now(timezone.utc) | ||
session.commit() | ||
except SQLAlchemyError as e: | ||
print(f"Error updating conversation with Confluence timestamp: {e}") | ||
return None | ||
|
||
def get_unposted_conversations(self): | ||
try: | ||
with self.Session() as session: | ||
with self.db.get_session() as session: | ||
return session.query(BookmarkedConversation).filter_by(posted_on_confluence=None).all() | ||
except SQLAlchemyError as e: | ||
print(f"Error getting unposted conversations: {e}") | ||
return None | ||
return None |
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
from sqlalchemy.exc import SQLAlchemyError | ||
from configuration import db_url | ||
from models.qa_interaction import QAInteraction | ||
from models.space_info import SpaceInfo | ||
from models.page_data import PageData | ||
from models.bookmarked_conversation import BookmarkedConversation | ||
from models.quiz_question import QuizQuestion | ||
from models.user_score import UserScore | ||
|
||
|
||
class Database: | ||
""" | ||
Class providing access to a SQLAlchemy database. | ||
This class implements the Singleton pattern for creating and managing a connection to a SQLAlchemy database. | ||
It provides methods for getting database sessions and accessing the SQLAlchemy Engine object. | ||
Attributes: | ||
engine (sqlalchemy.engine.Engine): The SQLAlchemy Engine object representing the connection to the database. | ||
Session (sqlalchemy.orm.Session): The SQLAlchemy session factory used for creating database sessions. | ||
""" | ||
|
||
_instance = None | ||
|
||
def __new__(cls): | ||
""" | ||
Create a new instance of the Database class. | ||
If an instance of the class has not been created yet, it is created; otherwise, the existing instance is returned. | ||
Returns: | ||
Database: An instance of the Database class. | ||
""" | ||
if cls._instance is None: | ||
cls._instance = super().__new__(cls) | ||
cls._instance._init_engine() | ||
cls._instance._create_tables() | ||
return cls._instance | ||
|
||
def _create_tables(self): | ||
""" | ||
Create tables in the database if they do not exist. | ||
""" | ||
for model in [QAInteraction, SpaceInfo, PageData, BookmarkedConversation, QuizQuestion, UserScore]: | ||
model.metadata.create_all(self.engine) | ||
|
||
def _init_engine(self): | ||
""" | ||
Initialize the SQLAlchemy Engine object and session factory. | ||
Creates the Engine object for connecting to the database and the session factory for creating database sessions. | ||
""" | ||
self.engine = create_engine(db_url) | ||
self.Session = sessionmaker(bind=self.engine) | ||
|
||
def get_session(self): | ||
""" | ||
Get a new database session. | ||
Returns: | ||
sqlalchemy.orm.Session: A new database session. | ||
""" | ||
return self.Session() | ||
|
||
def add_object(self, obj): | ||
""" | ||
Adds the given object to the database. | ||
Args: | ||
obj: The object to add to the database. | ||
Returns: | ||
int or None: The ID of the added object if successful, None otherwise. | ||
Raises: | ||
None | ||
""" | ||
try: | ||
with self.get_session() as session: | ||
session.add(obj) | ||
session.commit() | ||
return obj | ||
except SQLAlchemyError as e: | ||
class_name = obj.__class__.__name__ | ||
print(f"Error adding object of type {class_name}: {e}") | ||
session.rollback() | ||
return None |
Oops, something went wrong.