Gino: How to work with alembic?

Created on 29 Oct 2018  路  13Comments  路  Source: python-gino/gino

Tutorial mentions following:

In practice create_all() is usually not an ideal solution. To manage database schema, tool like Alembic is recommended.

What I Did

I followed Alembic docs about Auto Generating Migrations and it tells to do:

from myapp.mymodel import Base
target_metadata = Base.metadata

I imported my model class - got error about missing attribute. Same when I did it for base db.Model:

from widgets.models import db
target_metadata = db.Model.metadata

Documentation needs some section about where to get that metadata for alembic to autogenerate migrations.

question

Most helpful comment

For future reference, Here's a gist that you might find helpful:

https://gist.github.com/omarryhan/cb7bf6d7142c154496ba6623a56326c2

All 13 comments

Ah right, will add in FAQ. Thanks for the report!

db is just the metadata. So this will do:

from widgets.models import db as target_metadata

You can see this in a very outdated example here.

Awesome, it works.

For future reference, Here's a gist that you might find helpful:

https://gist.github.com/omarryhan/cb7bf6d7142c154496ba6623a56326c2

Trying to get Alembic and Gino working together as well, but I'm having some issues with running any Alembic commands, saying AttributeError: type object 'AsyncpgDBAPI' has no attribute 'connect'

img

@Ovyerus I don't know the context of the code you attached. Alembic doesn't support Python's async way, and Gino will only provide metadata or db schema that Alembic can use to infer the migration.
I believe underneath it uses psycopg2 to access PostgreSQL.

Alembic env.py in run_migrations_online. And turns out that the issue was because I forgot to replace the asyncpg in my URL with postgres so I guess it tried to use the asyncpg driver. Brain fart.

You can always use postgres:// or postgresql:// in URL, which works for both GINO and Alembic. Because the default driver of postgresql for GINO engine is asyncpg, therefore postgres:// is short for postgresql+asyncpg://; while for normal SQLAlchemy/Alembic, the default driver is psycopg2, the same short form shall be correctly expanded into postgresql+psycopg2://.

Ah, good to know 馃憤

Hey, I recently came across this situation. I am able to use alembic with gino and handle some basic migrations, but I really don't like the interaction between the two by different reasons:

  • the API exposed by sqlalchemy used by alembic does not exist in gino, e.g. connection.begin vs connection.transaction
  • To deal with migrations and work async I need to have psycopg2 and asyncpg, this makes no sense to me.

Do you have a plan to deal with any of the issues I am seeing?

Best,
Jorge.

@jorgeecardona Thanks for pointing it out! I'm well aware of this issue. As I checked out code in Alembic, it currently can't support Python's async style.
The easy answer, I think, is to have an async version of Alembic, but it'll not be easy task. 馃槄

Closing this one as we have an example now.

@fantix Can I use this without Poetry? How do I run the load_modules() function without it? What goes in the for ep in entry_points["variable]?

@austincollinpena please also see this how-to by @xnuinside in #672

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ape364 picture ape364  路  6Comments

willyhakim picture willyhakim  路  4Comments

saeedghx68 picture saeedghx68  路  4Comments

quantum13 picture quantum13  路  6Comments

AmatanHead picture AmatanHead  路  5Comments