Fixing "Already-Fixed" Flask-Migrate Error, And Joining HNG11

How Chance Gave Me The Obvious Solution To A Long-Standing Flask-Migrate Error

Fixing "Already-Fixed" Flask-Migrate Error, And Joining HNG11

Photo by Ariel on Unsplash

My first web project, Tradepally.com, was initially built and deployed with Python 3.7 and Flask 1.1 in 2020. While everything worked fine through the years, I knew, through research and new learning, that I'd need to upgrade the Python, Flask, and other dependency versions at some point to ensure better security and compatibility with newer technologies.

The thought of doing these upgrades was my biggest nightmare at the time because I felt like I wouldn't be able to handle it and what it might result in. All the same, I finally did the upgrades in 2023, and true to my fears, a long list of ugly issues, errors, and bugs sprang up. I'll write comprehensively about the whole upgrade process in a later post, but for now, let me talk about the one with Flask-Migrate—an extension tool that handles SQLAlchemy database migrations for Flask applications using Alembic.

I didn't know [and still don't know as of the time of writing this] exactly how to properly plan a web project development or major stack upgrades, which is something I would like to improve on with my participation in the current HNG Internship, HNG11. All I knew was that I needed to do the upgrades at the time, so I started. One important thing I did, though, was that I saved a backup repo of the working codebase at the time, in case things went completely south.

I started by downloading a newer Python version and creating a new virtual environment for the project clone with it. This alone introduced a number of errors, especially with the default Python datetime, collections, dict modules. After overcoming those, I started updating Flask and other dependencies to their latest versions, and all hell broke loose. Among the myriad of issues that arose in this second phase was the one that caused Flask-Migrate to stop working in the VSCode Terminal [which was my handy and go-to command-line interface], throwing the error shown below:

Traceback (most recent call last):
File "manage.py", line 4, in
from flask_migrate import Migrate, MigrateCommand
ImportError: cannot import name 'MigrateCommand' from 'flask_migrate'

This issue, caused by Flask-Migrate dropping support for Flask-Script [as seen in the screenshot below], though not big and severe in itself, forced me to halt all supposed development and production database migrations for Tradepally.com for months, even after all other issues had been fixed.

What made it worse for me was that I could no longer use the downgrade option suggested by Flask-Migrate's creator, Miguel Grinberg, as seen in the screenshot, because I had upgraded every other dependency in the virtual environment to their latest versions, which automatically rendered Flask-Migrate 2.7.0 incompatible. So, I had to choose the lesser evil: deploying with the latest version, knowing full well that I would not be able to add or remove a column from any model in the database, let alone add or remove tables until a solution is found.

For the longest time, I viewed this problem as an issue with my own code. After nearly giving up on any further database migrations using Flask-Migrate due to "this error" and starting to search for an alternative, a sort of miracle happened. By chance, I realized that the solution had been there from the start. All I needed to do to continue database migrations was to use Flask-CLI in the system's default Command Prompt or PowerShell instead of the VSCode Terminal. Simple!

The whole process taught me to always look at problems from different perspectives early in their lifetime, instead of being fixated on one or a few well-known possibilities. Another noteworthy point is that this "miracle solution" came while I was working on a different project, Safenatty.com - my first security-focused web project as a Google Cybersecurity Professional Certificate student, where I want to focus on User Education. It was during one coding session for Safenatty, which involved the Command Prompt, that my mind suggested I should try the Tradepally migration script as well - and boom, it worked like magic. You needed to see how happy it made me. You can only imagine!

I'm glad to have talked a little about this daunting but fulfilling task I embarked on: migrating a live project from one major Python 3 version to another (3 steps higher) at this time, thanks to the HNG Internship, HNG11, which made it the Stage Zero Task for participants in the Backend Development Track.

I'm Chimeziri Obioha, a Python backend programmer with four years of independent experience. I'm currently taking the Google Cybersecurity Professional Certificate course while maintaining Tradepally.com and trying to figure out how to push it, together with Safenatty.com and Treasurebill.com. I resume daily at my informal financial services business outlet in Ebute-Metta, Lagos, Nigeria, and hope to gain valuable professional experience with HNG Premium in HNG11. I like to call myself the #tradersTechie because I'm a spare parts trader turned techie.