Skip to content

Commit

Permalink
feat: taskfile + poetry based db init REF #24
Browse files Browse the repository at this point in the history
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
  • Loading branch information
devraj committed Mar 6, 2023
1 parent f531ae9 commit 3236cbd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tasks:
db:init:
desc: initialise the database schema
cmds:
- docker compose exec api sh -c "poetry run init_db"
- docker compose exec api sh -c "poetry run initdb"
db:rev:
desc: create a database migration, pass a string as commit string
cmds:
Expand Down
9 changes: 9 additions & 0 deletions src/labs/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ async def init_models():
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)


def initalise():
""" Async IO containers to run the init_models function
This is called from the command line via poetry
"""
import asyncio
asyncio.run(init_models())

# TODO: Revise implementation in async context
# Donot use until we have a better understanding of how to use async context
# @asynccontextmanager
Expand Down
2 changes: 1 addition & 1 deletion src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ httpx = "^0.23.3"
watchdog = "^2.1.8"

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

[tool.poetry.group.dev.dependencies]
pytest = "^7.1.3"
Expand Down

0 comments on commit 3236cbd

Please sign in to comment.