Migrating with South

Migrating with South

A talk I gave at DJUGL #2

077e9a0cb34fa3eba2699240c9509717?s=128

Andrew Godwin

January 19, 2009
Tweet

Transcript

  1. south Andrew�Godwin Migrating�with andrew@aeracode.org

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

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

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

  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.

  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)

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

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

  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)
  12. Database�Abstraction south Migrating�with Sigh.

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

  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
  16. south Migrating�with The�Future ORM�in�migrations More�databases Robust�models.py�parsing

  17. south Migrating�with Fin. http://south.aeracode.org Ideas,�tickets�and�patches�always�welcome Andrew�Godwin andrew@aeracode.org