Fix: Robust DB connection for zone sync - reads DATABASE_URL directly from .env
Some checks failed
CI / Frontend Lint & Type Check (push) Has been cancelled
CI / Frontend Build (push) Has been cancelled
CI / Backend Lint (push) Has been cancelled
CI / Backend Tests (push) Has been cancelled
CI / Docker Build (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
Deploy / Build & Push Images (push) Has been cancelled
Deploy / Deploy to Server (push) Has been cancelled
Deploy / Notify (push) Has been cancelled
Some checks failed
CI / Frontend Lint & Type Check (push) Has been cancelled
CI / Frontend Build (push) Has been cancelled
CI / Backend Lint (push) Has been cancelled
CI / Backend Tests (push) Has been cancelled
CI / Docker Build (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
Deploy / Build & Push Images (push) Has been cancelled
Deploy / Deploy to Server (push) Has been cancelled
Deploy / Notify (push) Has been cancelled
This commit is contained in:
@ -86,13 +86,31 @@ class ZoneSyncResult:
|
|||||||
|
|
||||||
|
|
||||||
async def get_db_session():
|
async def get_db_session():
|
||||||
"""Create async database session for zone sync script"""
|
"""Create async database session - ROBUST VERSION for standalone script"""
|
||||||
# Direct path to DB (script runs from backend/ directory)
|
# Read DATABASE_URL directly from .env (avoids import issues when running standalone)
|
||||||
db_path = Path("/home/user/pounce/backend/domainwatch.db")
|
env_file = Path("/home/user/pounce/backend/.env")
|
||||||
if not db_path.exists():
|
if not env_file.exists():
|
||||||
db_path = Path("domainwatch.db")
|
env_file = Path(__file__).parent.parent / ".env"
|
||||||
|
|
||||||
db_url = f"sqlite+aiosqlite:///{db_path}"
|
db_url = None
|
||||||
|
if env_file.exists():
|
||||||
|
for line in env_file.read_text().splitlines():
|
||||||
|
if line.startswith("DATABASE_URL="):
|
||||||
|
db_url = line.split("=", 1)[1].strip().strip('"').strip("'")
|
||||||
|
break
|
||||||
|
|
||||||
|
# Default to SQLite if not found
|
||||||
|
if not db_url:
|
||||||
|
db_url = "sqlite:///./domainwatch.db"
|
||||||
|
logger.warning(f"DATABASE_URL not found in .env, using default: {db_url}")
|
||||||
|
|
||||||
|
# Convert to async driver
|
||||||
|
if "sqlite://" in db_url and "aiosqlite" not in db_url:
|
||||||
|
db_url = db_url.replace("sqlite://", "sqlite+aiosqlite://")
|
||||||
|
elif "postgresql://" in db_url and "asyncpg" not in db_url:
|
||||||
|
db_url = db_url.replace("postgresql://", "postgresql+asyncpg://")
|
||||||
|
|
||||||
|
logger.info(f"Database URL: {db_url[:50]}...")
|
||||||
|
|
||||||
engine = create_async_engine(db_url, echo=False)
|
engine = create_async_engine(db_url, echo=False)
|
||||||
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user