My keynote from DjangoCon US 2014
's migrationsdigging into
View Slide
Andrew GodwinAuthor of South migrations libraryHi, I'mAuthor of 1.7 Django migrationsSenior Software Engineer at
south migrations for djangosouth.aeracode.orgDjangoCon 2008https://speakerdeck.com/andrewgodwin/south-migrations-for-django
Migrations:They're pretty good.
South0.10.20.30.40.50.60.70.81.0Aug 2008Aug 2008Sep 2008Jan 2009Apr 2009Jun 2009Mar 2010May 2013Jul 2014
The Early Years0.10.20.30.40.50.60.70.81.0Aug 2008Aug 2008Sep 2008Jan 2009Apr 2009Jun 2009Mar 2010May 2013Jul 2014
Approaching Stability0.10.20.30.40.50.60.70.81.0Aug 2008Aug 2008Sep 2008Jan 2009Apr 2009Jun 2009Mar 2010May 2013Jul 2014Well, in the short term,South 1.0 will be released.““June 2010
The Long Gap & Kickstarter0.10.20.30.40.50.60.70.81.0Aug 2008Aug 2008Sep 2008Jan 2009Apr 2009Jun 2009Mar 2010May 2013Jul 2014
Today0.10.20.30.40.50.60.70.81.0Aug 2008Aug 2008Sep 2008Jan 2009Apr 2009Jun 2009Mar 2010May 2013Jul 2014
For at least a year now, people havebeen suggesting to me that Southshould be in Django core.““June 2010
The Initial PlanDjangoSchema backendORM HooksSouth 2Migration handlingUser interface
The Revised PlanDjangoSchema backendORM HooksSouth 2Migration handlingUser interfaceBackport for 1.4 - 1.6
The Revised Revised PlanDjangoSchema backendORM HooksMigration handlingUser interface
Moving South into Djangoinstead, "Adding migrations to Django"
Logically SeparateSchemaEditorSchema Migrationsfield.deconstruct()ModelOptions.appsOperationsLoader / GraphExecutorAutodetectorOptimiserState
Operations & State
Operations are a declarativerepresentation of model changesState is an in-memory representationof entire project state
State StateOperationAddModelNo Models 1 Model
State StateOperationStateOperationStateOperationStateOperationMigration 1 Migration 2
AddModel DeleteModel RenameModelAddField DeleteField AlterModelOptionsRenameField AlterModelTable AlterFieldAlterUniqueTogether AlterIndexTogetherRunSQL RunPython
SchemaEditor
connection.schema_editor()Abstraction over database DDLTakes 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 anddependencies as directed graphLoop detection, leaf/root selection
myapp/0001myapp/0002 otherapp/0001contenttypes/0001appthree/0001myapp/0003 otherapp/0002
The Autodetector
Compares two States and outputsa set of operations to performOperations have own dependenciesand resolver
Model A Model A w/FK, Model BdiffAddModel(B)AddField(A, "b", FK(B))dependency sortAddModel(B) AddField(A, "b", FK(B))BEFORE AFTER
The Optimiser
Takes one set of Operations andoutputs another with the same effectSteps over pairs and checks if theycombine and what they pass over
can'treducereducepossiblecheckconflictsreduced,reset
A New FormatMore conciseDeclarativeIntrospectable
Migration actionsFrozen ORM
makemigrationsfield.deconstruct()Loader / GraphAutodetector OptimiserStateWriter12State354
migrateSchemaEditorModelOptions.appsOperationsLoader / Graph ExecutorState1 2Recorder3
In-memory runningCreates models from migration setsAutodetector diffs created from on-diskUsed to feed SchemaEditor / ORM
State StateOperationStateOperationStateOperationStateOperationMigration 1 Migration 2ModelsModelsRender Render
But what went wrong?
Swappable Models
Your migration dependenciesmyapp/0001myapp/0002otherapp/0001auth/0001contenttypes/0001
myapp/0001myapp/0002otherapp/0001auth/0001contenttypes/0001thirdapp/0001Your migration dependencieson swappable models
what?Your migration dependencieson swappable modelsmyapp/0001myapp/0002otherapp/0001auth/0001contenttypes/0001thirdapp/0001???
Unmigrated Apps
Test persistence
Test persistenceon MySQL
Random Meta optionsorder_with_respect_to? Really?
Proxy Models
But we're there!Django 1.7, out now!
Thanks!Andrew Godwin[email protected]