Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Database Migrations, South, And You
Andrew Godwin
September 08, 2009
Programming
0
55
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
Writing Maintainable Software At Scale
andrewgodwin
0
190
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
150
Async, Python, and the Future
andrewgodwin
2
440
How To Break Django: With Async
andrewgodwin
1
420
Taking Django's ORM Async
andrewgodwin
0
400
The Long Road To Asynchrony
andrewgodwin
0
420
The Scientist & The Engineer
andrewgodwin
1
440
Pioneering Real-Time
andrewgodwin
0
190
Just Add Await: Retrofitting Async Into Django
andrewgodwin
2
1.2k
Other Decks in Programming
See All in Programming
Swift Expression Macros: a practical introduction
kishikawakatsumi
2
720
OIDC仕様に準拠した Makuake ID連携基盤構築の裏側
ymtdzzz
0
430
フロントエンドで 良いコードを書くために
t_keshi
3
1.6k
AWS App Runnerがそろそろ本番環境でも使い物になりそう
n1215
PRO
0
940
Enumを自動で網羅的にテストしてみた
estie
0
1.2k
(新米)エンジニアリングマネージャーのしごと #RSGT2023
murabayashi
9
5.6k
状態ってなに?🙃
taro28
0
260
How to Fight Production Incidents?
asatarin
0
180
Amebaブログの会員画面システム刷新の道程
ryotasugawara
1
220
PHPアプリケーションにおけるアーキテクチャメトリクスについて / Architecture Metrics in PHP Applications
isanasan
1
220
PHPDocにおける配列の型定義を少し知る
shimabox
1
130
Gradle build: The time is now
nonews
1
440
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1020
430k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
7
570
The World Runs on Bad Software
bkeepers
PRO
59
5.7k
Intergalactic Javascript Robots from Outer Space
tanoku
261
26k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
15
1.2k
Three Pipe Problems
jasonvnalue
89
8.9k
Keith and Marios Guide to Fast Websites
keithpitt
407
21k
A Tale of Four Properties
chriscoyier
149
21k
What the flash - Photography Introduction
edds
64
10k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
44
14k
Scaling GitHub
holman
453
140k
Bash Introduction
62gerente
601
210k
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