CoolFace
Apppublic

malepati/custom_template_working

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
database_details.md52 linesDownload Raw Back to root
1# Database Analysis & Configuration2 3## 1. Database Architecture & Data persistence4 5### **Current Setup**6- **Engine**: The system is built using **SQLAlchemy** (async) and **SQLModel**.7- **Default Database**: It defaults to a **SQLite** database file named `fastapi.db`.8- **Location**:9  - The database file is stored in the directory defined by the `APP_DATA_DIRECTORY` environment variable.10  - If that variable is not set, it defaults to `<Current Working Directory>/app_data`.11  - In some fallback cases (seen in `db_utils.py`), it might attempt to use `/tmp/presenton` if the app data getter fails or returns empty.12 13### **Why "Every New Session is Deleted"?**14The system does **NOT** explicitly delete data on startup. The `create_db_and_tables` function only creates tables if they don't exist.15 16**Reasons for data loss:**171.  **Ephemeral File System**: If you are running this in a constrained environment (like a docker container without volume mounting, or a temporary cloud shell), the `app_data` folder is destroyed when the session restarts.182.  **In-Memory Fallback**: If the path is invalid or unwritable, SQLite might failover or be re-initialized in a temp location that is cleared on reboot.193.  **Local Execution**: If you run the server from different directories, `os.getcwd()` changes, creating a *new* `app_data/fastapi.db` in that new directory, making it appear as if old data is lost.20 21### **How it saves data**22- Data is saved automatically to the `fastapi.db` file (or connected external DB) whenever an API request creates a presentation.23- Tables include: `presentations`, `slides`, `templates`, etc.24- **There is NO user isolation**. The current `PresentationModel` (and others) does not have a `user_id` column. This means it is designed as a **single-user** application. If you have multiple users connecting to one server, they will see/overwrite the same data unless you separate them at the infrastructure level.25 26## 2. How to Set a New Database27 28You can switch to a persistent database (like PostgreSQL, MySQL) or a persistent SQLite file location without changing the code.29 30### **Configuration Methods**31You do **not** need to create tables manually. The system automatically runs `create_db_and_tables()` on startup, which will generate the schema for you.32 33#### **Option A: Environment Variable (Recommended)**34Set the `DATABASE_URL` environment variable before running the server.35 36**Examples:**37- **PostgreSQL**: `export DATABASE_URL="postgresql://user:password@localhost/dbname"`38- **MySQL**: `export DATABASE_URL="mysql://user:password@localhost/dbname"`39- **Custom SQLite**: `export DATABASE_URL="sqlite:////absolute/path/to/persistent/folder/fastapi.db"`40 41#### **Option B: User Config File**42Modify `user_config.json` (managed by `UserConfigEnvUpdateMiddleware`). Even though `UserConfig` class in `models/user_config.py` primarily lists LLM keys, the `db_utils.py` reads from the environment. `UserConfig` currently does not seem to expose a direct `DATABASE_URL` field to be updated via the API's config endpoint, so **Option A** is the most reliable method unless you modify `UserConfig` model.43 44## 3. Recommendations for Multi-User Support45 46Since the code currently lacks `user_id` fields in its models (`servers/fastapi/models/sql/presentation.py`), it treats everyone as the same user.47 48**To support multiple users reliably:**491.  **Code Change Required**: You must add `user_id: str` (indexed) to `PresentationModel`, `SlideModel`, etc.502.  **API Update**: Update all API endpoints to require user identification (via headers or auth token) and filter queries by this `user_id`.513.  **Infrastructure**: Switch to a robust database like PostgreSQL (`DATABASE_URL=postgresql://...`) to handle concurrent writes better than SQLite.52