Search⌘ K
AI Features

Making Flask Migrations

Explore how to create and manage database migrations for a Flask-based microservice using the manager.py script. Learn to initialize, generate, and apply migrations inside a Docker container using Flask-Migrate commands to keep your MySQL database schema synchronized with your application models.

Creating a migration file

We must create a migration file to make migrations in our Core app. Let’s create a migration file in the backendservice2 folder named manager.py. Then, we can open the file and import the following modules:

Python
from core import core
from core import db
from flask_migrate import Migrate
from flask_migrate import MigrateCommand
from flask_script import Manager
  • Lines 1–2: We import our Flask application instance, core, and our MySQL database instance, db, from our core.py.

  • Lines 3–4: We import Migrate and the MigrateCommand from flask_migrate.

  • Line 5: We import the Manager from flask_script.

Now that we are done ...