2024-03-02 05:01:59 -05:00
|
|
|
from sqlmodel import Session, create_engine, select
|
:recycle: Refactor backend, settings, DB sessions, types, configs, plugins (#158)
* :recycle: Refactor backend, update DB session handling
* :sparkles: Add mypy config and plugins
* :heavy_plus_sign: Use Python-jose instead of PyJWT
as it has some extra functionalities and features
* :sparkles: Add/update scripts for test, lint, format
* :wrench: Update lint and format configs
* :art: Update import format, comments, and types
* :art: Add types to config
* :sparkles: Add types for all the code, and small fixes
* :art: Use global imports to simplify exploring with Jupyter
* :recycle: Import schemas and models, instead of each class
* :truck: Rename db_session to db for simplicity
* :pushpin: Update dependencies installation for testing
2020-04-20 19:03:13 +02:00
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
from app import crud
|
2020-04-16 23:56:10 -06:00
|
|
|
from app.core.config import settings
|
2024-03-02 05:01:59 -05:00
|
|
|
from app.models import User, UserCreate
|
|
|
|
|
|
|
|
engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI))
|
|
|
|
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
# make sure all SQLModel models are imported (app.models) before initializing DB
|
|
|
|
# otherwise, SQLModel might fail to initialize relationships properly
|
2024-03-12 20:23:20 +01:00
|
|
|
# for more details: https://github.com/tiangolo/full-stack-fastapi-template/issues/28
|
2019-05-22 13:29:24 +02:00
|
|
|
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
def init_db(session: Session) -> None:
|
2019-02-09 19:42:36 +04:00
|
|
|
# Tables should be created with Alembic migrations
|
|
|
|
# But if you don't want to use migrations, create
|
2024-02-25 21:51:56 +01:00
|
|
|
# the tables un-commenting the next lines
|
|
|
|
# from sqlmodel import SQLModel
|
|
|
|
|
2024-03-02 05:01:59 -05:00
|
|
|
# from app.core.engine import engine
|
2024-02-25 21:51:56 +01:00
|
|
|
# This works because the models are already imported and registered from app.models
|
|
|
|
# SQLModel.metadata.create_all(engine)
|
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
user = session.exec(
|
|
|
|
select(User).where(User.email == settings.FIRST_SUPERUSER)
|
|
|
|
).first()
|
2019-02-09 19:42:36 +04:00
|
|
|
if not user:
|
2023-11-25 00:08:22 +01:00
|
|
|
user_in = UserCreate(
|
2020-04-16 23:56:10 -06:00
|
|
|
email=settings.FIRST_SUPERUSER,
|
|
|
|
password=settings.FIRST_SUPERUSER_PASSWORD,
|
2019-02-09 19:42:36 +04:00
|
|
|
is_superuser=True,
|
|
|
|
)
|
2023-11-29 17:22:15 -05:00
|
|
|
user = crud.create_user(session=session, user_create=user_in)
|