🔧 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:
parent
6e3a6ce8c3
commit
69d03f64b1
1 changed files with 32 additions and 63 deletions
|
|
@ -62,33 +62,8 @@ class Settings(BaseSettings):
|
|||
|
||||
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)
|
||||
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:
|
||||
logger.debug(
|
||||
"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"
|
||||
if Path("./langflow.db").exists():
|
||||
if Path(new_path).exists():
|
||||
logger.debug(
|
||||
f"Database already exists at {new_path}, using it"
|
||||
)
|
||||
logger.debug(f"Database already exists at {new_path}, using it")
|
||||
else:
|
||||
try:
|
||||
logger.debug(
|
||||
"Copying existing database to new location"
|
||||
)
|
||||
logger.debug("Copying existing database to new location")
|
||||
copy2("./langflow.db", new_path)
|
||||
logger.debug(f"Copied existing database to {new_path}")
|
||||
except Exception:
|
||||
logger.error(
|
||||
"Failed to copy database, using default path"
|
||||
)
|
||||
logger.error("Failed to copy database, using default path")
|
||||
new_path = "./langflow.db"
|
||||
|
||||
value = f"sqlite:///{new_path}"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue