Repository navigation
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
While initialising my first project from the template I had to do the following to initialise the database using the following steps:
root@af3b478425ca:/opt# python
Python 3.10.4 (main, May 11 2022, 10:32:02) [GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from acacia import db, models
>>> db.init_models()
<coroutine object init_models at 0xffff993e5a10>
>>> init = db.init_models()
>>> init()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'coroutine' object is not callable
>>> import asyncio
>>> asyncio.run(init)
2022-07-23 01:35:04,927 INFO sqlalchemy.engine.Engine select pg_catalog.version()
2022-07-23 01:35:04,927 INFO sqlalchemy.engine.Engine [raw sql] ()
2022-07-23 01:35:04,928 INFO sqlalchemy.engine.Engine select current_schema()
2022-07-23 01:35:04,928 INFO sqlalchemy.engine.Engine [raw sql] ()
2022-07-23 01:35:04,929 INFO sqlalchemy.engine.Engine show standard_conforming_strings
2022-07-23 01:35:04,929 INFO sqlalchemy.engine.Engine [raw sql] ()
2022-07-23 01:35:04,930 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2022-07-23 01:35:04,931 INFO sqlalchemy.engine.Engine select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=%s
2022-07-23 01:35:04,931 INFO sqlalchemy.engine.Engine [generated in 0.00014s] ('user',)
2022-07-23 01:35:04,932 INFO sqlalchemy.engine.Engine select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=%s
2022-07-23 01:35:04,932 INFO sqlalchemy.engine.Engine [cached since 0.001741s ago] ('user',)
2022-07-23 01:35:04,933 INFO sqlalchemy.engine.Engine
CREATE TABLE "user" (
id UUID DEFAULT gen_random_uuid() NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL,
email VARCHAR,
password VARCHAR,
verified BOOLEAN DEFAULT false NOT NULL,
mobile_number VARCHAR,
first_name VARCHAR,
last_name VARCHAR,
is_admin BOOLEAN DEFAULT false NOT NULL,
otp_secret VARCHAR,
PRIMARY KEY (id),
UNIQUE (email)
)
2022-07-23 01:35:04,933 INFO sqlalchemy.engine.Engine [no key 0.00007s] ()
2022-07-23 01:35:04,939 INFO sqlalchemy.engine.Engine COMMITwhich 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 something
task db:init
and the initial database schema would be present. This would go hand in hand with #23 and could possible combine the two steps.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Activity
feat: adds poetry based script to init database REFS #24
devraj commentedon Sep 14, 2022
with
asyncpgwe have to run the init methods in aasyncloopVerify that the tables were created:
refactor: attempt to get scripts working (REFS #24)
feat: taskfile + poetry based db init REF #24
devraj commentedon Mar 22, 2023
While the above example works, this does not appear to work via the
poetryscript:Steps to reproduce
task db:initThe database engine has
echo=Truehence you should see theSQLstatements if the tables are created, instead you see:Note that following the above steps does work.
Possible cause
The method that
poetrycalls wraps theasyncfunction in a method. I have not investigated this yet, but it's well possible thatasyncio.runmight have to be called outside of a function scope which is what would be happening when it's run on thepythonshell.If this is the case then we might have to refactor the
db.pypackage to be a folder and then have a__main__.pythat has the initialise function.This raises alerts towards updating the documentation around this.
devraj commentedon May 20, 2023
The bug turns out to be that the
initialisemethod did not havemodelsimported into context:importing it into context fixes this issue:
I had left the
modelsimport out thinking that it had no effect, but clearly not importing does not give SQLAlchemy the context of the models.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.