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

Migrating with South

Migrating with South

A talk I gave at DJUGL #2

Andrew Godwin

January 19, 2009
Tweet

More Decks by Andrew Godwin

Other Decks in Programming

Transcript

  1. south
    Andrew�Godwin
    Migrating�with
    [email protected]

    View Slide

  2. One�of�many
    south
    Migrating�with
    django-evolution
    migratory
    dmigrations
    deseb

    View Slide

  3. Two�parts
    south
    Migrating�with
    migration�engine
    database�abstractor
    MySQL
    PostgreSQL
    SQLite
    SQL�Server
    tracking
    dependencies
    conflicts

    View Slide

  4. What�are�migrations?
    south
    Migrating�with
    /�mutations�/�diffs�/�etc

    View Slide

  5. Problem:�database�schema�isn't
    meaningfully�versioned
    south
    Migrating�with
    -�It's�usually�in�a�VCS�(models.py),�but�the
    ��database�exists��outside�of�version�control.
    -�Migration�libraries�essentially�add�version�control
    ��to�database�schemas.

    View Slide

  6. South�keeps�its�distance�from�models.py.
    south
    Migrating�with
    You're�never�sure�if�it's�from�the�past,�present�or�future.
    (see:�cheesy�time-travel�films)

    View Slide

  7. You�can�use�it,�though.
    south
    Migrating�with
    While�migrations�are�independent�from�it,�they�can
    be�made�from�models.py�automatically.

    View Slide

  8. A�migration! south
    Migrating�with
    from south.db import db
    from core.models import *
    cl ass Migration:
    def forwards(self):
    db.add_column("core_nation", "slug",
    models.SlugField(unique=True, default="test")
    )
    def backwards(self):
    db.add_column("core_nation", "slug")

    View Slide

  9. How�is�migration�formed? south
    Migrating�with
    ./manage.py startmigration core test_migration
    The�South�command App�name Migration�name
    ./manage.py startmigration core \
    test_migration
    Blank�migration:
    Add�everything�(initial;�replaces�the�syncdb):
    --initial
    ./manage.py startmigration core \
    test_migration
    Add�a�new�field:
    --add-field Picture.slug
    These�all�make�a�file�like�c ore/migrations/0002_test_migration.py.

    View Slide

  10. Migrations�are�per-app.
    south
    Migrating�with
    If�an�app�doesn't�need�migrations,�it�uses�syncdb�as�normal.
    If�apps�depend�on�others,�there's�a�dependency�resolver.

    View Slide

  11. Installation
    south
    Migrating�with
    Put�South�somewhere�on�the�Python�path
    svn co http://.../trun k/south/ easy_install South
    or
    Add�it�to�INSTALLED_APPS
    INSTALLED_APPS = [ ... , "south" ]
    Run�syncdb�(South�uses�a�table�to�track�applied�migrations)
    ./manage.py syncdb
    That's�it.�Start�migrating.
    (We�have�docs�on�things�like�converting�existing�apps)

    View Slide

  12. Database�Abstraction
    south
    Migrating�with
    Sigh.

    View Slide

  13. south
    Migrating�with
    epic�fail fail win
    M
    ySQL
    Postgres
    SQLite
    SQL�Server
    Highly�scientific�graph
    No�DDL�Transactions
    No�Column�Alters Awesome.
    Ugh.

    View Slide

  14. south
    Migrating�with
    We�have�good�support�for�PostgreSQL�and�MySQL
    But�MySQL�users�can't�wrap�migrations�in�transactions.
    We�have�alright�support�for�SQLite
    Haven't�got�the�alter_column�workarounds�in�yet.
    We�have�basic�support�for�SQL�Server
    It�wasn't�me,�honest.

    View Slide

  15. south
    Migrating�with
    # M
    ock M
    odel s
    User = db.mock_model(model_name='User', db_table='auth_user', db_tablespace='',
    pk_field_name='id', pk_field_type=models.AutoField)
    # M
    odel ' User Pr of i l e'
    db.create_table('core_userprofile', (
    ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
    ('user', models.OneToOneField(User, related_name="profile")),
    ))
    API�Example

    View Slide

  16. south
    Migrating�with
    The�Future
    ORM�in�migrations
    More�databases
    Robust�models.py�parsing

    View Slide

  17. south
    Migrating�with
    Fin.
    http://south.aeracode.org
    Ideas,�tickets�and�patches�always�welcome
    Andrew�Godwin [email protected]

    View Slide