🐛 fix(base.py): handle case when no database_url is provided and raise an error

✨ feat(base.py): add error handling when creating database and tables to provide more informative error messages
The code now checks if the `settings.database_url` is provided and raises a `RuntimeError` if it is not. This ensures that the application does not attempt to create a database connection without a valid URL. Additionally, error handling has been added when creating the database and tables. If an exception occurs during the creation process, an error message is logged and a `RuntimeError` is raised with a more informative error message. This helps in identifying and resolving any issues related to the database creation process.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-05 11:47:55 -03:00
commit 20da596d9a

View file

@ -2,17 +2,22 @@ from langflow.settings import settings
from sqlmodel import SQLModel, Session, create_engine from sqlmodel import SQLModel, Session, create_engine
from langflow.utils.logger import logger from langflow.utils.logger import logger
if settings.database_url.startswith("sqlite"): if settings.database_url and settings.database_url.startswith("sqlite"):
connect_args = {"check_same_thread": False} connect_args = {"check_same_thread": False}
else: else:
connect_args = {} connect_args = {}
if not settings.database_url:
raise RuntimeError("No database_url provided")
engine = create_engine(settings.database_url, connect_args=connect_args) engine = create_engine(settings.database_url, connect_args=connect_args)
def create_db_and_tables(): def create_db_and_tables():
logger.debug("Creating database and tables") logger.debug("Creating database and tables")
SQLModel.metadata.create_all(engine) try:
SQLModel.metadata.create_all(engine)
except Exception as exc:
logger.error(f"Error creating database and tables: {exc}")
raise RuntimeError("Error creating database and tables") from exc
# Now check if the table Flow exists, if not, something went wrong # Now check if the table Flow exists, if not, something went wrong
# and we need to create the tables again. # and we need to create the tables again.
from sqlalchemy import inspect from sqlalchemy import inspect
@ -21,6 +26,7 @@ def create_db_and_tables():
if "flow" not in inspector.get_table_names(): if "flow" not in inspector.get_table_names():
logger.error("Something went wrong creating the database and tables.") logger.error("Something went wrong creating the database and tables.")
logger.error("Please check your database settings.") logger.error("Please check your database settings.")
raise RuntimeError("Something went wrong creating the database and tables.") raise RuntimeError("Something went wrong creating the database and tables.")
else: else:
logger.debug("Database and tables created successfully") logger.debug("Database and tables created successfully")