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
110
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
230
Django Through The Years
andrewgodwin
0
110
Writing Maintainable Software At Scale
andrewgodwin
0
350
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
270
Async, Python, and the Future
andrewgodwin
2
560
How To Break Django: With Async
andrewgodwin
1
610
Taking Django's ORM Async
andrewgodwin
0
620
The Long Road To Asynchrony
andrewgodwin
0
540
The Scientist & The Engineer
andrewgodwin
1
630
Other Decks in Programming
See All in Programming
Microservices rules (July 2024) : what good looks like
cer
PRO
0
1.6k
なぜ宣言的 UI は壊れにくいのか / Why declarative UI is less fragile
uenitty
29
13k
最近追加した型の紹介とその振り返り
aki19035vc
0
180
CSC307 Lecture 13
javiergs
PRO
0
150
OpenAI/Gemini APIを使って EPUBを翻訳するCLIツールをつくってみた
tomiyan
0
790
入社1ヶ月でここまでやった!Findy Toolsインフラ支援の最適化
rvirus0817
6
1.4k
初心者がおさえておきたいAWS CDKのベストプラクティス 2024
konokenj
15
7.3k
Javaの現状2024夏 / Java current status 2024 summer
kishida
4
1.4k
Rustのweb開発を助ける 便利なツール紹介
yuki0418
1
190
しくじり先生 Image Matching Challenge 2024 編
goosehaaan
0
810
コード生成を伴うLLMエージェント - 2024.07.18 Tokyo AI
smiyawaki0820
11
4.1k
CSC307 Lecture 08
javiergs
PRO
0
330
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.9k
Making Projects Easy
brettharned
111
5.7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
129
32k
Git: the NoSQL Database
bkeepers
PRO
423
64k
Build The Right Thing And Hit Your Dates
maggiecrowley
28
2.2k
Docker and Python
trallard
37
2.9k
Fantastic passwords and where to find them - at NoRuKo
philnash
42
2.7k
What’s in a name? Adding method to the madness
productmarketing
PRO
21
2.9k
It's Worth the Effort
3n
181
27k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
23
1.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
353
29k
Automating Front-end Workflow
addyosmani
1362
200k
Transcript
database�migrations, southand�you Andrew�Godwin
[email protected]
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
[email protected]
Thanks�for�listening. http://www.flickr.com/photos/sovietuk/378834721/ http://south.aeracode.org Andrew�Godwin
[email protected]
@andrewgodwin