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
Digging Into Django's Migrations
Search
Andrew Godwin
September 03, 2014
Programming
3
550
Digging Into Django's Migrations
My keynote from DjangoCon US 2014
Andrew Godwin
September 03, 2014
Tweet
Share
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
200
Django Through The Years
andrewgodwin
0
84
Writing Maintainable Software At Scale
andrewgodwin
0
320
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
240
Async, Python, and the Future
andrewgodwin
2
540
How To Break Django: With Async
andrewgodwin
1
570
Taking Django's ORM Async
andrewgodwin
0
580
The Long Road To Asynchrony
andrewgodwin
0
510
The Scientist & The Engineer
andrewgodwin
1
570
Other Decks in Programming
See All in Programming
ADRを一年運用してみた/adr_after_a_year
hanhan1978
7
2.3k
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
250
ONE WEDGE_company_guide
1wedge_one
0
390
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
170
エンターテイメント業界で利用されるAWS
demuyan
0
200
ログラスを支える設計標準について / loglass-design-standards
urmot
10
2.1k
スキーマ駆動開発による品質とスピードの両立 - 私達は何故、スキーマを書くのか
kentaroutakeda
0
130
チーム力を高めるスクラム実践法:カンバン公開と課題攻略について - ニフティのスクラムトーク Vol. 2 - NIFTY Tech Talk #18
niftycorp
PRO
1
110
코틀린으로 멀티플랫폼 만들기
pangmoo
0
120
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
520
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
0
100
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
150
Featured
See All Featured
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
Large-scale JavaScript Application Architecture
addyosmani
503
110k
Statistics for Hackers
jakevdp
789
220k
The Cost Of JavaScript in 2023
addyosmani
14
3.8k
GraphQLとの向き合い方2022年版
quramy
31
12k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
321
20k
Embracing the Ebb and Flow
colly
79
4.1k
Building Flexible Design Systems
yeseniaperezcruz
318
37k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
18
1.7k
Creatively Recalculating Your Daily Design Routine
revolveconf
209
11k
Building Applications with DynamoDB
mza
88
5.6k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
226
16k
Transcript
's migrations digging into
Andrew Godwin Author of South migrations library Hi, I'm Author
of 1.7 Django migrations Senior Software Engineer at
south migrations for django south.aeracode.org DjangoCon 2008 https://speakerdeck.com/andrewgodwin/south-migrations-for-django
Migrations: They're pretty good.
South 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 1.0
Aug 2008 Aug 2008 Sep 2008 Jan 2009 Apr 2009 Jun 2009 Mar 2010 May 2013 Jul 2014
The Early Years 0.1 0.2 0.3 0.4 0.5 0.6 0.7
0.8 1.0 Aug 2008 Aug 2008 Sep 2008 Jan 2009 Apr 2009 Jun 2009 Mar 2010 May 2013 Jul 2014
Approaching Stability 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
1.0 Aug 2008 Aug 2008 Sep 2008 Jan 2009 Apr 2009 Jun 2009 Mar 2010 May 2013 Jul 2014 Well, in the short term, South 1.0 will be released. “ “ June 2010
The Long Gap & Kickstarter 0.1 0.2 0.3 0.4 0.5
0.6 0.7 0.8 1.0 Aug 2008 Aug 2008 Sep 2008 Jan 2009 Apr 2009 Jun 2009 Mar 2010 May 2013 Jul 2014
Today 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 1.0
Aug 2008 Aug 2008 Sep 2008 Jan 2009 Apr 2009 Jun 2009 Mar 2010 May 2013 Jul 2014
For at least a year now, people have been suggesting
to me that South should be in Django core. “ “ June 2010
The Initial Plan Django Schema backend ORM Hooks South 2
Migration handling User interface
The Revised Plan Django Schema backend ORM Hooks South 2
Migration handling User interface Backport for 1.4 - 1.6
The Revised Revised Plan Django Schema backend ORM Hooks Migration
handling User interface
Moving South into Django instead, "Adding migrations to Django"
Logically Separate SchemaEditor Schema Migrations field.deconstruct() ModelOptions.apps Operations Loader /
Graph Executor Autodetector Optimiser State
Operations & State
Operations are a declarative representation of model changes State is
an in-memory representation of entire project state
State State Operation AddModel No Models 1 Model
State State Operation State Operation State Operation State Operation Migration
1 Migration 2
AddModel DeleteModel RenameModel AddField DeleteField AlterModelOptions RenameField AlterModelTable AlterField AlterUniqueTogether
AlterIndexTogether RunSQL RunPython
SchemaEditor
connection.schema_editor() Abstraction over database DDL Takes Django models and fields
with connection.schema_editor() as ed: ed.create_model(Author) ed.add_field( Book, "author", models.ForeignKey(Author), )
Field Deconstruction
Every field has deconstruct() Returns arguments passed to __init__ to
duplicate itself
>>> field = CharField(max_length=255) >>> field.deconstruct() ( None, 'django.db.models.CharField', [],
{'max_length': 255} )
The Graph
Represents migrations and dependencies as directed graph Loop detection, leaf/root
selection
myapp/0001 myapp/0002 otherapp/0001 contenttypes/0001 appthree/0001 myapp/0003 otherapp/0002
The Autodetector
Compares two States and outputs a set of operations to
perform Operations have own dependencies and resolver
Model A Model A w/FK, Model B diff AddModel(B) AddField(A,
"b", FK(B)) dependency sort AddModel(B) AddField(A, "b", FK(B)) BEFORE AFTER
The Optimiser
Takes one set of Operations and outputs another with the
same effect Steps over pairs and checks if they combine and what they pass over
can't reduce reduce possible check conflicts reduced, reset
A New Format More concise Declarative Introspectable
Migration actions Frozen ORM
None
makemigrations field.deconstruct() Loader / Graph Autodetector Optimiser State Writer 1
2 State 3 5 4
migrate SchemaEditor ModelOptions.apps Operations Loader / Graph Executor State 1
2 Recorder 3
In-memory running Creates models from migration sets Autodetector diffs created
from on-disk Used to feed SchemaEditor / ORM
State State Operation State Operation State Operation State Operation Migration
1 Migration 2 Models Models Render Render
But what went wrong?
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 ???
Unmigrated Apps
Test persistence
Test persistence on MySQL
Random Meta options order_with_respect_to? Really?
Proxy Models
But we're there! Django 1.7, out now!
Thanks! Andrew Godwin
[email protected]