Spaces:
Sleeping
Sleeping
backend with database
Browse files- backend/__pycache__/config.cpython-311.pyc +0 -0
- backend/__pycache__/content_generator.cpython-311.pyc +0 -0
- backend/__pycache__/db.cpython-311.pyc +0 -0
- backend/__pycache__/db_cache.cpython-311.pyc +0 -0
- backend/__pycache__/db_init.cpython-311.pyc +0 -0
- backend/__pycache__/main.cpython-311.pyc +0 -0
- backend/db.py +1 -1
- backend/db_cache.py +1 -1
- backend/db_init.py +27 -1
- backend/utils/__pycache__/generate_completions.cpython-311.pyc +0 -0
backend/__pycache__/config.cpython-311.pyc
CHANGED
|
Binary files a/backend/__pycache__/config.cpython-311.pyc and b/backend/__pycache__/config.cpython-311.pyc differ
|
|
|
backend/__pycache__/content_generator.cpython-311.pyc
CHANGED
|
Binary files a/backend/__pycache__/content_generator.cpython-311.pyc and b/backend/__pycache__/content_generator.cpython-311.pyc differ
|
|
|
backend/__pycache__/db.cpython-311.pyc
CHANGED
|
Binary files a/backend/__pycache__/db.cpython-311.pyc and b/backend/__pycache__/db.cpython-311.pyc differ
|
|
|
backend/__pycache__/db_cache.cpython-311.pyc
CHANGED
|
Binary files a/backend/__pycache__/db_cache.cpython-311.pyc and b/backend/__pycache__/db_cache.cpython-311.pyc differ
|
|
|
backend/__pycache__/db_init.cpython-311.pyc
CHANGED
|
Binary files a/backend/__pycache__/db_init.cpython-311.pyc and b/backend/__pycache__/db_init.cpython-311.pyc differ
|
|
|
backend/__pycache__/main.cpython-311.pyc
CHANGED
|
Binary files a/backend/__pycache__/main.cpython-311.pyc and b/backend/__pycache__/main.cpython-311.pyc differ
|
|
|
backend/db.py
CHANGED
|
@@ -9,7 +9,7 @@ import logging
|
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
# Database file path
|
| 12 |
-
DB_PATH = os.getenv("DATABASE_PATH", "
|
| 13 |
|
| 14 |
|
| 15 |
class Database:
|
|
|
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
# Database file path
|
| 12 |
+
DB_PATH = os.getenv("DATABASE_PATH", "/app/ai_tutor.db")
|
| 13 |
|
| 14 |
|
| 15 |
class Database:
|
backend/db_cache.py
CHANGED
|
@@ -6,7 +6,7 @@ import logging
|
|
| 6 |
import hashlib
|
| 7 |
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
-
DB_PATH = os.getenv("DATABASE_PATH", "
|
| 10 |
|
| 11 |
class ApiCache:
|
| 12 |
"""Generic caching service using a dedicated database table."""
|
|
|
|
| 6 |
import hashlib
|
| 7 |
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
+
DB_PATH = os.getenv("DATABASE_PATH", "/app/ai_tutor.db")
|
| 10 |
|
| 11 |
class ApiCache:
|
| 12 |
"""Generic caching service using a dedicated database table."""
|
backend/db_init.py
CHANGED
|
@@ -7,6 +7,8 @@ import os
|
|
| 7 |
import aiosqlite
|
| 8 |
import logging
|
| 9 |
from pathlib import Path
|
|
|
|
|
|
|
| 10 |
from typing import Dict, Any, List
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
|
@@ -15,8 +17,32 @@ class DatabaseInitializer:
|
|
| 15 |
"""Handles database initialization and health checks"""
|
| 16 |
|
| 17 |
def __init__(self, db_path: str = None):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
self.schema_path = self._find_schema_file()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def _find_schema_file(self) -> str:
|
| 22 |
"""Return the path to the schema.sql file.
|
|
|
|
| 7 |
import aiosqlite
|
| 8 |
import logging
|
| 9 |
from pathlib import Path
|
| 10 |
+
import tempfile
|
| 11 |
+
import atexit
|
| 12 |
from typing import Dict, Any, List
|
| 13 |
|
| 14 |
logger = logging.getLogger(__name__)
|
|
|
|
| 17 |
"""Handles database initialization and health checks"""
|
| 18 |
|
| 19 |
def __init__(self, db_path: str = None):
|
| 20 |
+
# Check if we should use a temporary database
|
| 21 |
+
use_temp_db = os.getenv("USE_TEMP_DATABASE", "false").lower() == "true"
|
| 22 |
+
|
| 23 |
+
if db_path:
|
| 24 |
+
self.db_path = db_path
|
| 25 |
+
elif use_temp_db:
|
| 26 |
+
# Create a temporary database file that gets cleaned up on exit
|
| 27 |
+
temp_fd, temp_path = tempfile.mkstemp(suffix='.db', prefix='ai_tutor_')
|
| 28 |
+
os.close(temp_fd) # Close the file descriptor, we'll reopen it with sqlite
|
| 29 |
+
self.db_path = temp_path
|
| 30 |
+
# Register cleanup on exit
|
| 31 |
+
atexit.register(self._cleanup_temp_db)
|
| 32 |
+
logger.info(f"Using temporary database: {self.db_path}")
|
| 33 |
+
else:
|
| 34 |
+
self.db_path = os.getenv("DATABASE_PATH", "/app/ai_tutor.db")
|
| 35 |
+
|
| 36 |
self.schema_path = self._find_schema_file()
|
| 37 |
+
|
| 38 |
+
def _cleanup_temp_db(self):
|
| 39 |
+
"""Clean up temporary database file"""
|
| 40 |
+
try:
|
| 41 |
+
if os.path.exists(self.db_path):
|
| 42 |
+
os.unlink(self.db_path)
|
| 43 |
+
logger.info(f"Cleaned up temporary database: {self.db_path}")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
logger.error(f"Error cleaning up temporary database: {e}")
|
| 46 |
|
| 47 |
def _find_schema_file(self) -> str:
|
| 48 |
"""Return the path to the schema.sql file.
|
backend/utils/__pycache__/generate_completions.cpython-311.pyc
CHANGED
|
Binary files a/backend/utils/__pycache__/generate_completions.cpython-311.pyc and b/backend/utils/__pycache__/generate_completions.cpython-311.pyc differ
|
|
|