Putting the create db stuff back.

Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
2025-08-22 19:28:17 -04:00
parent 0164fa6e7f
commit 0dccf3e347
2 changed files with 18 additions and 0 deletions
+10
View File
@@ -5,6 +5,7 @@ from typing import Any
from fastapi import FastAPI
from backend import config
from backend.models import create_db_and_tables
ENVIRONMENT = config("ENVIRONMENT", default="development")
@@ -16,6 +17,15 @@ if ENVIRONMENT not in SHOW_DOCS_ENVIRONMENTS:
app_configs["openapi_url"] = None # Disable OpenAPI schema
async def lifespan(app: FastAPI):
"""Lifespan event to create database tables on startup."""
await create_db_and_tables()
yield # This is where the lifespan ends
app_configs["lifespan"] = lifespan
app = FastAPI(**app_configs)
+8
View File
@@ -9,6 +9,8 @@ from sqlalchemy.orm import MappedAsDataclass
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
from backend.db import engine
# Base class for declarative models
class Base(MappedAsDataclass, DeclarativeBase):
@@ -78,3 +80,9 @@ class Invitee(Base):
booking: Mapped["Booking"] = relationship(back_populates="invitees", init=False)
user: Mapped["User"] = relationship(back_populates="invitees", init=False)
async def create_db_and_tables() -> None: # pragma: no cover
"""Create the database and tables."""
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)