Database Migrations, South, And You

Database Migrations, South, And You

A talk I gave at DjangoCon US 2009

077e9a0cb34fa3eba2699240c9509717?s=128

Andrew Godwin

September 08, 2009
Tweet

Transcript

  1. database�migrations, southand�you Andrew�Godwin andrew@aeracode.org

  2. What�are�migrations? http://www.flickr.com/photos/moonjazz/1216783552/

  3. They're�NOT�moving�data�from� one�database�to�another. http://www.flickr.com/photos/shindotv/3835365427/

  4. Better�name:�Schema�Evolution. http://www.flickr.com/photos/fmc550uz/3562899067/

  5. Django�has�nothing�built-in.

  6. + + =

  7. + + = Migration�1:�Creates�table�A Migration�2:�Creates�table�B Migration�3:�Adds�field�to�A, ������������������������creates�t able�C Apply�each�in�order,�get�current�schema

  8. One�set�of�migrations�per�app. http://www.flickr.com/photos/dan_h/2517103627/

  9. Apps�aren't�really�independent. http://www.flickr.com/photos/johnbullas/3301805150/

  10. None
  11. None
  12. Is�your�app�fresh,�or�old? http://www.flickr.com/photos/matthewfch/536763437/

  13. Fresh? -�Populate�appname/models.py -�Make�your�initial�migration: ./manage.py startmigration appname --initial -�Apply�that�migration ./manage.py migrate

    appname
  14. Already�syncdb'd? -�Locally,�run: ./manage.py convert_to_south appname -�Commit,�and�then�on�others�do: ./manage.py migrate --fake appname

    0001 (see:�south.aeracode.org/wiki/ConvertingAnApp)
  15. Migrations�are�flexible. http://www.flickr.com/photos/dunechaser/2630434670/

  16. Autodetection ./manage.py startmigration \ --auto appname added_some_column Use�the�autodetector Name�of�the�app Suffix�of�the�new�migration

  17. class Migration: def forwards(self, orm): # Adding field 'Adopter.lizard2' db.add_column('southdemo_adopter',

    'lizard2', orm['southdemo.adopter:lizard2']) def backwards(self, orm): # Deleting field 'Adopter.lizard2' db.delete_column('southdemo_adopter', 'lizard2_id')
  18. We�'freeze'�model�definitions. http://www.flickr.com/photos/stuckincustoms/3410783929/

  19. models = { 'southdemo.adopter' : { 'first_name': ( 'django.db.models.fields.CharField' ,

    [], {'max_length': '50'} ), 'lizard': ( 'django.db.models.fields.related.ForeignKey' , [], { 'related_name' : "'adopters'", 'to': "orm['southdemo.Lizard']" } ), ... }
  20. We�migrate�data,�too. http://www.flickr.com/photos/ian-s/2152798588/

  21. def forwards(self, orm): for adopter in orm.Adopter.objects.all(): try: adopter.first_name, adopter.last_name

    = \ adopter.name.split( " ", 1) except ValueError : adopter.first_name, adopter.last_name = \ adopter.name, "" adopter.save()
  22. Roll�your�own�migrations. http://www.flickr.com/photos/thomashawk/93819794/

  23. Unit�test�integration,�if�you�want http://www.flickr.com/photos/cobalt/409924867/

  24. The�Future�World�Of�Tomorrow http://www.flickr.com/photos/stuckincustoms/211566219/

  25. Thanks�for�listening. http://www.flickr.com/photos/sovietuk/378834721/ http://south.aeracode.org Andrew�Godwin andrew@aeracode.org

  26. Thanks�for�listening. http://www.flickr.com/photos/sovietuk/378834721/ http://south.aeracode.org Andrew�Godwin andrew@aeracode.org @andrewgodwin