File size: 766 Bytes
c053e7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from app.sql.models import Base
from app.sql.models import User
from app.core.crud import hash_password
from app.sql.database import SessionLocal, engine
from app.config import SUPERUSER_USERNAME, SUPERUSER_PASSWORD


def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()


def init_db():
    db = SessionLocal()
    Base.metadata.create_all(bind=engine)

    hashed_superuser_password = hash_password(SUPERUSER_PASSWORD)
    superuser = User(username=SUPERUSER_USERNAME,
                     password=hashed_superuser_password, is_superuser=True, is_active=True)

    if db.query(User).filter(User.username == SUPERUSER_USERNAME).first() is None:
        db.add(superuser)
        db.commit()
        db.refresh(superuser)