Template
mirror of
https://github.com/Nekidev/uv-fastapi-tortoise.git
synced 2026-09-12 19:57:25 +00:00
First version
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
from pathlib import Path
|
||||
|
||||
from aerich import Command
|
||||
|
||||
from tortoise import Tortoise, connections
|
||||
|
||||
from project.lib import settings
|
||||
|
||||
|
||||
async def on_startup():
|
||||
"""Connects to the database and creates the missing schemas on FastAPI startup."""
|
||||
|
||||
async with Command(
|
||||
tortoise_config=settings.DATABASE, app="models", location="project/db/migrations"
|
||||
) as command:
|
||||
parent_dir = Path(__file__).parent
|
||||
|
||||
if not (parent_dir / "migrations").exists():
|
||||
await command.init_migrations(safe=True)
|
||||
|
||||
await command.upgrade()
|
||||
|
||||
await Tortoise.init(config=settings.CONFIG)
|
||||
|
||||
|
||||
async def on_shutdown():
|
||||
"""Closes the database connections on FastAPI shutdown."""
|
||||
|
||||
await connections.close_all()
|
||||
Reference in New Issue
Block a user