-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathinit_db.py
More file actions
73 lines (65 loc) · 2.44 KB
/
init_db.py
File metadata and controls
73 lines (65 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging
from sqlalchemy.orm import Session
from app import crud, schemas
from app.db import base # noqa: F401
from app.core.config import settings
logger = logging.getLogger(__name__)
RECIPES = [
{
"id": 1,
"label": "Chicken Vesuvio",
"source": "Serious Eats",
"url": "http://www.seriouseats.com/recipes/2011/12/chicken-vesuvio-recipe.html",
},
{
"id": 2,
"label": "Chicken Paprikash",
"source": "No Recipes",
"url": "http://norecipes.com/recipe/chicken-paprikash/",
},
{
"id": 3,
"label": "Cauliflower and Tofu Curry Recipe",
"source": "Serious Eats",
"url": "http://www.seriouseats.com/recipes/2011/02/cauliflower-and-tofu-curry-recipe.html", # noqa
},
]
# 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
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)
if settings.FIRST_SUPERUSER:
user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER)
if not user:
user_in = schemas.UserCreate(
first_name="First_name",
surname="Surname",
email=settings.FIRST_SUPERUSER,
is_superuser=True,
password=settings.FIRST_SUPERUSER_PW,
)
user = crud.user.create(db, obj_in=user_in) # noqa: F841
else:
logger.warning(
"Skipping creating superuser. User with email "
f"{settings.FIRST_SUPERUSER} already exists. "
)
if not user.recipes:
for recipe in RECIPES:
recipe_in = schemas.RecipeCreate(
label=recipe["label"],
source=recipe["source"],
url=recipe["url"],
submitter_id=user.id,
)
crud.recipe.create(db, obj_in=recipe_in)
else:
logger.warning(
"Skipping creating superuser. FIRST_SUPERUSER needs to be "
"provided as an env variable. "
"e.g. FIRST_SUPERUSER=admin@api.coursemaker.io"
)