Skip to content
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

How to work with alembic? #374

Closed
bunyk opened this issue Oct 29, 2018 · 13 comments
Closed

How to work with alembic? #374

bunyk opened this issue Oct 29, 2018 · 13 comments
Assignees
Labels
question A community question, closed when inactive.
Milestone

Comments

@bunyk
Copy link

bunyk commented Oct 29, 2018

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.

@fantix
Copy link
Member

fantix commented Oct 29, 2018

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.

@fantix fantix self-assigned this Oct 29, 2018
@fantix fantix added the question A community question, closed when inactive. label Oct 29, 2018
@fantix fantix added this to the v0.8.x milestone Oct 29, 2018
@bunyk
Copy link
Author

bunyk commented Oct 29, 2018

Awesome, it works.

@omarryhan
Copy link

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

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

@Ovyerus
Copy link

Ovyerus commented Mar 12, 2019

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

@wwwjfy
Copy link
Member

wwwjfy commented Mar 12, 2019

@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.

@Ovyerus
Copy link

Ovyerus commented Mar 12, 2019

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.

@fantix
Copy link
Member

fantix commented Mar 12, 2019

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://.

@Ovyerus
Copy link

Ovyerus commented Mar 12, 2019

Ah, good to know 👍

@jorgeecardona
Copy link

jorgeecardona commented Jun 2, 2019

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.

@wwwjfy
Copy link
Member

wwwjfy commented Jun 3, 2019

@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. 😅

@fantix
Copy link
Member

fantix commented Apr 20, 2020

Closing this one as we have an example now.

@fantix fantix closed this as completed Apr 20, 2020
@austincollinpena
Copy link
Contributor

@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]?

@fantix
Copy link
Member

fantix commented May 24, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A community question, closed when inactive.
Projects
None yet
Development

No branches or pull requests

7 participants