diff --git a/docs_src/sql_databases/tutorial001.py b/docs_src/sql_databases/tutorial001.py index be86ec0ee..3e824255b 100644 --- a/docs_src/sql_databases/tutorial001.py +++ b/docs_src/sql_databases/tutorial001.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import List, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -27,12 +28,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_an.py b/docs_src/sql_databases/tutorial001_an.py index 8c000d31c..9e492546e 100644 --- a/docs_src/sql_databases/tutorial001_an.py +++ b/docs_src/sql_databases/tutorial001_an.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import List, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -30,12 +31,14 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() - -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_an_py310.py b/docs_src/sql_databases/tutorial001_an_py310.py index de1fb81fa..2054877cb 100644 --- a/docs_src/sql_databases/tutorial001_an_py310.py +++ b/docs_src/sql_databases/tutorial001_an_py310.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Annotated from fastapi import Depends, FastAPI, HTTPException, Query @@ -29,12 +30,14 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() - -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_an_py39.py b/docs_src/sql_databases/tutorial001_an_py39.py index 595892746..770151ec4 100644 --- a/docs_src/sql_databases/tutorial001_an_py39.py +++ b/docs_src/sql_databases/tutorial001_an_py39.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Annotated, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -29,12 +30,14 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() - -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_py310.py b/docs_src/sql_databases/tutorial001_py310.py index b58462e6a..a38000caf 100644 --- a/docs_src/sql_databases/tutorial001_py310.py +++ b/docs_src/sql_databases/tutorial001_py310.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + from fastapi import Depends, FastAPI, HTTPException, Query from sqlmodel import Field, Session, SQLModel, create_engine, select @@ -25,12 +27,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_py39.py b/docs_src/sql_databases/tutorial001_py39.py index 410a52d0c..22de0dd53 100644 --- a/docs_src/sql_databases/tutorial001_py39.py +++ b/docs_src/sql_databases/tutorial001_py39.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -27,12 +28,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial002.py b/docs_src/sql_databases/tutorial002.py index 4350d19c6..e42a3a75f 100644 --- a/docs_src/sql_databases/tutorial002.py +++ b/docs_src/sql_databases/tutorial002.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import List, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -44,12 +45,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_an.py b/docs_src/sql_databases/tutorial002_an.py index 15e3d7c3a..72570196b 100644 --- a/docs_src/sql_databases/tutorial002_an.py +++ b/docs_src/sql_databases/tutorial002_an.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import List, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -46,12 +47,15 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_an_py310.py b/docs_src/sql_databases/tutorial002_an_py310.py index 64c554b8a..3baa99666 100644 --- a/docs_src/sql_databases/tutorial002_an_py310.py +++ b/docs_src/sql_databases/tutorial002_an_py310.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Annotated from fastapi import Depends, FastAPI, HTTPException, Query @@ -45,12 +46,15 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_an_py39.py b/docs_src/sql_databases/tutorial002_an_py39.py index a8a0721ff..4ff0628b4 100644 --- a/docs_src/sql_databases/tutorial002_an_py39.py +++ b/docs_src/sql_databases/tutorial002_an_py39.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Annotated, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -45,12 +46,15 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_py310.py b/docs_src/sql_databases/tutorial002_py310.py index ec3d68db5..c2efe088c 100644 --- a/docs_src/sql_databases/tutorial002_py310.py +++ b/docs_src/sql_databases/tutorial002_py310.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + from fastapi import Depends, FastAPI, HTTPException, Query from sqlmodel import Field, Session, SQLModel, create_engine, select @@ -42,12 +44,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_py39.py b/docs_src/sql_databases/tutorial002_py39.py index d8f5dd090..74c70d0f6 100644 --- a/docs_src/sql_databases/tutorial002_py39.py +++ b/docs_src/sql_databases/tutorial002_py39.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -44,12 +45,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic)