Database migration helps you update your app's database structure safely when you change your code. It keeps your data and app working well together.
Database migration in deployment in Flask
flask db init
flask db migrate -m "message"
flask db upgradeflask db init creates migration setup files once per project.
flask db migrate creates a migration script based on model changes.
flask db init
flask db migrate -m "Add user email column"flask db upgrade
This Flask app uses Flask-Migrate to handle database changes. Initially, the User model has only a username. Later, we add an email column. We create migration scripts and apply them to update the database without losing data.
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) migrate = Migrate(app, db) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True, nullable=False) # After adding this new column, run: # flask db migrate -m "Add email to User" # flask db upgrade # Updated User model with new email column class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=True) if __name__ == '__main__': app.run(debug=True)
Always back up your database before running migrations in production.
Use descriptive messages in flask db migrate -m to track changes clearly.
Test migrations in a development environment before applying them live.
Database migration updates your database structure safely as your app changes.
Use Flask-Migrate commands: init, migrate, and upgrade.
This keeps your data safe and your app working smoothly during updates.
Practice
Solution
Step 1: Understand database migration purpose
Database migration updates the database schema to match app changes without losing data.Step 2: Compare options
Only To safely update the database structure as the app changes describes safe schema updates; others describe unrelated or harmful actions.Final Answer:
To safely update the database structure as the app changes -> Option CQuick Check:
Migration = safe schema update [OK]
- Thinking migration deletes data
- Confusing migration with backup
- Assuming migration speeds up app
Solution
Step 1: Identify command for starting migrations
Theflask db initcommand creates the migration folder and scripts for the first time.Step 2: Check other commands
migratecreates migration files,upgradeapplies them,createis invalid.Final Answer:
flask db init -> Option DQuick Check:
Init = start migration scripts [OK]
- Using migrate before init
- Confusing upgrade with init
- Typing create instead of init
flask db initflask db migrate -m "Add user table"flask db upgradeWhat happens after the last command?
Solution
Step 1: Understand each command effect
initsets up migration scripts,migratecreates migration files,upgradeapplies changes to the database.Step 2: Identify final effect
Afterupgrade, the database schema updates to include new tables or changes.Final Answer:
The database schema updates with the new user table -> Option AQuick Check:
Upgrade = apply schema changes [OK]
- Thinking upgrade deletes migration files
- Assuming app restarts automatically
- Ignoring migration message
flask db migrate but see no new migration file created. What is the likely cause?Solution
Step 1: Understand migrate command behavior
flask db migratecreates migration files only if there are schema changes detected.Step 2: Analyze no new file case
If no migration file appears, it means no schema changes were found, so no new migration is needed.Final Answer:
Your database is already up to date with no changes -> Option BQuick Check:
No changes = no migration file [OK]
- Skipping init causes errors but not no file
- Confusing upgrade with migrate
- Thinking app restart affects migration files
Solution
Step 1: Understand correct migration order
First, create migration files withflask db migratedescribing changes, then apply them withflask db upgrade.Step 2: Check options for correct sequence
flask db migrate -m "Add column" -> flask db upgrade follows this order; others reverse or misuse commands.Final Answer:
flask db migrate -m "Add column" -> flask db upgrade -> Option AQuick Check:
Migrate then upgrade = safe update [OK]
- Running upgrade before migrate
- Running init after migrate
- Skipping migrate step
Ready to prove it?
Pass the test and this pattern moves unknown
