Slide 1

Slide 1 text

Models & Migrations & Models & Migrations & Schemas – oh my! Schemas – oh my! Andrew Godwin Andrew Godwin @andrewgodwin @andrewgodwin flickr.com/anders_young flickr.com/anders_young

Slide 2

Slide 2 text

· Django core dev · Django core dev · South · South · Lanyrd · Lanyrd

Slide 3

Slide 3 text

· The Past · The Past django-evolution, south 0.1 django-evolution, south 0.1 · The Present · The Present south 0.7 south 0.7 · The Future · The Future django 1.6? south 2.0? django 1.6? south 2.0?

Slide 4

Slide 4 text

· The Past · The Past django-evolution, south 0.1 django-evolution, south 0.1 · The Present · The Present south 0.7 south 0.7 · The Future · The Future django 1.6? south 2.0? django 1.6? south 2.0?

Slide 5

Slide 5 text

Databases hate schema changes Databases hate schema changes “ “All that optimising gone to waste...” All that optimising gone to waste...” flickr.com/96dpi flickr.com/96dpi

Slide 6

Slide 6 text

· Locks whole tables · Locks whole tables · Hammers disk I/O · Hammers disk I/O · Causes inconsistent results · Causes inconsistent results

Slide 7

Slide 7 text

There's a code/schema split There's a code/schema split Your database is going to ignore git Your database is going to ignore git flickr.com/nataliejohnson flickr.com/nataliejohnson

Slide 8

Slide 8 text

· Extra fields are fine · Extra fields are fine · Missing fields are not · Missing fields are not · Painful/slow to sync · Painful/slow to sync

Slide 9

Slide 9 text

django-evolution django-evolution dmigrations dmigrations

Slide 10

Slide 10 text

South 0.1 South 0.1

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

· South 0.2 · South 0.2 mysql support mysql support

Slide 14

Slide 14 text

· South 0.3 · South 0.3 dependencies dependencies

Slide 15

Slide 15 text

· South 0.4 · South 0.4 altering columns altering columns SQLite SQLite

Slide 16

Slide 16 text

· South 0.5 · South 0.5 ORM freezing ORM freezing Automatic change detection Automatic change detection

Slide 17

Slide 17 text

· South 0.6 · South 0.6 Field introspection Field introspection Dependency solving speed Dependency solving speed

Slide 18

Slide 18 text

· South 0.7 · South 0.7 data/schema split data/schema split missing defaults for NOT NULL missing defaults for NOT NULL multidb multidb custom fields ignored custom fields ignored

Slide 19

Slide 19 text

· The Past · The Past django-evolution, south 0.1 django-evolution, south 0.1 · The Present · The Present south 0.7 south 0.7 · The Future · The Future django 1.6? south 2.0? django 1.6? south 2.0?

Slide 20

Slide 20 text

· Mostly stable · Mostly stable Core API hasn't changed for 2 years Core API hasn't changed for 2 years

Slide 21

Slide 21 text

· No rebase/collapse · No rebase/collapse Needs a big change to migration tracking Needs a big change to migration tracking

Slide 22

Slide 22 text

· Opaque migrations · Opaque migrations Impossible to peek inside migrations Impossible to peek inside migrations

Slide 23

Slide 23 text

· The Past · The Past django-evolution, south 0.1 django-evolution, south 0.1 · The Present · The Present south 0.7 south 0.7 · The Future · The Future django 1.6? south 2.0? django 1.6? south 2.0?

Slide 24

Slide 24 text

django.db.backends.schema django.db.backends.schema

Slide 25

Slide 25 text

· Database abstraction layer · Database abstraction layer Has no idea migrations exist Has no idea migrations exist

Slide 26

Slide 26 text

from django.db import connection from django.db import connection editor = connection.schema_editor() editor = connection.schema_editor() editor.start() editor.start() editor.create_model(Author) editor.create_model(Author) editor.create_model(Book) editor.create_model(Book) editor.commit() editor.commit()

Slide 27

Slide 27 text

from django.db import connection from django.db import connection editor = connection.schema_editor() editor = connection.schema_editor() editor.start() editor.start() editor.alter_field( editor.alter_field( Book, Book, CharField(max_length=100, db_index=True), CharField(max_length=100, db_index=True), TextField(), TextField(), ) ) editor.delete_field(Author, ”age”) editor.delete_field(Author, ”age”) editor.commit() editor.commit()

Slide 28

Slide 28 text

github.com/andrewgodwin/ github.com/andrewgodwin/ django/tree/schema-alteration django/tree/schema-alteration

Slide 29

Slide 29 text

contrib. contrib.migrations migrations

Slide 30

Slide 30 text

· Migration creation/running · Migration creation/running Would replace South Would replace South

Slide 31

Slide 31 text

· Declarative migrations · Declarative migrations Combinable, optimisable Combinable, optimisable

Slide 32

Slide 32 text

· No frozen ORM · No frozen ORM Derived from the migration history Derived from the migration history

Slide 33

Slide 33 text

· Raw SQL support · Raw SQL support For those with beards For those with beards

Slide 34

Slide 34 text

· SQL output support · SQL output support For those with full-flowing beards For those with full-flowing beards

Slide 35

Slide 35 text

from migrations.api import * from migrations.api import * class Migration(BaseMigration): class Migration(BaseMigration): actions = [ actions = [ CreateModel(name = "Author", fields = [ CreateModel(name = "Author", fields = [ ("name", ("name", Field( Field( "django.db.models.fields.CharField", "django.db.models.fields.CharField", [], {"max_length": "100"} [], {"max_length": "100"} )), )), ]) ]) ] ]

Slide 36

Slide 36 text

github.com/andrewgodwin/migrations/ github.com/andrewgodwin/migrations/ Incomplete and broken code! Incomplete and broken code!

Slide 37

Slide 37 text

south 1.0 south 1.0

Slide 38

Slide 38 text

· Python 3 support · Python 3 support To coincide with Django 1.5 To coincide with Django 1.5

Slide 39

Slide 39 text

· Will require Python 2.6 · Will require Python 2.6 As otherwise Python 3 is a PITA As otherwise Python 3 is a PITA

Slide 40

Slide 40 text

All subject to change! All subject to change! This isn't set in stone. This isn't set in stone.

Slide 41

Slide 41 text

· Feedback on schema-alt branch · Feedback on schema-alt branch How to help How to help · Weird and wacky use cases · Weird and wacky use cases · Come talk to me during sprints · Come talk to me during sprints

Slide 42

Slide 42 text

Questions? Questions? Andrew Godwin / @andrewgodwin Andrew Godwin / @andrewgodwin flickr.com/oimax flickr.com/oimax