From 20da596d9a28dc3049d45043a596f0b7e473e5f7 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 5 Jul 2023 11:47:55 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(base.py):=20handle=20case=20?= =?UTF-8?q?when=20no=20database=5Furl=20is=20provided=20and=20raise=20an?= =?UTF-8?q?=20error=20=E2=9C=A8=20feat(base.py):=20add=20error=20handling?= =?UTF-8?q?=20when=20creating=20database=20and=20tables=20to=20provide=20m?= =?UTF-8?q?ore=20informative=20error=20messages=20The=20code=20now=20check?= =?UTF-8?q?s=20if=20the=20`settings.database=5Furl`=20is=20provided=20and?= =?UTF-8?q?=20raises=20a=20`RuntimeError`=20if=20it=20is=20not.=20This=20e?= =?UTF-8?q?nsures=20that=20the=20application=20does=20not=20attempt=20to?= =?UTF-8?q?=20create=20a=20database=20connection=20without=20a=20valid=20U?= =?UTF-8?q?RL.=20Additionally,=20error=20handling=20has=20been=20added=20w?= =?UTF-8?q?hen=20creating=20the=20database=20and=20tables.=20If=20an=20exc?= =?UTF-8?q?eption=20occurs=20during=20the=20creation=20process,=20an=20err?= =?UTF-8?q?or=20message=20is=20logged=20and=20a=20`RuntimeError`=20is=20ra?= =?UTF-8?q?ised=20with=20a=20more=20informative=20error=20message.=20This?= =?UTF-8?q?=20helps=20in=20identifying=20and=20resolving=20any=20issues=20?= =?UTF-8?q?related=20to=20the=20database=20creation=20process.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/database/base.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/database/base.py b/src/backend/langflow/database/base.py index add28192a..256434523 100644 --- a/src/backend/langflow/database/base.py +++ b/src/backend/langflow/database/base.py @@ -2,17 +2,22 @@ from langflow.settings import settings from sqlmodel import SQLModel, Session, create_engine 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} else: connect_args = {} - +if not settings.database_url: + raise RuntimeError("No database_url provided") engine = create_engine(settings.database_url, connect_args=connect_args) def create_db_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 # and we need to create the tables again. from sqlalchemy import inspect @@ -21,6 +26,7 @@ def create_db_and_tables(): if "flow" not in inspector.get_table_names(): logger.error("Something went wrong creating the database and tables.") logger.error("Please check your database settings.") + raise RuntimeError("Something went wrong creating the database and tables.") else: logger.debug("Database and tables created successfully")