Public Access
mirror of
https://github.com/xlorepdarkhelm/numinar-coding-project.git
synced 2026-09-11 02:23:22 -04:00
Putting the create db stuff back.
Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user