0
0
Djangoframework~10 mins

Database migration in production in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Database migration in production
Write migration files
Test migrations locally
Backup production database
Put app in maintenance mode
Run migration commands
Verify migration success
Bring app back online
This flow shows the safe steps to update a production database schema using Django migrations.
Execution Sample
Django
python manage.py makemigrations
python manage.py migrate --noinput
# Backup DB
# Maintenance mode on
# Run migrate
# Maintenance mode off
This code sequence creates migration files, applies them without prompts, and manages production safety steps.
Execution Table
StepActionCommand/OperationResultNotes
1Create migration filespython manage.py makemigrationsNew migration files createdDetects model changes
2Test migrations locallypython manage.py migrateDatabase updated locallyEnsures no errors
3Backup production DBpg_dump or equivalentBackup file createdSafety step before changes
4Enable maintenance modeCustom command or configApp stops serving requestsPrevents user changes during migration
5Run migrations in productionpython manage.py migrate --noinputDatabase schema updatedNo prompts to avoid hangs
6Verify migration successCheck logs and DB schemaNo errors foundConfirms migration applied
7Disable maintenance modeCustom command or configApp resumes normal operationUsers can access app again
8ExitAll steps completedProduction DB updated safelyProcess finished
💡 All migration steps completed successfully, production database schema updated without downtime.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
Migration filesNoneCreatedCreatedCreatedCreated
Local DB schemaOldUpdatedUpdatedUpdatedUpdated
Production DB backupNoneNoneCreatedCreatedCreated
Production DB schemaOldOldOldUpdatedUpdated
App statusOnlineOnlineOnlineMaintenance modeOnline
Key Moments - 3 Insights
Why do we backup the production database before running migrations?
Backing up ensures you can restore data if migration causes errors or data loss, as shown in step 3 of the execution_table.
Why use --noinput when running migrations in production?
The --noinput flag prevents the migration command from waiting for user input, avoiding hangs during automated deployment (step 5).
What is the purpose of maintenance mode during migration?
Maintenance mode stops users from making changes during migration, preventing conflicts or partial updates (step 4 and 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the app status after step 5?
AOnline
BMaintenance mode
COffline
DUnknown
💡 Hint
Check the 'App status' variable in variable_tracker after step 5.
At which step is the production database backup created?
AStep 3
BStep 1
CStep 5
DStep 7
💡 Hint
Look at the 'Production DB backup' variable in variable_tracker and the execution_table step descriptions.
If we skip enabling maintenance mode, which step would be missing from the execution_table?
AStep 6
BStep 2
CStep 4
DStep 8
💡 Hint
Maintenance mode is enabled at step 4 according to the execution_table.
Concept Snapshot
Django production migration steps:
1. Create migration files with makemigrations.
2. Test migrations locally.
3. Backup production DB.
4. Enable maintenance mode.
5. Run migrate with --noinput.
6. Verify success and disable maintenance mode.
Always backup and prevent user changes during migration.
Full Transcript
This visual execution shows how to safely update a Django production database schema. First, migration files are created and tested locally. Then, a backup of the production database is made to protect data. The app is put into maintenance mode to stop user activity. Next, migrations run with no input to avoid hangs. After verifying success, maintenance mode is turned off and the app resumes normal operation. Variables like migration files, database schema, and app status change step-by-step to ensure a smooth update.