Skip to content

Commit 3236cbd

Browse files
committed
feat: taskfile + poetry based db init REF #24
this is a long standing feature request where you are able to use a taskfile endpoint to initialise the database. sqlalchemy uses asyncio, the following makes available a method in the package which is a asyncio wrapper for the sqlalchemy metadata create and rop methods. you can use 2023-03-06 03:42:45,317 INFO sqlalchemy.engine.Engine select pg_catalog.version() 2023-03-06 03:42:45,317 INFO sqlalchemy.engine.Engine [raw sql] () 2023-03-06 03:42:45,318 INFO sqlalchemy.engine.Engine select current_schema() 2023-03-06 03:42:45,318 INFO sqlalchemy.engine.Engine [raw sql] () 2023-03-06 03:42:45,319 INFO sqlalchemy.engine.Engine show standard_conforming_strings 2023-03-06 03:42:45,319 INFO sqlalchemy.engine.Engine [raw sql] () 2023-03-06 03:42:45,319 INFO sqlalchemy.engine.Engine BEGIN (implicit) 2023-03-06 03:42:45,320 INFO sqlalchemy.engine.Engine COMMIT to invoke this routine, which in turn uses a poetry script to run the method provided by the package. note that this has hard coded references to the labs package which will have to be cleaned by the eject script in #55 poetry is run in the container which should have the configuration variables required for the database environment
1 parent f531ae9 commit 3236cbd

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

Diff for: Taskfile.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tasks:
3636
db:init:
3737
desc: initialise the database schema
3838
cmds:
39-
- docker compose exec api sh -c "poetry run init_db"
39+
- docker compose exec api sh -c "poetry run initdb"
4040
db:rev:
4141
desc: create a database migration, pass a string as commit string
4242
cmds:

Diff for: src/labs/db.py

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ async def init_models():
4747
await conn.run_sync(Base.metadata.drop_all)
4848
await conn.run_sync(Base.metadata.create_all)
4949

50+
51+
def initalise():
52+
""" Async IO containers to run the init_models function
53+
54+
This is called from the command line via poetry
55+
"""
56+
import asyncio
57+
asyncio.run(init_models())
58+
5059
# TODO: Revise implementation in async context
5160
# Donot use until we have a better understanding of how to use async context
5261
# @asynccontextmanager

Diff for: src/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ httpx = "^0.23.3"
4141
watchdog = "^2.1.8"
4242

4343
[tool.poetry.scripts]
44-
init-db = "labs.db:main"
44+
initdb = "labs.db:initalise"
4545

4646
[tool.poetry.group.dev.dependencies]
4747
pytest = "^7.1.3"

0 commit comments

Comments
 (0)