Spaces:
Sleeping
Sleeping
dbname
Browse files- app.py +10 -7
- core/database.py +6 -1
- services/drive_service.py +2 -1
app.py
CHANGED
|
@@ -11,7 +11,7 @@ from fastapi import FastAPI, Request
|
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
from fastapi.responses import JSONResponse
|
| 13 |
|
| 14 |
-
from core.database import init_db
|
| 15 |
from routers import auth, blink, contact, credits, general, gemini, payments
|
| 16 |
from services.drive_service import DriveService
|
| 17 |
|
|
@@ -35,14 +35,17 @@ async def lifespan(app: FastAPI):
|
|
| 35 |
|
| 36 |
# Check for RESET_DB environment variable
|
| 37 |
if os.getenv("RESET_DB", "").lower() == "true":
|
| 38 |
-
logger.warning("RESET_DB is set to true. Skipping download and clearing local database.")
|
| 39 |
-
if os.path.exists(
|
| 40 |
-
os.remove(
|
| 41 |
logger.info("Local database deleted.")
|
| 42 |
else:
|
| 43 |
-
# Startup: Download DB from Drive
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
await init_db()
|
| 48 |
logger.info("Database initialized successfully")
|
|
|
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
from fastapi.responses import JSONResponse
|
| 13 |
|
| 14 |
+
from core.database import init_db, DB_FILENAME
|
| 15 |
from routers import auth, blink, contact, credits, general, gemini, payments
|
| 16 |
from services.drive_service import DriveService
|
| 17 |
|
|
|
|
| 35 |
|
| 36 |
# Check for RESET_DB environment variable
|
| 37 |
if os.getenv("RESET_DB", "").lower() == "true":
|
| 38 |
+
logger.warning(f"RESET_DB is set to true. Skipping download and clearing local database ({DB_FILENAME}).")
|
| 39 |
+
if os.path.exists(DB_FILENAME):
|
| 40 |
+
os.remove(DB_FILENAME)
|
| 41 |
logger.info("Local database deleted.")
|
| 42 |
else:
|
| 43 |
+
# Startup: Download DB from Drive ONLY if local file doesn't exist
|
| 44 |
+
if not os.path.exists(DB_FILENAME):
|
| 45 |
+
logger.info("Startup: Local DB missing. Attempting to download from Google Drive...")
|
| 46 |
+
drive_service.download_db()
|
| 47 |
+
else:
|
| 48 |
+
logger.info("Startup: Local DB found. Skipping download to preserve data.")
|
| 49 |
|
| 50 |
await init_db()
|
| 51 |
logger.info("Database initialized successfully")
|
core/database.py
CHANGED
|
@@ -5,8 +5,13 @@ from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sess
|
|
| 5 |
from sqlalchemy.orm import DeclarativeBase
|
| 6 |
import os
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Database URL - SQLite file in the same directory
|
| 9 |
-
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./
|
| 10 |
|
| 11 |
# Create async engine
|
| 12 |
engine = create_async_engine(
|
|
|
|
| 5 |
from sqlalchemy.orm import DeclarativeBase
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# Database configuration
|
| 9 |
+
DB_NAME = os.getenv("DB_NAME", "blink_data")
|
| 10 |
+
ENV = os.getenv("ENV", "prod")
|
| 11 |
+
DB_FILENAME = f"{DB_NAME}_{ENV}.db"
|
| 12 |
+
|
| 13 |
# Database URL - SQLite file in the same directory
|
| 14 |
+
DATABASE_URL = os.getenv("DATABASE_URL", f"sqlite+aiosqlite:///./{DB_FILENAME}")
|
| 15 |
|
| 16 |
# Create async engine
|
| 17 |
engine = create_async_engine(
|
services/drive_service.py
CHANGED
|
@@ -18,8 +18,9 @@ class DriveService:
|
|
| 18 |
'https://www.googleapis.com/auth/gmail.send',
|
| 19 |
'https://www.googleapis.com/auth/drive.file'
|
| 20 |
]
|
|
|
|
| 21 |
FOLDER_NAME = "apigateway"
|
| 22 |
-
DB_FILENAME
|
| 23 |
|
| 24 |
def __init__(self):
|
| 25 |
self.creds = None
|
|
|
|
| 18 |
'https://www.googleapis.com/auth/gmail.send',
|
| 19 |
'https://www.googleapis.com/auth/drive.file'
|
| 20 |
]
|
| 21 |
+
from core.database import DB_FILENAME
|
| 22 |
FOLDER_NAME = "apigateway"
|
| 23 |
+
# DB_FILENAME is now imported from core.database
|
| 24 |
|
| 25 |
def __init__(self):
|
| 26 |
self.creds = None
|