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.
This commit is contained in:
@@ -2,8 +2,9 @@
|
|||||||
from typing import Awaitable, Callable
|
from typing import Awaitable, Callable
|
||||||
|
|
||||||
import fastapi
|
import fastapi
|
||||||
|
import sqlalchemy
|
||||||
|
|
||||||
from .. import __version__
|
from .. import __version__, tables
|
||||||
from ..config import Configuration
|
from ..config import Configuration
|
||||||
from .context import Context
|
from .context import Context
|
||||||
|
|
||||||
@@ -22,6 +23,12 @@ context = Context()
|
|||||||
context.config = Configuration()
|
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')
|
@app.middleware('http')
|
||||||
async def context_middleware(
|
async def context_middleware(
|
||||||
request: fastapi.Request,
|
request: fastapi.Request,
|
||||||
|
|||||||
Reference in New Issue
Block a user