Flask-migrate: alembic stuck on database upgrade

Created on 26 Jan 2015  路  12Comments  路  Source: miguelgrinberg/Flask-Migrate

I'm running a database migration, and Alembic complains with
alembic.util.CommandError: Target database is not up to date.

So and I then run a database upgrade
python manage.py db upgrade

Here is the output:
INFO [alembic.migration] Context impl PostgresqlImpl.
INFO [alembic.migration] Will assume transactional DDL.
INFO [alembic.migration] Running upgrade 604497a5e5 -> 431e33e6fce, empty message

The issue is that Alembic never finishes the upgrade (never quits the terminal) and it appears stuck on the third step, I have tried severally, even leaving it for several minutes but it never completes the upgrade.
I have tried running this locally and on the server and it doesn't work (it's not an issue with the connection).

Most helpful comment

Thanks for the reply - worked it out in the end:
I had a database session open in a flask shell, which was preventing alembic from obtaining a lock and therefore running the update - finished the session and then the migration worked fine.

All 12 comments

What was your fix? I'm having the same issue!

I can't quite remember, but I reckon you could squash the migration by deleting it (since it's not committed) and redoing the migrate and upgrade steps again.

Thanks for the reply - worked it out in the end:
I had a database session open in a flask shell, which was preventing alembic from obtaining a lock and therefore running the update - finished the session and then the migration worked fine.

@authentik8 Had the same problem, thanks ! :+1:
P.S. I noticed that you found the fix after 2 days. Just wondering, did you let it be like that for two days ?

No problem, put up an explanation for exactly that reason!

In reply to your PS, no (I was out for one of the middle days anyway!) - I got as far as working out that if I killed and recreated my tmux session whenever it hung then I could get it to work. Obviously the issue was intermittent as I only had a flask shell open some of the time, but it worked "well enough" for a while.

Eventually I got tired of having to reconfigure all of my panes after every time it hung, so I decided to stop developing more stuff until I resolved it. Took me about 15 minutes of playing around with different things until I could isolate the cause (which then made sense, reading the SQLAlchemy docs on how sessions work).

Yep, this should have been obvious, ha!.
I had around 3 terminals open one for vim, one for the flask server and the other for the flask shell, still I didn't notice. palm to the face.

This is happening also because of thread with an infinite loop (intentionally for checking some kind of event),

@samakshjain Thank you for saying so. I use gnu screen as well, and that was the exact problem! Also facepalm

Just adding this as a potential cause:
For me, it was a docker container running in the background that was locking the database...

For those who want to run alembic upgrade when app is running, add the following code in your project's app file.

@app.teardown_appcontext
    def shutdown_session(exception=None):
        db.remove()

the variable db is the database session from your database setting file. so you should import it first.
you have to remove the session (return it to the pool) after each request.

When alembic runs, it uses flask's app context, and the currently running app is holding all connections, which means there are no available connections for alembic to use.

https://flask.palletsprojects.com/en/1.0.x/appcontext/

For me, it's because the type tinyint and bool are the same thing in mysql but alembic sees them differently, so every time I upgrade it stuck. See here for more detail.

Same issue - for me I had one of my tables misspelled such as in
__tablename__ = 'nlp_words_in_topics_t'
Check syntax, spelling errors

Was this page helpful?
0 / 5 - 0 ratings