Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Models and Migrations and Schemas - oh my!

Models and Migrations and Schemas - oh my!

A talk from DjangoCon US 2012 on South, Django, and schema migrations.

Andrew Godwin

September 06, 2012
Tweet

More Decks by Andrew Godwin

Other Decks in Programming

Transcript

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

    View full-size slide

  2. · Django core dev
    · Django core dev
    · South
    · South
    · Lanyrd
    · Lanyrd

    View full-size slide

  3. · 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?

    View full-size slide

  4. · 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?

    View full-size slide

  5. 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

    View full-size slide

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

    View full-size slide

  7. 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

    View full-size slide

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

    View full-size slide

  9. django-evolution
    django-evolution
    dmigrations
    dmigrations

    View full-size slide

  10. South 0.1
    South 0.1

    View full-size slide

  11. · South 0.2
    · South 0.2
    mysql support
    mysql support

    View full-size slide

  12. · South 0.3
    · South 0.3
    dependencies
    dependencies

    View full-size slide

  13. · South 0.4
    · South 0.4
    altering columns
    altering columns
    SQLite
    SQLite

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  16. · 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

    View full-size slide

  17. · 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?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. · Opaque migrations
    · Opaque migrations
    Impossible to peek inside migrations
    Impossible to peek inside migrations

    View full-size slide

  21. · 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?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. 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()

    View full-size slide

  25. 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()

    View full-size slide

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

    View full-size slide

  27. contrib.
    contrib.migrations
    migrations

    View full-size slide

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

    View full-size slide

  29. · Declarative migrations
    · Declarative migrations
    Combinable, optimisable
    Combinable, optimisable

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. 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"}
    )),
    )),
    ])
    ])
    ]
    ]

    View full-size slide

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

    View full-size slide

  35. south 1.0
    south 1.0

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  39. · 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

    View full-size slide

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

    View full-size slide