Skip to content

Commit 9cdcad4

Browse files
authored
Merge pull request #1 from testdrivenio/updates
add sqlmodel and alembic
2 parents f6c58fe + bb633b0 commit 9cdcad4

File tree

9 files changed

+22
-20
lines changed

9 files changed

+22
-20
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
__pycache__
22
env
3-
htmlcov
4-
.coverage

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
Sample FastAPI project that uses async SQLAlchemy, SQLModel, Postgres, Alembic, and Docker.
44

5-
## Getting Started
5+
## Want to learn how to build this?
6+
7+
Check out the [post](https://testdriven.io/blog/fastapi-sqlmodel/).
8+
9+
## Want to use this project?
610

711
```sh
812
$ docker-compose up -d --build

project/app/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
DATABASE_URL = os.environ.get("DATABASE_URL")
1010

11-
engine = create_async_engine(DATABASE_URL, echo=True)
11+
engine = create_async_engine(DATABASE_URL, echo=True, future=True)
1212

1313

14-
async def create_db_and_tables():
14+
async def init_db():
1515
async with engine.begin() as conn:
1616
# await conn.run_sync(SQLModel.metadata.drop_all)
1717
await conn.run_sync(SQLModel.metadata.create_all)

project/app/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from fastapi import Depends, FastAPI
2-
from sqlalchemy import select
2+
from sqlalchemy.future import select
33
from sqlalchemy.ext.asyncio import AsyncSession
44

55
from app.db import get_session
@@ -25,4 +25,5 @@ async def add_song(song: SongCreate, session: AsyncSession = Depends(get_session
2525
song = Song(name=song.name, artist=song.artist, year=song.year)
2626
session.add(song)
2727
await session.commit()
28+
await session.refresh(song)
2829
return song

project/app/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from typing import Optional
2+
13
from sqlmodel import SQLModel, Field
24

35

46
class SongBase(SQLModel):
57
name: str
68
artist: str
7-
year: str
9+
year: Optional[int] = None
810

911

1012
class Song(SongBase, table=True):

project/migrations/env.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
# for 'autogenerate' support
2323
# from myapp import mymodel
2424
# target_metadata = mymodel.Base.metadata
25-
target_metadata = None
2625
target_metadata = SQLModel.metadata
2726

2827
# other values from the config, defined by the needs of env.py,

project/migrations/versions/94598cb1fa7b_add_year.py renamed to project/migrations/versions/53754b2c08a4_add_year.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
"""add_year
1+
"""add year
22
3-
Revision ID: 94598cb1fa7b
4-
Revises: bf9bd33c0c06
5-
Create Date: 2021-09-08 23:27:31.948299
3+
Revision ID: 53754b2c08a4
4+
Revises: f9c634db477d
5+
Create Date: 2021-09-10 00:52:38.668620
66
77
"""
88
from alembic import op
@@ -11,15 +11,15 @@
1111

1212

1313
# revision identifiers, used by Alembic.
14-
revision = '94598cb1fa7b'
15-
down_revision = 'bf9bd33c0c06'
14+
revision = '53754b2c08a4'
15+
down_revision = 'f9c634db477d'
1616
branch_labels = None
1717
depends_on = None
1818

1919

2020
def upgrade():
2121
# ### commands auto generated by Alembic - please adjust! ###
22-
op.add_column('song', sa.Column('year', sqlmodel.sql.sqltypes.AutoString(), nullable=False))
22+
op.add_column('song', sa.Column('year', sa.Integer(), nullable=True))
2323
op.create_index(op.f('ix_song_year'), 'song', ['year'], unique=False)
2424
# ### end Alembic commands ###
2525

project/migrations/versions/bf9bd33c0c06_init.py renamed to project/migrations/versions/f9c634db477d_init.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""init
22
3-
Revision ID: bf9bd33c0c06
3+
Revision ID: f9c634db477d
44
Revises:
5-
Create Date: 2021-09-08 23:27:12.444844
5+
Create Date: 2021-09-10 00:24:32.718895
66
77
"""
88
from alembic import op
@@ -11,7 +11,7 @@
1111

1212

1313
# revision identifiers, used by Alembic.
14-
revision = 'bf9bd33c0c06'
14+
revision = 'f9c634db477d'
1515
down_revision = None
1616
branch_labels = None
1717
depends_on = None

project/requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
alembic==1.7.1
22
asyncpg==0.24.0
33
fastapi==0.68.1
4-
SQLAlchemy==1.4.23
54
sqlmodel==0.0.4
6-
typer==0.4.0
75
uvicorn==0.15.0

0 commit comments

Comments
 (0)