0
0
Flaskframework~10 mins

Database migration in deployment in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Database migration in deployment
Write migration script
Generate migration files
Review migration files
Apply migration to local DB
Commit migration files
Deploy to server
Run migration command on server
Database schema updated
App uses new schema
This flow shows how migration scripts are created, tested locally, committed, deployed, and applied to update the database schema safely.
Execution Sample
Flask
from flask_migrate import Migrate, upgrade

migrate = Migrate(app, db)

# On server:
upgrade()
This code initializes migration support and runs the upgrade command to apply migrations on deployment.
Execution Table
StepActionCommand/CodeResultNotes
1Write migration scriptflask db migrate -m 'Add users table'Migration file createdDefines changes to DB schema
2Review migration fileOpen migration scriptConfirmed changes correctCheck for errors
3Apply migration locallyflask db upgradeLocal DB schema updatedTest migration works
4Commit migration filesgit add migrations/ && git commit -m 'Add migration files'Files saved in repoReady for deployment
5Deploy app to servergit push and deployNew code and migrations on serverServer ready for migration
6Run migration on serverflask db upgradeServer DB schema updatedApply changes safely
7App runs with new schemaStart appApp uses updated DBDeployment complete
💡 All migration steps completed, database schema is up to date on server
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 6Final
Migration filesNoneCreatedCreatedCreatedCreated
Local DB schemaOldOldUpdatedUpdatedUpdated
Server DB schemaOldOldOldUpdatedUpdated
Key Moments - 3 Insights
Why do we run 'flask db migrate' before 'flask db upgrade'?
Because 'flask db migrate' creates the migration script describing changes, and 'flask db upgrade' applies those changes to the database. See execution_table steps 1 and 3.
Why test migration locally before deploying?
To ensure the migration script works correctly and does not break the database. This is shown in execution_table step 3.
What happens if we forget to run 'flask db upgrade' on the server?
The server database schema stays old, causing errors when the app expects new tables or columns. See execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what command creates the migration script?
Aflask db upgrade
Bflask db migrate -m 'Add users table'
Cgit commit migrations/
DStart app
💡 Hint
Check Step 1 in the execution_table where migration file is created
At which step is the local database schema updated?
AStep 6
BStep 1
CStep 3
DStep 7
💡 Hint
Look at Step 3 in execution_table where local DB schema changes
If we skip committing migration files, what happens on deployment?
AMigration files missing on server, upgrade fails
BLocal DB schema updates automatically
CServer applies migrations successfully
DApp runs without database
💡 Hint
See Step 4 and 5 in execution_table about committing and deploying migration files
Concept Snapshot
Database migration in Flask:
1. Use 'flask db migrate' to create migration scripts.
2. Review and test locally with 'flask db upgrade'.
3. Commit migration files to version control.
4. Deploy code and run 'flask db upgrade' on server.
5. This updates the database schema safely without data loss.
Full Transcript
Database migration in deployment with Flask involves creating migration scripts that describe changes to the database schema. First, you run 'flask db migrate' to generate these scripts. Then, you review and apply them locally using 'flask db upgrade' to test. After confirming, you commit the migration files to your code repository. When deploying, you push the code and migration files to the server. On the server, running 'flask db upgrade' applies the schema changes to the production database. This process ensures the database structure matches the app's expectations without losing data or causing errors.