forked from fastapi/full-stack-fastapi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Adopt SQLModel, create models, start using it (fastapi#559)
* 🔥 Remove old SQLAlchemy models * ✨ Add new SQLModel models * 🔧 Update Alembic configs to work with SQLModel * ✨ Re-generate initial Alembic migration * 🔧 Update PostgreSQL driver connection string URL * ✨ Create new SQLModel engine * 🔥 Remove old unneeded SQLAlchemy-specific files * ♻️ Update init_db * ♻️ Use new SQLModel session * ♻️ Update conftest with new DB Session * ♻️ Update pre-start scripts to use SQLModel session * ♻️ Import new SQLModel models * ✨ Create new simplified create_user crud util * ♻️ Update import in CRUDBase class (soon to be removed) * 🙈 Update .gitignore with Python files
- Loading branch information
Showing
26 changed files
with
193 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
__pycache__ | ||
app.egg-info | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 0 additions & 59 deletions
59
src/backend/app/alembic/versions/d4867f3a4c0a_first_revision.py
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
src/backend/app/alembic/versions/e2412789c190_initialize_models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Initialize models | ||
Revision ID: e2412789c190 | ||
Revises: | ||
Create Date: 2023-11-24 22:55:43.195942 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
import sqlmodel.sql.sqltypes | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'e2412789c190' | ||
down_revision = None | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('user', | ||
sa.Column('email', sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column('is_active', sa.Boolean(), nullable=False), | ||
sa.Column('is_superuser', sa.Boolean(), nullable=False), | ||
sa.Column('full_name', sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('hashed_password', sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_index(op.f('ix_user_email'), 'user', ['email'], unique=True) | ||
op.create_table('item', | ||
sa.Column('description', sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('title', sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column('owner_id', sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(['owner_id'], ['user.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('item') | ||
op.drop_index(op.f('ix_user_email'), table_name='user') | ||
op.drop_table('user') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from sqlmodel import create_engine | ||
|
||
from app.core.config import settings | ||
|
||
engine = create_engine(settings.SQLALCHEMY_DATABASE_URI) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
from sqlalchemy.orm import Session | ||
from sqlmodel import Session, select | ||
|
||
from app import crud, schemas | ||
from app import crud | ||
from app.core.config import settings | ||
from app.db import base # noqa: F401 | ||
from app.models import User, UserCreate # noqa: F401 | ||
|
||
# make sure all SQL Alchemy models are imported (app.db.base) before initializing DB | ||
# otherwise, SQL Alchemy might fail to initialize relationships properly | ||
# make sure all SQLModel models are imported (app.models) before initializing DB | ||
# otherwise, SQLModel 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: | ||
def init_db(session: 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) | ||
user = session.exec( | ||
select(User).where(User.email == settings.FIRST_SUPERUSER) | ||
).first() | ||
if not user: | ||
user_in = schemas.UserCreate( | ||
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 | ||
user = crud.create_user(session, user_create=user_in) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.