🐛 fix(manager.py): handle table creation errors and log warnings for existing tables
✨ feat(manager.py): improve table creation process by checking if tables already exist before creating them
This commit is contained in:
parent
3724876620
commit
78c54d6736
1 changed files with 17 additions and 9 deletions
|
|
@ -6,6 +6,7 @@ from langflow.services.database.utils import Result, TableResults
|
||||||
from langflow.services.getters import get_settings_service
|
from langflow.services.getters import get_settings_service
|
||||||
from sqlalchemy import inspect
|
from sqlalchemy import inspect
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.exc import OperationalError
|
||||||
from sqlmodel import SQLModel, Session, create_engine
|
from sqlmodel import SQLModel, Session, create_engine
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from alembic.config import Config
|
from alembic.config import Config
|
||||||
|
|
@ -157,20 +158,27 @@ class DatabaseService(Service):
|
||||||
from sqlalchemy import inspect
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
inspector = inspect(self.engine)
|
inspector = inspect(self.engine)
|
||||||
current_tables = ["flow", "user", "apikey"]
|
|
||||||
table_names = inspector.get_table_names()
|
table_names = inspector.get_table_names()
|
||||||
|
current_tables = ["flow", "user", "apikey"]
|
||||||
|
|
||||||
if table_names and all(table in table_names for table in current_tables):
|
if table_names and all(table in table_names for table in current_tables):
|
||||||
logger.debug("Database and tables already exist")
|
logger.debug("Database and tables already exist")
|
||||||
return
|
return
|
||||||
logger.debug("Creating database and tables")
|
|
||||||
try:
|
|
||||||
SQLModel.metadata.create_all(self.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
|
logger.debug("Creating database and tables")
|
||||||
# and we need to create the tables again.
|
|
||||||
|
for table in SQLModel.metadata.sorted_tables:
|
||||||
|
try:
|
||||||
|
table.create(self.engine, checkfirst=True)
|
||||||
|
except OperationalError as oe:
|
||||||
|
logger.warning(
|
||||||
|
f"Table {table} already exists, skipping. Exception: {oe}"
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error(f"Error creating table {table}: {exc}")
|
||||||
|
raise RuntimeError(f"Error creating table {table}") from exc
|
||||||
|
|
||||||
|
# Now check if the required tables exist, if not, something went wrong.
|
||||||
inspector = inspect(self.engine)
|
inspector = inspect(self.engine)
|
||||||
table_names = inspector.get_table_names()
|
table_names = inspector.get_table_names()
|
||||||
for table in current_tables:
|
for table in current_tables:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue