2025-06-02 19:17:05 +00:00

28 lines
959 B
Python

import logging
from sqlalchemy.orm import Session
from app import crud, schemas
from app.core.config import settings
from app.db.session import Base, engine
logger = logging.getLogger(__name__)
def init_db(db: Session) -> None:
"""Initialize the database, creating tables and initial data."""
# Create tables if they don't exist
Base.metadata.create_all(bind=engine)
# Create initial superuser if needed
user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER_EMAIL)
if not user:
user_in = schemas.UserCreate(
email=settings.FIRST_SUPERUSER_EMAIL,
username=settings.FIRST_SUPERUSER_USERNAME,
password=settings.FIRST_SUPERUSER_PASSWORD,
is_superuser=True,
)
user = crud.user.create(db, obj_in=user_in)
logger.info(f"Superuser {user.email} created successfully")
else:
logger.info("Superuser already exists, skipping creation")