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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Andrew Godwin
November 14, 2014
Programming
6
12k
Migrations Under The Hood
A talk I gave at Django Under The Hood 2014.
Andrew Godwin
November 14, 2014
Tweet
Share
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
370
Django Through The Years
andrewgodwin
0
290
Writing Maintainable Software At Scale
andrewgodwin
0
500
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
390
Async, Python, and the Future
andrewgodwin
2
720
How To Break Django: With Async
andrewgodwin
1
780
Taking Django's ORM Async
andrewgodwin
0
770
The Long Road To Asynchrony
andrewgodwin
0
740
The Scientist & The Engineer
andrewgodwin
1
810
Other Decks in Programming
See All in Programming
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
1.1k
AI巻き込み型コードレビューのススメ
nealle
2
2.3k
AIに仕事を丸投げしたら、本当に楽になれるのか
dip_tech
PRO
0
160
The Ralph Wiggum Loop: First Principles of Autonomous Development
sembayui
0
3.7k
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
2k
「やめとこ」がなくなった — 1月にZennを始めて22本書いた AI共創開発のリアル
atani14
0
280
Go 1.26でのsliceのメモリアロケーション最適化 / Go 1.26 リリースパーティ #go126party
mazrean
1
230
文字コードの話
qnighy
41
15k
個人開発は儲からない - それでも開発開始1ヶ月で300万円売り上げた方法
taishiyade
0
110
CSC307 Lecture 14
javiergs
PRO
0
430
iOSアプリでフロントエンドと仲良くする
ryunakayama
0
120
CSC307 Lecture 08
javiergs
PRO
0
690
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
300
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
117
110k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
190
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
We Are The Robots
honzajavorek
0
180
Writing Fast Ruby
sferik
630
62k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.2k
Designing for Performance
lara
611
70k
Deep Space Network (abreviated)
tonyrice
0
77
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
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