alchemical
alchemical copied to clipboard
alchemical.aio.Alchemical with Flask and Flask-Migrate
Hey Miguel :wave: I hope all is well! I'm wondering if I could use Flask, and the as alchemical.aio.Alchemical object with Flask-Migrate. This seems like a combination that is probably not possible but I hope I'm wrong. For a somewhat artificial reason, I'm happy with uwsgi as my application server, and I don't want to switch over to aioflask/asgi/etc just yet. I've discovered some features in uwsgi that are keeping me there (for now). I'm interested in being able to get migrations, and utilize the new async support in Flask. Alchemical seems to get me really close! Curious to hear your thoughts on this, any advice appreciated.
You can use Alchemical with Flask and Flask-Migrate. There's even two examples that show how:
https://github.com/miguelgrinberg/alchemical/tree/main/examples/flask-single-db https://github.com/miguelgrinberg/alchemical/tree/main/examples/flask-multi-db
But this isn't async. You may have a misunderstanding with regards to how Alchemical works. This is just a wrapper for SQLAlchemy. Like it, both sync and async workflows are supported. Flask uses sync code, so the sync SQLAlchemy and Alchemical support is used. If you work with FastAPI or any other async framework, then the async support in SQLAlchemy and Alchemical is used.
Also, there is nothing wrong with working with Flask and sync code. You make it sound as if people must migrate to async solutions. That is really not the case.
I should have also mentioned that I am interested in using Flask in the new async mode that ships with. So I’m interested in using the aio Alchemical object with the new support that Flask async comes with. Then I want to also use Flask-Migrate to deal with migrations. I was unsure if this combination works.
The problem is that I do want to work with async code for this particular Flask app. I just don’t want to go full-async and make use of WsgiToAsgi. I also don’t want to stray away from vanilla Flask and uwsgi. So I know I’m operating in a weird area of constraints. Appreciate talking it through with you. Apologies for my brevity, I hope the point comes across. I will clarify anything later based on your comments.
The async support in Flask is not very useful, so I don't intend to support it in Alchemical. I'm not sure if the aio.Alchemical
class will work with it or not, but it might. You will not be able to use the Flask integration, though, it would be just creating the aio.Alchemical
object and using it more or less like you would in a FastAPI application.
Adding Flask-Migrate is going to be tricky, because that relies on the sync code.
I see. I think I may be better off with a different approach in this scenario, not to any fault of this project. I think may not be the right fit for my set of constraints. There’s probably a path of a less resistance with another approach. But I’ve not entirely given up yet. Maybe there is something I can work by just using Alembic directly rather than Flask-Migrate (which pains me a little because I like it…) while still using Alchemical