Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Database Migrations, South, And You
Search
Andrew Godwin
September 08, 2009
Programming
0
160
Database Migrations, South, And You
A talk I gave at DjangoCon US 2009
Andrew Godwin
September 08, 2009
Tweet
Share
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
280
Django Through The Years
andrewgodwin
0
180
Writing Maintainable Software At Scale
andrewgodwin
0
420
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
330
Async, Python, and the Future
andrewgodwin
2
630
How To Break Django: With Async
andrewgodwin
1
700
Taking Django's ORM Async
andrewgodwin
0
700
The Long Road To Asynchrony
andrewgodwin
0
620
The Scientist & The Engineer
andrewgodwin
1
730
Other Decks in Programming
See All in Programming
Node.js, Deno, Bun 最新動向とその所感について
yosuke_furukawa
PRO
6
3k
PHPUnit 高速化テクニック / PHPUnit Speedup Techniques
pinkumohikan
1
780
イベントソーシングによってインピーダンスミスマッチから解放された話
tkawae
1
310
remix + cloudflare workers (DO) docker上でいい感じに開発する
yoshidatomoaki
0
110
爆速スッキリ! Rspack 移行の成果と道のり - Muddy Web #11
dora1998
0
130
Preact、HooksとSignalsの両立 / Preact: Harmonizing Hooks and Signals
ssssota
1
120
WordPress Playground for Developers
iambherulal
0
120
PsySHから紐解くREPLの仕組み
muno92
PRO
1
480
読もう! Android build ドキュメント
andpad
1
190
DenoでOpenTelemetryに入門する
yotahada3
2
280
英語文法から学ぶ、クリーンな設計の秘訣
newnomad
1
260
プログラミング教育のコスパの話
superkinoko
0
110
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
94
13k
Into the Great Unknown - MozCon
thekraken
35
1.7k
GraphQLの誤解/rethinking-graphql
sonatard
69
10k
Typedesign – Prime Four
hannesfritz
41
2.6k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Embracing the Ebb and Flow
colly
84
4.6k
Designing Experiences People Love
moore
140
23k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
A Philosophy of Restraint
colly
203
16k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
50
2.4k
Transcript
database�migrations, southand�you Andrew�Godwin andrew@aeracode.org
What�are�migrations? http://www.flickr.com/photos/moonjazz/1216783552/
They're�NOT�moving�data�from� one�database�to�another. http://www.flickr.com/photos/shindotv/3835365427/
Better�name:�Schema�Evolution. http://www.flickr.com/photos/fmc550uz/3562899067/
Django�has�nothing�built-in.
+ + =
+ + = 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
One�set�of�migrations�per�app. http://www.flickr.com/photos/dan_h/2517103627/
Apps�aren't�really�independent. http://www.flickr.com/photos/johnbullas/3301805150/
None
None
Is�your�app�fresh,�or�old? http://www.flickr.com/photos/matthewfch/536763437/
Fresh? -�Populate�appname/models.py -�Make�your�initial�migration: ./manage.py startmigration appname --initial -�Apply�that�migration ./manage.py migrate
appname
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)
Migrations�are�flexible. http://www.flickr.com/photos/dunechaser/2630434670/
Autodetection ./manage.py startmigration \ --auto appname added_some_column Use�the�autodetector Name�of�the�app Suffix�of�the�new�migration
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')
We�'freeze'�model�definitions. http://www.flickr.com/photos/stuckincustoms/3410783929/
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']" } ), ... }
We�migrate�data,�too. http://www.flickr.com/photos/ian-s/2152798588/
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()
Roll�your�own�migrations. http://www.flickr.com/photos/thomashawk/93819794/
Unit�test�integration,�if�you�want http://www.flickr.com/photos/cobalt/409924867/
The�Future�World�Of�Tomorrow http://www.flickr.com/photos/stuckincustoms/211566219/
Thanks�for�listening. http://www.flickr.com/photos/sovietuk/378834721/ http://south.aeracode.org Andrew�Godwin andrew@aeracode.org
Thanks�for�listening. http://www.flickr.com/photos/sovietuk/378834721/ http://south.aeracode.org Andrew�Godwin andrew@aeracode.org @andrewgodwin