south talk
DESCRIPTION
TRANSCRIPT
Schema Migrations for Django
Friday, September 27, 13
WHAT WE’LL COVER
How to use South
• South and git
• Data migrations
• Model inheritance
• Wacky Stuff
Friday, September 27, 13
WHY SOUTH?
Generally:
• Migrations allow you to synchronize your database with your code.
• Important for:
• multiple developers• rolling back• migrating data • deploying
Specifically:
• South is the standard
• South is being rolled into Django in 1.7
• South is awesome & fun!
Friday, September 27, 13
SETTING UPsouth.readthedocs.org
Friday, September 27, 13
SETTING UP
OR
pip install south
1. Install
Friday, September 27, 13
SETTING UP2. Installed apps
Friday, September 27, 13
SETTING UP3. syncdb
Friday, September 27, 13
SETTING UP(Create an app & models)
Friday, September 27, 13
SETTING UP
Existing apps:convert_to_south appname
New apps:schemamigration appname --initial
4. Setup each app
Friday, September 27, 13
HOW IT WORKS
• Migrations folder
• South migrations history table
• Models.py
• Database schema & data
Creates:
Reads from:
Alters:
Friday, September 27, 13
HOW IT WORKS
Friday, September 27, 13
HOW IT WORKS
Friday, September 27, 13
HOW IT WORKS
Friday, September 27, 13
NORMAL WORKFLOWupdate modelmanage.py schemamigration appname --automanage.py migrate (appname)
eureka! a better model...manage.py migrate backwards (zero)
update modelmanage.py migrate --list
delete files***redo the schemamigrationmanage.py schemamigration appname --auto
{
Friday, September 27, 13
NORMAL WORKFLOWupdate modelmanage.py schemamigration appname --automanage.py migrate (appname)
eureka! a better model...manage.py migrate backwards (zero)
update modelmanage.py migrate --list
delete files***redo the schemamigrationmanage.py schemamigration appname --auto
{
DANGER!
Friday, September 27, 13
AVOIDING GIT TROUBLES
Friday, September 27, 13
AVOIDING GIT TROUBLES
• update Authors model• schemamigration: 0002
• eureka!• migrates back to 0001
• deletes migration 0002• creates new 0002
• writes code• pulls code• migrates to 0002
• writes code• pulls code• ghost migration
Dev #1: Author Dev #2: Blog
Friday, September 27, 13
AVOIDING GIT TROUBLES
Resolving:NEVER
delete a migration that has others have access to
Friday, September 27, 13
AVOIDING GIT TROUBLES
• update Authors model• schemamigration: 0002
• updates Blog model• schemamigration: 0003
• pushes
Dev #1: Author Dev #2: Blog
• update Blog model• schemamigration: 0002
• updates Blog model• schemamigration: 0003
• pulls
Friday, September 27, 13
AVOIDING GIT TROUBLES
• update Authors model• schemamigration: 0002
• updates Blog model• schemamigration: 0003
• pushes
Dev #1: Author Dev #2: Blog
• update Blog model• schemamigration: 0002
• updates Blog model• schemamigration: 0003
• pulls
= CONFLICT
Friday, September 27, 13
AVOIDING GIT TROUBLESBETTER:
git feng shui
models, migrations
other code
other code
previous commit
FEATURE DEVELOPMENT
models, migrations
Friday, September 27, 13
DATA MIGRATIONSMove date_of_birth from Authors to UserProfile
Friday, September 27, 13
DATA MIGRATIONSSANDWICH
schemamigration
datamigration
schemamigration
Friday, September 27, 13
DATA MIGRATIONSBOTTOM SLICE:schemamigration
Friday, September 27, 13
DATA MIGRATIONSMIDDLE:
datamigration
Friday, September 27, 13
DATA MIGRATIONSMIDDLE:
datamigration
Friday, September 27, 13
DATA MIGRATIONSMIDDLE:
datamigration
Friday, September 27, 13
DATA MIGRATIONSTOP SLICE:
schemamigration
Remove date_of_birth from Author
Friday, September 27, 13
DATA MIGRATIONSTOP SLICE:
schemamigration
Remove date_of_birth from Author
Remember: Data migration needs all information available.
Friday, September 27, 13
MODEL INHERITANCE
Friday, September 27, 13
MODEL INHERITANCE
Friday, September 27, 13
MODEL INHERITANCE
Friday, September 27, 13
MODEL INHERITANCE
Friday, September 27, 13
WACKY STUFF
Friday, September 27, 13
TESTING
Friday, September 27, 13