langflow-ai / langflow

Langflow is a low-code app builder for RAG and multi-agent AI applications. It’s Python-based and agnostic to any model, API, or database.
http://www.langflow.org
MIT License
26.91k stars 3.57k forks source link

Langflow saves SQLite Database in venv #2694

Closed smoky19x closed 1 month ago

smoky19x commented 1 month ago

Describe the bug Langflow saves the SQLite database (langflow.db, langflow.db-shm and langflow.db-wal) of its flows directly in the venv, e.g. /app/.venv/lib/python3.12/site-packages/langflow/langflow.db*. If the venv is deleted or renewed, the data will also be deleted. It certainly makes sense to save the database outside the venv, possibly in the ".cache" directory?

dosubot[bot] commented 1 month ago

Hello @smoky19x! I'm here to help you with any bugs or questions you have in the Langflow repository. Let me know how I can assist you!

To modify Langflow to save the SQLite database files outside the virtual environment, you can set the LANGFLOW_CONFIG_DIR environment variable to a directory outside the virtual environment, such as the ".cache" directory. Additionally, set LANGFLOW_SAVE_DB_IN_CONFIG_DIR to true to ensure the database is saved in the specified config directory.

Here is how you can configure your .env file:

# Config directory
# Directory where files, logs and database will be stored
# Example: LANGFLOW_CONFIG_DIR=~/.langflow
LANGFLOW_CONFIG_DIR=~/.cache/langflow

# Save database in the config directory
# Values: true, false
# If false, the database will be saved in Langflow's root directory
# This means that the database will be deleted when Langflow is uninstalled
# and that the database will not be shared between different virtual environments
# Example: LANGFLOW_SAVE_DB_IN_CONFIG_DIR=true
LANGFLOW_SAVE_DB_IN_CONFIG_DIR=true

# Database URL
# Postgres example: LANGFLOW_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/langflow
# SQLite example:
LANGFLOW_DATABASE_URL=sqlite:///~/.cache/langflow/langflow.db

This configuration will ensure that the SQLite database is stored in the ~/.cache/langflow directory, preventing data loss when the virtual environment is deleted or renewed [1][2][3].

To continue talking to Dosu, mention @dosu.

smoky19x commented 1 month ago

@dosu: Thanks for the answer. In the .env file, however, it is sufficient to set one setting, either LANGFLOW_SAVE_DB_IN_CONFIG_DIR=true or LANGFLOW_DATABASE_URL=sqlite:///.... It is not necessary to set both settings.

Just for the sake of completeness: An sqlite URL to a file must use an absolute path. The tilde for the home directory cannot be used.

dosubot[bot] commented 1 month ago

Thanks for the clarification! You're right, setting either LANGFLOW_SAVE_DB_IN_CONFIG_DIR=true or LANGFLOW_DATABASE_URL=sqlite:///... is sufficient. Also, good point about the absolute path requirement for SQLite URLs. Here's the corrected configuration:

# Config directory
LANGFLOW_CONFIG_DIR=/absolute/path/to/.cache/langflow

# Save database in the config directory
LANGFLOW_SAVE_DB_IN_CONFIG_DIR=true

# Alternatively, you can directly set the database URL
# LANGFLOW_DATABASE_URL=sqlite:////absolute/path/to/.cache/langflow/langflow.db

Make sure to replace /absolute/path/to/.cache/langflow with the actual absolute path to your desired directory. This should ensure your database is stored outside the virtual environment.

To continue talking to Dosu, mention @dosu.