Apply and Roll Back with upgrade
Move the schema forward and backward.
Apply and Roll Back with upgrade is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
From Script to Schema
You have a migration file, but the database has not changed yet. One command turns that script into real tables. 🚀
Run upgrade
flask db upgrade runs every pending migration and brings your database up to the latest version in order.
flask db upgradeIt Calls upgrade()
Behind the scenes Alembic executes the upgrade function inside each pending script, applying its create and alter operations.
def upgrade():
op.create_table("user", ...)The Version Gets Stamped
After a successful run, the new revision id is saved in the alembic_version table so Alembic remembers where you are.
Only Pending Ones Run
upgrade is smart: it skips migrations already applied and runs only the pending ones, so re-running it is safe.
Made a Mistake?
If a migration went wrong, you do not edit the database by hand. You roll back to a known good version instead.
Roll Back with downgrade
flask db downgrade reverses the last migration by running its downgrade function, undoing the most recent change.
flask db downgradeTarget a Specific Revision
Both commands accept a target. Pass a revision id to move directly to that exact point in your migration history.
flask db upgrade a1b2c3d4Check Where You Are
Run flask db current anytime to print the revision your database sits on right now. Great for a quick sanity check.
flask db currentRead the History
flask db history lists every migration in order, so you can see the full path from the first revision to the latest.
flask db historyApply in Production
On deploy you run upgrade as a release step, never migrate. Production simply replays the scripts your team already reviewed.
Quick Check
The last migration broke something. How do you undo just that one change?
Recap
You applied changes with flask db upgrade, reversed them with downgrade, and learned to check current and history. 🎉
Frequently asked questions
Is the “Apply and Roll Back with upgrade” lesson free?
Yes — the full text of “Apply and Roll Back with upgrade” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Apply and Roll Back with upgrade”?
Move the schema forward and backward. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Apply and Roll Back with upgrade” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why You Need Migrations, Not create_all
- Init and Autogenerate a Migration
- Apply and Roll Back with upgrade
- Review and Edit Generated Scripts