-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide poetry based script to initialise the database #24
Comments
with import asyncio
from labs import db, models
init = db.init_models()
asyncio.run(init) Verify that the tables were created:
|
while you can run scripts via pythom -m, it will be nice to wrap runnable scripts via poetry, and use the taskfile to run modules inside of the containers the use cases are around being able to initialise models, or other such tasks that require a pythonic interface
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
While the above example works, this does not appear to work via the Steps to reproduce
The database engine has
Note that following the above steps does work. Possible cause If this is the case then we might have to refactor the This raises alerts towards updating the documentation around this. |
The bug turns out to be that the def initialise():
""" Async IO containers to run the init_models function
This is called from the command line via poetry scripts.
"""
import asyncio
asyncio.run(init_models()) importing it into context fixes this issue: def initialise():
""" Async IO containers to run the init_models function
This is called from the command line via poetry scripts.
Note: while import the models package seems useless, it is
infact crucial that the models are in context before the
create_all is called, otherwise the context has no models
defined and none will be created.
"""
import asyncio
from . import models
asyncio.run(init_models()) I had left the Note that while to avoid circular dependency we should reference models from the packages directly, but it's easier to aggregate the models to the top package for this sort of utility. |
While initialising my first project from the template I had to do the following to initialise the database using the following steps:
which was essentially me running the
init_db
async method to create the tables. It would be preferable to wrap this up into a poetry script and made available as a task, so the user could do somethingand the initial database schema would be present. This would go hand in hand with #23 and could possible combine the two steps.
The text was updated successfully, but these errors were encountered: