Slide 1

Slide 1 text

Andrew Godwin @andrewgodwin everybody loves migrations

Slide 2

Slide 2 text

Django core developer Andrew Godwin South author Database wrangler extraordinare

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

What is schema alteration? Background Why wasn't it in Django before? Why are you doing all this anyway?

Slide 5

Slide 5 text

Adding a field What is schema alteration? Changing unique_together Setting db_index=True Changing the type of a field Renaming a field Setting null=True Adding a model Deleting a model

Slide 6

Slide 6 text

Adding a field What Django did before Changing unique_together Setting db_index=True Changing the type of a field Renaming a field Setting null=True Adding a model Deleting a model

Slide 7

Slide 7 text

Why is it so hard? Versioning Different databases Not necessary up front

Slide 8

Slide 8 text

South

Slide 9

Slide 9 text

Released 2008 Most popular solution Not without issues

Slide 10

Slide 10 text

Basic Layout

Slide 11

Slide 11 text

Basic Layout schemamigration: Creates migration files in apps

Slide 12

Slide 12 text

Basic Layout schemamigration: Creates migration files in apps --auto: No, really, make them for me.

Slide 13

Slide 13 text

Basic Layout schemamigration: Creates migration files in apps datamigration: Also creates migration files in apps --auto: No, really, make them for me.

Slide 14

Slide 14 text

Basic Layout schemamigration: Creates migration files in apps datamigration: Also creates migration files in apps migrate: Applies migrations only --auto: No, really, make them for me.

Slide 15

Slide 15 text

Basic Layout schemamigration: Creates migration files in apps datamigration: Also creates migration files in apps migrate: Applies migrations only syncdb: Does no-migration apps. --auto: No, really, make them for me.

Slide 16

Slide 16 text

Basic Layout schemamigration: Creates migration files in apps datamigration: Also creates migration files in apps migrate: Applies migrations only syncdb: Does no-migration apps. Occasionally does migrations. --auto: No, really, make them for me.

Slide 17

Slide 17 text

Basic Layout schemamigration: Creates migration files in apps datamigration: Also creates migration files in apps migrate: Applies migrations only syncdb: Does no-migration apps. Occasionally does migrations. --auto: No, really, make them for me. test: Good luck with that one.

Slide 18

Slide 18 text

Issues Migrations build up over time VCS merges suck That file format...

Slide 19

Slide 19 text

Migration actions Frozen ORM

Slide 20

Slide 20 text

django.db.migrations A rewrite five years in the making

Slide 21

Slide 21 text

Design Goals

Slide 22

Slide 22 text

Clean migrations Readable diffs are really important

Slide 23

Slide 23 text

Squashable migrations No need for those hundreds of old ones

Slide 24

Slide 24 text

Better merge detection Because most teams don't read commits

Slide 25

Slide 25 text

Better commands --auto is just so pointless now

Slide 26

Slide 26 text

Automatic dependencies Stops those silent errors a year later

Slide 27

Slide 27 text

Reuseable Schema API There are valid reasons to change tables

Slide 28

Slide 28 text

Third-party compatability If nobody uses it, it's pointless

Slide 29

Slide 29 text

Third-party compatability Squashable migrations Better merge detection Better commands Automatic dependencies Reuseable Schema API Clean migrations

Slide 30

Slide 30 text

Design Overview

Slide 31

Slide 31 text

Schema alteration Migration engine

Slide 32

Slide 32 text

Schema alteration with connection.schema_editor() as editor: editor.create_model(Foo) editor.delete_field(Bar, "baz")

Slide 33

Slide 33 text

Model and Field based Will eventually replace .creation

Slide 34

Slide 34 text

Migrations makemigrations: Makes migrations. migrate: Applies migrations + legacy creation

Slide 35

Slide 35 text

Migrations makemigrations: Makes migrations. migrate: Applies migrations + legacy creation syncdb: Gone!

Slide 36

Slide 36 text

Autodetector Executor Makes new migrations Plans and runs migrations

Slide 37

Slide 37 text

Better format

Slide 38

Slide 38 text

Compound History Create Model Page with fields content, date Create Model Author with fields name, age Add title to Page Remove age from Author Migration 1 Migration 2 Migration 3 Model Page with fields title, content, date Model Author with field name

Slide 39

Slide 39 text

Operations CreateModel DeleteModel AlterModelTable AlterUniqueTogether AlterIndexTogether AddField AlterField RenameField RemoveField RunSQL RunPython

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

BONUS CODE

Slide 42

Slide 42 text

Field.deconstruct() Nicer way of serialising fields Vital to both this and the upcoming composite fields project

Slide 43

Slide 43 text

MORE BONUS CODE

Slide 44

Slide 44 text

Multiple AppCaches If you know what this means, well done. Basically, you can make multiple versions of the same model in memory at once.

Slide 45

Slide 45 text

Progress Report Where am I spending those Kickstarter pounds?

Slide 46

Slide 46 text

Schema backends PostgreSQL MySQL SQLite Oracle Migration Engine Dependency resolver Basic operations makemigrations migrate squashmigrations DONE DONE DONE DONE DONE DONE DONE DONE NOT YET Advanced operations NOT YET Autodetector PARTIAL

Slide 47

Slide 47 text

How does it affect YOU?

Slide 48

Slide 48 text

Don't Panic! syncdb and signals still work Upgrade path from South migrations South 2 to backport new format

Slide 49

Slide 49 text

Don't Panic! syncdb and signals still work Upgrade path from South migrations South 2 to backport new format Slightly Panic! Custom fields will need some work

Slide 50

Slide 50 text

Future Ideas

Slide 51

Slide 51 text

Better autodetection Automatic data migrations?

Slide 52

Slide 52 text

Versioned model interface True downtimeless deployments?

Slide 53

Slide 53 text

Percona support I almost feel sorry for the MySQL users.

Slide 54

Slide 54 text

Nonrelational support Oh, they still need migrations.

Slide 55

Slide 55 text

Things to Remember

Slide 56

Slide 56 text

New migration format

Slide 57

Slide 57 text

New migration format Even easier to use

Slide 58

Slide 58 text

New migration format Django models through and through Even easier to use

Slide 59

Slide 59 text

New migration format Django models through and through Please use a proper database Even easier to use

Slide 60

Slide 60 text

New migration format Django models through and through Please use a proper database Schemas are your friend. Even easier to use

Slide 61

Slide 61 text

Thanks. Questions welcome, especially from почемучка Andrew Godwin @andrewgodwin