12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- from sqlalchemy.orm import Session
- from app import crud, schemas
- from app.core.config import settings
- from app.db import base # noqa: F401
- from app.models.enum import Membership, Progress
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy import create_engine
- # 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 = declarative_base()
- #Base.query = db.query_property()
- '''
- user = "choozmo"
- password = "pAssw0rd"
- host = "db.ptt.cx:3306"
- db_name = "video_maker"
- engine = create_engine(f'mysql://{user}:{password}@{host}/{db_name}', pool_pre_ping=True)
- base.Base.metadata.create_all(bind=engine)
- if settings.MEMBERSHIP_STATUS:
- for STATUS in settings.MEMBERSHIP_STATUS:
- if not db.query(Membership).filter(Membership.status == STATUS).first():
- db.add(Membership(status=STATUS))
- db.commit()
- if settings.PROGRESS_STATE:
- for STATE in settings.PROGRESS_STATE:
- if not db.query(Progress).filter(Progress.state == STATE).first():
- db.add(Progress(state=STATE))
- db.commit()
-
- user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER)
- if not user:
- user_in = schemas.UserCreate(
- email=settings.FIRST_SUPERUSER,
- password=settings.FIRST_SUPERUSER_PASSWORD,
- is_superuser=True,
- )
- user = crud.user.create(db, obj_in=user_in) # noqa: F841
-
|