10

I have an issue on running the upgrade command with Flask-Migrate. Originally I was trying to modify three tables at once and it hung, I narrowed it down to a specific table (the other upgrades worked without issue). I don't see any locks on the database. I'm using Postgres and just working in a development environment at the moment. The migrate command seems to work fine and generates the upgrade method without any issues (see code)

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('equipment', 'criteria_notes')
    # ### end Alembic commands ###

Once I run the upgrade command I get stuck here:

>flask db upgrade
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 76bf72d8e0e4 -> 1723c01f0606, empty message

with a blinking cursor. When I exit out of the command window (I'm using windows) no changes have been applied.

I've tried

  • Restarting computer
  • Looking for locks or other sessions with pgAdmin
  • Modifying other tables, was able to add and drop columns
Sobigen
  • 2,038
  • 15
  • 23
  • How big is the table in which this column exists? – Miguel Grinberg Aug 16 '17 at 18:32
  • @Miguel It's tiny at the moment. 11 columns 4 rows. – Sobigen Aug 16 '17 at 19:49
  • What happens if you try to delete the column via SQL (i.e. `ALTER TABLE ...`)? Does that hang too? – Miguel Grinberg Aug 17 '17 at 16:14
  • @Miguel That works. I tried that yesterday afternoon as a workaround. I found all the SQL commands using `Flask db upgrade --sql` and updated my table and the alembic table. – Sobigen Aug 17 '17 at 16:48
  • Since this is still new, @Miguel I am still running into the same issue. My error originated from changing `icon = db.Column(db.String(12))` to `icon = db.Column(db.String)` in one my small tables (4 columns). Upgrading is stuck, deleting the built (but not committed) migrations and redoing does not help, downgrading 1 migration version lower is stuck Here is an image of pgAdmin, saying that I have to sessions open and 1 is waiting for the other to release its lock: http://imgur.com/RCJykPa – tsumnia Aug 25 '17 at 20:45
  • 1
    I'm also having this issue. Oddly it's only happening in my staging environment. It seems to work when i create a dump of the database and run it in my local development environment. – Sir Neuman Apr 12 '18 at 19:34
  • same issue using postgres. If i run the commends in postgres it also hangs – user249806 Apr 24 '18 at 22:00

5 Answers5

7

You likely have other processes connected to this DB.

This happened to me, I had celery, uwsgi, and a separate custom process all connected to the flask DB on a production server, and I was wonder why the upgrades were hanging.

Once I shut these processes down manually the upgrade script ran fine without hanging.

In my case I also tried rebooting before realizing this, and this did not help because these other processes were programmed to startup on boot.

David Simic
  • 2,061
  • 2
  • 19
  • 33
  • 1
    It's possible I looked and didn't find any locks at the time. Since I posted this I worked around it by doing the raw SQL and I also dropped that whole database at some point and re-created it in the dev environment and never had the issue return. – Sobigen May 22 '18 at 12:47
  • I had to shutdown my Flask server and then also disconnect and reconnect to my server to get it to work – Matt May 08 '23 at 03:08
2

I had this same issue with MySql due to locks and this command did the trick for me.

sudo service mysql restart
AllanM007
  • 55
  • 4
  • 6
1

I had the same issue, this one happened to me because of locks,verify it using

select * from pg_locks; 

There were many PIDs corresponding to each lock,I restarted the postgresql service then ran the upgrade command it worked

sudo systemctl restart postgresql
flask db upgrade
Pavan Varyani
  • 1,454
  • 1
  • 8
  • 15
0

In my case it was due to locks on the table.

select * from pg_locks; 

in postgres showed me what was happening.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
user249806
  • 299
  • 1
  • 3
  • 14
0

Had the same issue with heroku postgres. When the db is modified locally, the application starts to act wierd. What I do is I restart the dynos for the application

In cases where the application and the db are in different containers, restart the application.