🔧 fix(manager.py): import missing dependencies to resolve NameError and improve code readability

 feat(manager.py): add method run_migrations_test() to check if all models are in the database and up to date with all columns
 feat(manager.py): add method check_table() to check if a table exists and if all expected columns are present
🔧 fix(utils.py): import missing dependencies to resolve NameError and improve code readability
 feat(utils.py): add dataclasses Result and TableResults to store check results for tables and columns
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-28 16:21:50 -03:00
commit 5f2a949ec5
2 changed files with 49 additions and 0 deletions

View file

@ -1,6 +1,7 @@
from pathlib import Path
from typing import TYPE_CHECKING
from langflow.services.base import Service
from langflow.services.database.utils import Result, TableResults
from langflow.services.utils import get_settings_manager
from sqlalchemy import inspect
import sqlalchemy as sa
@ -100,6 +101,40 @@ class DatabaseManager(Service):
alembic_cfg.set_main_option("sqlalchemy.url", self.database_url)
command.upgrade(alembic_cfg, "head")
def run_migrations_test(self):
# This method is used for testing purposes only
# We will check that all models are in the database
# and that the database is up to date with all columns
sql_models = [models.Flow, models.User, models.ApiKey]
results = []
for sql_model in sql_models:
results.append(
TableResults(sql_model.__tablename__, self.check_table(sql_model))
)
return results
def check_table(self, model):
results = []
inspector = inspect(self.engine)
table_name = model.__tablename__
expected_columns = list(model.__fields__.keys())
try:
available_columns = [
col["name"] for col in inspector.get_columns(table_name)
]
results.append(Result(name=table_name, type="table", success=True))
except sa.exc.NoSuchTableError:
logger.error(f"Missing table: {table_name}")
results.append(Result(name=table_name, type="table", success=False))
for column in expected_columns:
if column not in available_columns:
logger.error(f"Missing column: {column} in table {table_name}")
results.append(Result(name=column, type="column", success=False))
else:
results.append(Result(name=column, type="column", success=True))
return results
def create_db_and_tables(self):
logger.debug("Creating database and tables")
try:

View file

@ -1,3 +1,4 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING
from langflow.utils.logger import logger
from contextlib import contextmanager
@ -53,3 +54,16 @@ def session_getter(db_manager: "DatabaseManager"):
raise
finally:
session.close()
@dataclass
class Result:
name: str
type: str
success: bool
@dataclass
class TableResults:
table_name: str
results: list[Result]