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

alembic stuck on database upgrade #41

Closed
mattgathu opened this issue Jan 26, 2015 · 12 comments
Closed

alembic stuck on database upgrade #41

mattgathu opened this issue Jan 26, 2015 · 12 comments

Comments

@mattgathu
Copy link

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

@jwindridge
Copy link

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

@mattgathu
Copy link
Author

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.

@jwindridge
Copy link

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.

@samakshjain
Copy link

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

@jwindridge
Copy link

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

@samakshjain
Copy link

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.

@rnm-patel
Copy link

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

@Sparrow1029
Copy link

Sparrow1029 commented Jun 3, 2018

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

@reinoldus
Copy link

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

@kangju1
Copy link

kangju1 commented Aug 19, 2019

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/

@jabbawockeez
Copy link

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.

@edwardmoradian
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants