What if one small database change could break your whole live website?
Why Database migration in deployment in Flask? - Purpose & Use Cases
Imagine you have a website with a database, and you want to add a new feature that needs extra columns or tables. You try to change the database by hand on the live server while the site is running.
Manually changing the database is risky and slow. You might forget steps, cause errors, or break the site for users. It's hard to keep track of what changed and to fix problems quickly.
Database migration tools automate these changes safely. They keep a history of changes, apply updates step-by-step, and let you update the database structure without downtime or mistakes.
ALTER TABLE users ADD COLUMN age INT;
flask db migrate -m 'Add age column'
flask db upgradeIt lets developers update databases smoothly and confidently as the app grows and changes.
A social media app adds a new feature to store user birthdays. Using migrations, the team updates the database without stopping the site or losing data.
Manual database changes are risky and error-prone.
Migrations automate and track database updates safely.
This keeps apps running smoothly during deployment.
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
