backend: api: Create database tables at startup
We might need to use Alembic for schema migrations at some point, but until then, creating the database tables when the application first starts will work fine.master
parent
e33502eee1
commit
0ab0ef8874
|
@ -2,8 +2,9 @@
|
|||
from typing import Awaitable, Callable
|
||||
|
||||
import fastapi
|
||||
import sqlalchemy
|
||||
|
||||
from .. import __version__
|
||||
from .. import __version__, tables
|
||||
from ..config import Configuration
|
||||
from .context import Context
|
||||
|
||||
|
@ -22,6 +23,12 @@ context = Context()
|
|||
context.config = Configuration()
|
||||
|
||||
|
||||
@app.on_event('startup')
|
||||
def start() -> None:
|
||||
engine = sqlalchemy.create_engine(context.config.database.url)
|
||||
tables.metadata.create_all(engine)
|
||||
|
||||
|
||||
@app.middleware('http')
|
||||
async def context_middleware(
|
||||
request: fastapi.Request,
|
||||
|
|
Loading…
Reference in New Issue