Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Migrations Under The Hood
Search
Andrew Godwin
November 14, 2014
Programming
13k
6
Share
Migrations Under The Hood
A talk I gave at Django Under The Hood 2014.
Andrew Godwin
November 14, 2014
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
390
Django Through The Years
andrewgodwin
0
310
Writing Maintainable Software At Scale
andrewgodwin
0
520
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
420
Async, Python, and the Future
andrewgodwin
2
730
How To Break Django: With Async
andrewgodwin
1
800
Taking Django's ORM Async
andrewgodwin
0
810
The Long Road To Asynchrony
andrewgodwin
0
760
The Scientist & The Engineer
andrewgodwin
1
840
Other Decks in Programming
See All in Programming
開発とはなにか、Essenceカーネルで見えるもの
ukin0k0
0
210
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
310
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
160
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
4
750
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
810
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
3
1.1k
Claspは野良GASの夢をみるか
takter00
0
130
AI時代だからこそ「Bloc」を採用する価値があるのかもしれない
takuroabe
0
240
inferと仲良くなる10分間
ryokatsuse
1
260
密結合なバックエンドから TypeScript のコードを生成する
kemuridama
1
360
OCRを使ってゲームのアイテムをデータ化する
kishikawakatsumi
0
120
Agentic UI beyond Chats Architecture Patterns & Open Standards @ngMunich 05/2026
manfredsteyer
PRO
0
150
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Odyssey Design
rkendrick25
PRO
2
630
First, design no harm
axbom
PRO
2
1.2k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
330
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
23k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.1k
The SEO identity crisis: Don't let AI make you average
varn
0
470
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.3k
How GitHub (no longer) Works
holman
316
150k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
130
Transcript
Migrations UNDER THE HOOD Andrew Godwin Migrations
Andrew Godwin Author of South migrations library Hi, I'm Author
of 1.7 Django migrations Senior Software Engineer at Eventbrite Apparently at every Django conference
Migrations
Migrations
Other talks are available. This is a technical dive!
The Initial Plan Django Schema backend ORM Hooks South 2
Migration handling User interface
TheBRevisedBPlan Django SchemaBbackend ORMBHooks SouthB2 MigrationBhandling UserBinterface BackportBforB1.4B-B1.6
The Revised Revised Plan Django Schema backend ORM Hooks Migration
handling User interface
Logically Separate SchemaEditor Schema Migrations field.deconstruct() ModelOptions.apps Operations Loader /
Graph Executor Autodetector Optimiser State
Logically Separate SchemaEditor Schema Migrations field.deconstruct() ModelOptions.apps Operations Loader /
Graph Executor Autodetector Optimiser State
myapp/0001 myapp/0002 otherapp/0001 contenttypes/0001 appthree/0001 myapp/0003 otherapp/0002
(insert wavy memory effect!) Why do we need dependencies?
Makes all tables syncdb Sets all constraints Adds all ForeignKeys
Makes appA tables migrations Makes appB tables Adds FKs from
appB to appA Adds FKs from appA to appB
Makes appA tables migrations Makes appB tables Adds FKs from
appB to appA Adds FKs from appA to appB
The obvious stuff. Basic Dependencies
Or your database gets really mopey. FK target add before
FK added
In the general case; some DBs less fussy FK target
delete after FK deleted
Good luck doing it otherwise. Model create before field create
Also after field create Model delete after field alter
Stuff you occasionally forget Django has. Trickier Dependencies
And, of course, the reverse for deletion. M2M through model
before M2M
I'll let you figure out deletion reversals from now on.
MTI parent before MTI child
Index together, too Unique together after fields
Well done if you've actually used this feature. Order with
respect to after target
This happens when you turn proxies into real models. Same
delete before same create
Impossible dependencies
OH PLEASE NO TAKE IT AWAY Swappable models
Your migration dependencies myapp/0001 myapp/0002 otherapp/0001 auth/0001 contenttypes/0001
myapp/0001 myapp/0002 otherapp/0001 auth/0001 contenttypes/0001 thirdapp/0001 Your migration dependencies on
swappable models
what? Your migration dependencies on swappable models myapp/0001 myapp/0002 otherapp/0001
auth/0001 contenttypes/0001 thirdapp/0001 ???
what? Your migration dependencies on swappable models myapp/0001 myapp/0002 otherapp/0001
auth/0001 contenttypes/0001 thirdapp/0001 ??? argh
End of presentation. Click to exit.
They can't change at runtime, for swappable models or anything
else. Dependencies are static
(for a while, the answer was "careful function ordering") How
is it implemented?
no issues depend match shift operations restart
Using dependencies to define boundaries Chop operations into migrations
Optimisation
There's really quite a lot. Even more rules
create model + delete model = nothing create model +
create field = create model create field + alter field = create field alter field + delete field = delete field alter field + alter field = alter field
This time, with collapses rather than swaps Even more shuffling
can't reduce reduce possible check conflicts reduced, reset
Lets us be more verbose and clear with operations Applied
after autodetection
Makes your history a bit shorter Applied after squash
Loading & Graph
Builds graph of all basic migrations in memory Loads from
disk first
Marks applied state on each node Loads from db second
Only if all replaced nodes have same applied state Applies
squashes third
In-order list of all a node's ancestors in the tree
Graph returns "plans"
• Autodetector is slow • Optimizer is not great •
Graph state building is inefficient Room for improvement!
django/db/migrations/autodetector.py Autodetector django/db/migrations/optimizer.py Optimiser reduce()GfunctionGisGentryGforGlogic StartGatG_detect_changes()GandGfollowGcalls django/db/migrations/graph.py Graph django/db/migrations/loader.py
Thank you. Andrew Godwin @andrewgodwin