Skip to content

Commit 0095d81

Browse files
committed
fix: init script not creating models REFS #24
for the longest period of time i had been wondering what i was doing wrong where what is in the comments of the ticket works but then formalising it here did not work https://github.com/anomaly/lab-python-server/issues/24\#issuecomment-1246172651 i was initially suspecting asyncio related issues but it turns out that the models of the application were never imported into the context when the create_all was called, which i accidentally had in context when i prototyped the calls in the comment there are notes on the initialise method as to why the models are in context this also fixes the typo in the poetry command CLOSES #24
1 parent 668536a commit 0095d81

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

src/labs/db.py

+7-20
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,14 @@ async def init_models():
5151
def initialise():
5252
""" Async IO containers to run the init_models function
5353
54-
This is called from the command line via poetry
54+
This is called from the command line via poetry scripts.
55+
56+
Note: while import the models package seems useless, it is
57+
infact crucial that the models are in context before the
58+
create_all is called, otherwise the context has no models
59+
defined and none will be created.
5560
"""
5661
import asyncio
62+
from . import models
5763
asyncio.run(init_models())
5864

59-
# TODO: Revise implementation in async context
60-
# Donot use until we have a better understanding of how to use async context
61-
# @asynccontextmanager
62-
# async def session_context():
63-
# """Provide a transactional scope around a series of operations.
64-
# """
65-
# try:
66-
# yield async_session
67-
# await async_session.commit()
68-
# except: # noqa: E722
69-
# await async_session.rollback()
70-
# raise
71-
# finally:
72-
# await async_session.close()
73-
74-
75-
if __name__ == "__main__":
76-
import asyncio
77-
asyncio.run(init_models())

src/pyproject.toml

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
name = "labs"
33
version = "0.1.0"
44
description = "A template for building Python based web applications"
5-
authors = [
6-
"Dev Mukherjee <[email protected]>"
7-
]
5+
authors = ["Dev Mukherjee <[email protected]>"]
86
repository = "https://github.com/anomaly/lab-python-server"
97
documentation = "https://anomaly.academy"
108
readme = "README.md"
@@ -45,12 +43,12 @@ taskiq-aio-pika = "^0.2.0"
4543
watchdog = "^2.1.8"
4644

4745
[tool.poetry.scripts]
48-
initdb = "labs.db:initalise"
46+
initdb = "labs.db:initialise"
4947

5048
[tool.poetry.group.dev.dependencies]
5149
pytest = "^7.1.3"
5250
pytest-order = "^1.0.1"
53-
taskiq = {extras = ["reload"], version = "^0.4.1"}
51+
taskiq = { extras = ["reload"], version = "^0.4.1" }
5452
faker = "^18.6.0"
5553

5654
[build-system]

0 commit comments

Comments
 (0)