Fix Pydantic BaseSettings import error and improve application startup
- Update import from pydantic.BaseSettings to pydantic_settings.BaseSettings
- Add pydantic-settings==2.1.0 to requirements.txt for Pydantic v2 compatibility
- Implement application lifespan management for database initialization
- Add init_db.py for automatic database table creation and superuser setup
- Create test_imports.py for verifying import integrity
- Ensure proper startup sequence and error handling
This fixes the health check failure caused by Pydantic v2 migration changes.
🤖 Generated with BackendIM
Co-Authored-By: BackendIM <noreply@anthropic.com>
This commit is contained in:
parent
5a02fb8b1f
commit
f1326e83e6
@ -1,5 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
from pydantic import BaseSettings
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
PROJECT_NAME: str = "School Portal API"
|
PROJECT_NAME: str = "School Portal API"
|
||||||
|
25
app/db/init_db.py
Normal file
25
app/db/init_db.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.db.session import engine
|
||||||
|
from app.db.base import Base
|
||||||
|
from app.models import user, class_model, subject, grade, attendance, notification
|
||||||
|
from app.core.config import settings
|
||||||
|
from app.services.user import user_service
|
||||||
|
from app.schemas.user import UserCreate
|
||||||
|
from app.models.user import UserRole
|
||||||
|
|
||||||
|
def init_db(db: Session) -> None:
|
||||||
|
# Create tables
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
# Create initial superuser
|
||||||
|
user = user_service.get_by_email(db, email=settings.FIRST_SUPERUSER_EMAIL)
|
||||||
|
if not user:
|
||||||
|
user_in = UserCreate(
|
||||||
|
email=settings.FIRST_SUPERUSER_EMAIL,
|
||||||
|
password=settings.FIRST_SUPERUSER_PASSWORD,
|
||||||
|
first_name="System",
|
||||||
|
last_name="Administrator",
|
||||||
|
role=UserRole.ADMIN,
|
||||||
|
is_active=True
|
||||||
|
)
|
||||||
|
user = user_service.create(db, obj_in=user_in)
|
17
main.py
17
main.py
@ -1,7 +1,21 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
from app.api.v1.api import api_router
|
from app.api.v1.api import api_router
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
|
from app.db.session import SessionLocal
|
||||||
|
from app.db.init_db import init_db
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
# Startup
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
init_db(db)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
yield
|
||||||
|
# Shutdown
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title=settings.PROJECT_NAME,
|
title=settings.PROJECT_NAME,
|
||||||
@ -9,7 +23,8 @@ app = FastAPI(
|
|||||||
description="School Portal API - A comprehensive school management system",
|
description="School Portal API - A comprehensive school management system",
|
||||||
openapi_url="/openapi.json",
|
openapi_url="/openapi.json",
|
||||||
docs_url="/docs",
|
docs_url="/docs",
|
||||||
redoc_url="/redoc"
|
redoc_url="/redoc",
|
||||||
|
lifespan=lifespan
|
||||||
)
|
)
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
|
@ -3,6 +3,7 @@ uvicorn[standard]==0.24.0
|
|||||||
sqlalchemy==2.0.23
|
sqlalchemy==2.0.23
|
||||||
alembic==1.12.1
|
alembic==1.12.1
|
||||||
pydantic[email]==2.5.0
|
pydantic[email]==2.5.0
|
||||||
|
pydantic-settings==2.1.0
|
||||||
python-jose[cryptography]==3.3.0
|
python-jose[cryptography]==3.3.0
|
||||||
passlib[bcrypt]==1.7.4
|
passlib[bcrypt]==1.7.4
|
||||||
python-multipart==0.0.6
|
python-multipart==0.0.6
|
||||||
|
42
test_imports.py
Normal file
42
test_imports.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Test script to verify all imports work correctly."""
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("Testing core imports...")
|
||||||
|
from app.core.config import settings
|
||||||
|
print("✓ Config imported successfully")
|
||||||
|
|
||||||
|
from app.core.security import create_access_token
|
||||||
|
print("✓ Security imported successfully")
|
||||||
|
|
||||||
|
print("Testing database imports...")
|
||||||
|
from app.db.base import Base
|
||||||
|
from app.db.session import SessionLocal, engine
|
||||||
|
print("✓ Database imports successful")
|
||||||
|
|
||||||
|
print("Testing model imports...")
|
||||||
|
from app.models import User, Class, Subject, Grade, Attendance, Notification
|
||||||
|
print("✓ All models imported successfully")
|
||||||
|
|
||||||
|
print("Testing schema imports...")
|
||||||
|
from app.schemas import User as UserSchema, Token
|
||||||
|
print("✓ All schemas imported successfully")
|
||||||
|
|
||||||
|
print("Testing service imports...")
|
||||||
|
from app.services.user import user_service
|
||||||
|
print("✓ All services imported successfully")
|
||||||
|
|
||||||
|
print("Testing API imports...")
|
||||||
|
from app.api.v1.api import api_router
|
||||||
|
print("✓ API router imported successfully")
|
||||||
|
|
||||||
|
print("Testing main application...")
|
||||||
|
from main import app
|
||||||
|
print("✓ Main application imported successfully")
|
||||||
|
|
||||||
|
print("\n🎉 All imports successful! Application should start correctly.")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Import error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
Loading…
x
Reference in New Issue
Block a user