🔧 fix(base.py): remove redundant imports and nested class declaration

✨ feat(base.py): add validation logic to set the DATABASE_URL value based on environment variables and file paths
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-15 16:37:21 -03:00
commit 69d03f64b1

View file

@ -62,33 +62,8 @@ class Settings(BaseSettings):
return str(value) return str(value)
from typing import Any
import os
from pathlib import Path
from shutil import copy2
from loguru import logger
from pydantic import validator
class BaseSettings:
"""
Base settings class for Langflow service.
"""
DEBUG: bool = False
TESTING: bool = False
CONFIG_DIR: str = ""
DATABASE_URL: str = ""
@validator("DATABASE_URL", pre=True) @validator("DATABASE_URL", pre=True)
def set_database_url(cls, value, values): def set_database_url(cls, value, values):
"""
Validator to set the DATABASE_URL value.
If no value is provided, it will try to get the LANGFLOW_DATABASE_URL environment variable.
If that is not set, it will use a sqlite database.
If a CONFIG_DIR is provided, it will use that directory to store the sqlite database.
If a sqlite database already exists in the current directory, it will be copied to the new location.
"""
if not value: if not value:
logger.debug( logger.debug(
"No database_url provided, trying LANGFLOW_DATABASE_URL env variable" "No database_url provided, trying LANGFLOW_DATABASE_URL env variable"
@ -109,20 +84,14 @@ class Settings(BaseSettings):
new_path = f"{values['CONFIG_DIR']}/langflow.db" new_path = f"{values['CONFIG_DIR']}/langflow.db"
if Path("./langflow.db").exists(): if Path("./langflow.db").exists():
if Path(new_path).exists(): if Path(new_path).exists():
logger.debug( logger.debug(f"Database already exists at {new_path}, using it")
f"Database already exists at {new_path}, using it"
)
else: else:
try: try:
logger.debug( logger.debug("Copying existing database to new location")
"Copying existing database to new location"
)
copy2("./langflow.db", new_path) copy2("./langflow.db", new_path)
logger.debug(f"Copied existing database to {new_path}") logger.debug(f"Copied existing database to {new_path}")
except Exception: except Exception:
logger.error( logger.error("Failed to copy database, using default path")
"Failed to copy database, using default path"
)
new_path = "./langflow.db" new_path = "./langflow.db"
value = f"sqlite:///{new_path}" value = f"sqlite:///{new_path}"