0

I am using flask-migrate to handle migrations. After adding a new column to the db and running the migration, what is the best approach to set the value of the new column for old users?

Right now I am temporarily adding this snippet for a url:

def test():
    res = ""
    users = User.query.all()
    for u in users:
        res += f"{u.username} {u.is_blocked}<br>"
        u.is_blocked = False
    db.session.commit()
    return res

I think this is far from best practices. Should I create a small script that does the same work by directly connecting to the sql db, without using flask? Can anyone point me in the right direction?

davidism
  • 121,510
  • 29
  • 395
  • 339
ElLorans
  • 56
  • 5
  • you're using SQLAlchemy separate from Flask, right? not the flask-sqlalchemy module? if so, ya just the code you have in your function here would be fine to put in a stand-alone script. it almost seems to me like a test, so if you have tests you could add it to that file. and have some assertion/check that all records have that column populated. – mechanical_meat Jan 22 '22 at 18:03
  • 1
    Does [this](https://stackoverflow.com/questions/24612395/how-do-i-execute-inserts-and-updates-in-an-alembic-upgrade-script/24623979) answer your question? – snakecharmerb Jan 22 '22 at 18:47
  • Thanks @snakecharmerb, your [link](https://stackoverflow.com/questions/24612395/how-do-i-execute-inserts-and-updates-in-an-alembic-upgrade-script/24623979) solved my issue. – ElLorans Jan 22 '22 at 19:39

0 Answers0