from sqlalchemy.orm import Session from app import crud, schemas from app.core.config import settings from app.db import base # noqa: F401 # make sure all SQL Alchemy models are imported (app.db.base) before initializing DB # otherwise, SQL Alchemy might fail to initialize relationships properly # for more details: https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/28 def init_db(db: Session) -> None: # Create first admin user if not exists user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER) if not user: user_in = schemas.UserCreate( email=settings.FIRST_SUPERUSER, password=settings.FIRST_SUPERUSER_PASSWORD, is_superuser=True, full_name="Initial Admin User", ) user = crud.user.create(db, obj_in=user_in) # Add some initial departments if none exist if db.query(base.Department).count() == 0: departments = [ {"name": "HR", "description": "Human Resources Department"}, {"name": "IT", "description": "Information Technology Department"}, {"name": "Finance", "description": "Finance and Accounting Department"}, {"name": "Marketing", "description": "Marketing and Sales Department"}, {"name": "Operations", "description": "Operations and Production Department"}, ] for dept in departments: dept_in = schemas.DepartmentCreate(**dept) crud.department.create(db, obj_in=dept_in) # Add some initial job titles if none exist if db.query(base.JobTitle).count() == 0: job_titles = [ {"title": "HR Manager", "description": "Manages HR operations"}, {"title": "Software Engineer", "description": "Develops software applications"}, {"title": "Accountant", "description": "Handles financial records and accounting"}, {"title": "Marketing Specialist", "description": "Manages marketing campaigns"}, {"title": "Operations Manager", "description": "Manages day-to-day operations"}, ] for title in job_titles: title_in = schemas.JobTitleCreate(**title) crud.job_title.create(db, obj_in=title_in)