Coverage for src / local_deep_research / database / auth_db.py: 94%
27 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-01-11 00:51 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2026-01-11 00:51 +0000
1"""
2Authentication database initialization and management.
3This manages the central ldr_auth.db which only stores usernames.
4"""
6from pathlib import Path
8from loguru import logger
9from sqlalchemy import create_engine
10from sqlalchemy.orm import sessionmaker
12from ..config.paths import get_data_directory
13from .models.auth import User
14from .models.base import Base
17def get_auth_db_path() -> Path:
18 """Get the path to the authentication database."""
19 return get_data_directory() / "ldr_auth.db"
22def init_auth_database():
23 """Initialize the authentication database if it doesn't exist."""
24 auth_db_path = get_auth_db_path()
26 # Ensure the data directory exists
27 auth_db_path.parent.mkdir(parents=True, exist_ok=True)
29 # Check if database already exists
30 if auth_db_path.exists():
31 logger.debug(f"Auth database already exists at {auth_db_path}")
32 return
34 logger.info(f"Creating auth database at {auth_db_path}")
36 # Create the database
37 engine = create_engine(f"sqlite:///{auth_db_path}")
39 # Create tables
40 Base.metadata.create_all(engine, tables=[User.__table__])
42 logger.info("Auth database initialized successfully")
45def get_auth_db_session():
46 """Get a session for the auth database."""
47 auth_db_path = get_auth_db_path()
49 # Ensure database exists
50 if not auth_db_path.exists(): 50 ↛ 51line 50 didn't jump to line 51 because the condition on line 50 was never true
51 init_auth_database()
53 engine = create_engine(f"sqlite:///{auth_db_path}")
54 Session = sessionmaker(bind=engine)
55 return Session()
58# Initialize on import
59init_auth_database()