Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Flask-DB Helps You Migrate, Seed and Reset Your SQL Database

blog/cards/flask-db-helps-you-migrate-seed-and-reset-your-sql-database.jpg

For migrations it uses Alembic under the hood, you're also in full control over what gets seeded and you can reset your database at will.

Quick Jump: Demo Video

Flask-DB adds a new flask db command that lets you run these sub-commands:

init     Generate Alembic config files and seeds.py.
seed     Seed the database with your custom records.
reset    Drop, create and seed your database (careful in production).
migrate  Wrap the alembic CLI tool (defaults to running upgrade head).

It’s an evolution of a db command that I have been adding to my Flask apps and Build a SAAS App with Flask course since 2015. Now instead of adding that database command to every project’s source code, I improved it a bit and open sourced it.

To install it all you have to do is run pip3 install Flask-DB and you’re done. You don’t even need to initialize it. The commands will be ready immediately.

The video below shows how to install and use it in a project, along with cover why you may want to use this instead of running alembic commands directly. It also goes over how you can replace and migrate from Flask-Migrate.

By the way for the migrate command, Flask-DB still uses Alembic’s config files (with slight opinions) and it literally aliases all db migrate commands directly to alembic. This ensures maximum compatibility with Alembic and its CLI API.

Demo Video

Timestamps

  • 0:19 – It uses Alembic’s configs and commands for handling migrations
  • 1:06 – Flask-DB lets you reset and seed your main database and test database
  • 2:06 – Running pip3 install Flask-DB is all you need to do to install it
  • 3:25 – Exploring the new flask db command
  • 4:12 – Initializing Flask-DB (similar to alembic init but with more opinions)
  • 6:52 – Migrating from using Alembic directly or Flask-Migrate is painless
  • 7:31 – Resetting your database, along with optionally a separate test database
  • 10:04 – Seeding your database with any records that make sense for your app
  • 13:23 – You’ll probably be using db reset a lot in development on newer projects
  • 14:14 – Making your seeds idempotent to avoid errors when running it 2+ times
  • 15:10 – A few opinions and differences between db init vs alembic init
  • 19:14 – Creating a migration in our app using db migrate (which aliases alembic)
  • 22:58 – Running db migrate is a shortcut for db migrate upgrade head
  • 23:45 – Checking out our database to make sure the new column was added
  • 24:10 – Skimming the README file and generating help for db migrate and alembic
  • 25:42 – A detailed FAQ in the README for upgrading from Flask-Migrate
  • 26:12 – This extension was extracted out of building Flask apps for 5+ years
  • 26:48 – Customizing where the seeds.py file exists on disk
  • 28:18 – Recap

Are you going to be using this extension? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments