54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
from sqlalchemy.orm import Session
|
|
|
|
from backend.app import crud
|
|
from backend.app.core.config import settings
|
|
|
|
# make sure all SQL Alchemy models are imported (app.db.base) before initializing DB
|
|
# otherwise, SQL Alchemy might fail to initialize relationships properly
|
|
# for more details: https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/28
|
|
from backend.app.db.base_class import Base
|
|
from backend.app.db.session import engine
|
|
from backend.app.models.player import PlayerCreate
|
|
from backend.app.models.team import TeamWithPlayers
|
|
from backend.app.models.user import UserCreate
|
|
|
|
|
|
def init_db(db: Session) -> None:
|
|
# Tables should be created with Alembic migrations
|
|
# But if you don't want to use migrations, create
|
|
# the tables un-commenting the next line
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER)
|
|
if not user:
|
|
user_in = UserCreate(
|
|
email=settings.FIRST_SUPERUSER,
|
|
password=settings.FIRST_SUPERUSER_PASSWORD,
|
|
is_superuser=True,
|
|
)
|
|
user = crud.user.create(db, obj_in=user_in) # noqa: F841
|
|
|
|
players = crud.player.get_multi(db=db)
|
|
|
|
if not players:
|
|
|
|
player_in = PlayerCreate(
|
|
firstname="Simon",
|
|
lastname="Milvert",
|
|
)
|
|
|
|
player = crud.player.create(db=db, obj_in=player_in) # noqa: F841
|
|
player_in = PlayerCreate(
|
|
firstname="Johan",
|
|
lastname="Moden")
|
|
|
|
|
|
player = crud.player.create(db=db, obj_in=player_in) # noqa: F841
|
|
|
|
teams = crud.team.get_multi(db=db)
|
|
|
|
if not True:
|
|
print("Create Team")
|
|
player = crud.player.get(db, id=1)
|
|
team_in = TeamWithPlayers(teamname='1', players=[player])
|
|
team = crud.team.create(db, obj_in=team_in) # noqa: F841 |