Spaces:
Running
Running
Tobias Geisler
commited on
Commit
·
e7fd91f
1
Parent(s):
10aad52
db threadsafe session handling
Browse files- database.py +9 -26
database.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
# database.py
|
| 2 |
import os
|
| 3 |
from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean, DateTime, func
|
| 4 |
from sqlalchemy.ext.declarative import declarative_base
|
| 5 |
-
from sqlalchemy.orm import sessionmaker
|
| 6 |
from contextlib import contextmanager
|
|
|
|
| 7 |
from utils.utils import get_secret
|
| 8 |
import random
|
| 9 |
from better_profanity import profanity
|
|
@@ -31,7 +31,9 @@ except Exception as e:
|
|
| 31 |
logger.error(f"Error connecting to the database: {str(e)}")
|
| 32 |
raise e
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
Base = declarative_base()
|
| 37 |
|
|
@@ -92,7 +94,7 @@ def get_db_session():
|
|
| 92 |
db.rollback()
|
| 93 |
raise e
|
| 94 |
finally:
|
| 95 |
-
|
| 96 |
|
| 97 |
def create_chatbot(name, custom_instruction):
|
| 98 |
with get_db_session() as db:
|
|
@@ -107,11 +109,8 @@ def get_chatbot(chatbot_id):
|
|
| 107 |
with get_db_session() as db:
|
| 108 |
return db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id, Chatbot.is_active == True).first()
|
| 109 |
|
| 110 |
-
|
| 111 |
-
### These functions need to be rewritten to use the get_db_session context manager
|
| 112 |
def update_chatbot(chatbot_id, name=None, custom_instruction=None, is_active=None):
|
| 113 |
-
|
| 114 |
-
try:
|
| 115 |
chatbot = db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id).first()
|
| 116 |
if chatbot:
|
| 117 |
if name:
|
|
@@ -123,32 +122,16 @@ def update_chatbot(chatbot_id, name=None, custom_instruction=None, is_active=Non
|
|
| 123 |
db.commit()
|
| 124 |
db.refresh(chatbot)
|
| 125 |
return chatbot
|
| 126 |
-
except SQLAlchemyError as e:
|
| 127 |
-
db.rollback()
|
| 128 |
-
raise e
|
| 129 |
-
finally:
|
| 130 |
-
db.close()
|
| 131 |
|
| 132 |
def delete_chatbot(chatbot_id):
|
| 133 |
-
|
| 134 |
-
try:
|
| 135 |
chatbot = db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id).first()
|
| 136 |
if chatbot:
|
| 137 |
db.delete(chatbot)
|
| 138 |
db.commit()
|
| 139 |
return True
|
| 140 |
return False
|
| 141 |
-
except SQLAlchemyError as e:
|
| 142 |
-
db.rollback()
|
| 143 |
-
raise e
|
| 144 |
-
finally:
|
| 145 |
-
db.close()
|
| 146 |
|
| 147 |
def get_all_chatbots():
|
| 148 |
-
|
| 149 |
-
try:
|
| 150 |
return db.query(Chatbot).all()
|
| 151 |
-
except SQLAlchemyError as e:
|
| 152 |
-
raise e
|
| 153 |
-
finally:
|
| 154 |
-
db.close()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean, DateTime, func
|
| 3 |
from sqlalchemy.ext.declarative import declarative_base
|
| 4 |
+
from sqlalchemy.orm import sessionmaker, scoped_session
|
| 5 |
from contextlib import contextmanager
|
| 6 |
+
from sqlalchemy.exc import SQLAlchemyError
|
| 7 |
from utils.utils import get_secret
|
| 8 |
import random
|
| 9 |
from better_profanity import profanity
|
|
|
|
| 31 |
logger.error(f"Error connecting to the database: {str(e)}")
|
| 32 |
raise e
|
| 33 |
|
| 34 |
+
# Use scoped session to handle thread-local sessions
|
| 35 |
+
session_factory = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 36 |
+
SessionLocal = scoped_session(session_factory)
|
| 37 |
|
| 38 |
Base = declarative_base()
|
| 39 |
|
|
|
|
| 94 |
db.rollback()
|
| 95 |
raise e
|
| 96 |
finally:
|
| 97 |
+
SessionLocal.remove()
|
| 98 |
|
| 99 |
def create_chatbot(name, custom_instruction):
|
| 100 |
with get_db_session() as db:
|
|
|
|
| 109 |
with get_db_session() as db:
|
| 110 |
return db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id, Chatbot.is_active == True).first()
|
| 111 |
|
|
|
|
|
|
|
| 112 |
def update_chatbot(chatbot_id, name=None, custom_instruction=None, is_active=None):
|
| 113 |
+
with get_db_session() as db:
|
|
|
|
| 114 |
chatbot = db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id).first()
|
| 115 |
if chatbot:
|
| 116 |
if name:
|
|
|
|
| 122 |
db.commit()
|
| 123 |
db.refresh(chatbot)
|
| 124 |
return chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
def delete_chatbot(chatbot_id):
|
| 127 |
+
with get_db_session() as db:
|
|
|
|
| 128 |
chatbot = db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id).first()
|
| 129 |
if chatbot:
|
| 130 |
db.delete(chatbot)
|
| 131 |
db.commit()
|
| 132 |
return True
|
| 133 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
def get_all_chatbots():
|
| 136 |
+
with get_db_session() as db:
|
|
|
|
| 137 |
return db.query(Chatbot).all()
|
|
|
|
|
|
|
|
|
|
|
|