Skip to content

Commit 3205a35

Browse files
committed
Fixes #4 - pysqlite3 doesn't support asyncio
1 parent 52c9e75 commit 3205a35

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

code/ch8-async-databases/data/db_session.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@ def global_init(db_file: str):
2424
folder = Path(db_file).parent
2525
folder.mkdir(parents=True, exist_ok=True)
2626

27-
conn_str = 'sqlite:///' + db_file.strip()
28-
print("Connecting to DB with {}".format(conn_str))
27+
# Post-recording update:
28+
# SQLAlchemy started enforcing the underlying Python DB API was truly async
29+
# We don't really get that with SQLite but when you switch something like Postgres
30+
# It would "light up" with async. Since recording, SQLAlchemy throws and error
31+
# if this would be the case. We need to explicitly switch to aiosqlite as below.
32+
conn_str = 'sqlite+pysqlite:///' + db_file.strip()
33+
async_conn_str = 'sqlite+aiosqlite:///' + db_file.strip()
34+
print("Connecting to DB with {}".format(async_conn_str))
2935

3036
# Adding check_same_thread = False after the recording. This can be an issue about
3137
# creating / owner thread when cleaning up sessions, etc. This is a sqlite restriction
3238
# that we probably don't care about in this example.
3339
engine = sa.create_engine(conn_str, echo=False, connect_args={"check_same_thread": False})
34-
__async_engine = create_async_engine(conn_str, echo=False, connect_args={"check_same_thread": False})
40+
__async_engine = create_async_engine(async_conn_str, echo=False, connect_args={"check_same_thread": False})
3541
__factory = orm.sessionmaker(bind=engine)
3642

3743
# noinspection PyUnresolvedReferences
0 Bytes
Binary file not shown.

code/ch8-async-databases/requirements.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ python-multipart
66
git+https://github.com/mikeckennedy/fastapi-chameleon
77

88
starlette==0.13.6
9-
SQLAlchemy==1.4.0b1
109
progressbar2
1110
python-dateutil
1211
passlib
1312

13+
# Post-recording updates:
14+
# SQLAlchemy is out of beta for 1.4+:
15+
SQLAlchemy>=1.4.3
16+
17+
# SQLAlchemy started enforcing the underlying Python DB API was truly async
18+
# We don't really get that with SQLite but when you switch something like Postgres
19+
# It would "light up" with async. Since recording, SQLAlchemy throws and error
20+
# if this would be the case. We need to explicitly switch to aiosqlite.
21+
aiosqlite>=0.17.0
22+
1423

code/ch8-async-databases/services/user_service.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ async def login_user(email: str, password: str) -> Optional[User]:
3737
if not user:
3838
return user
3939

40-
if not crypto.verify(password, user.hash_password):
40+
try:
41+
if not crypto.verify(password, user.hash_password):
42+
return None
43+
except ValueError:
4144
return None
4245

4346
return user

0 commit comments

Comments
 (0)