Share
Embed
Django South
Présentation de South, l'outil de migration de base de données avec Django
Ordoquy Xavier - Linovia
November 06, 2012
Transcript
15 slides
-
Le problème • Garder en phase: • le code •
la base de données -
Solution SOUTH • Stocker les migrations jouées dans la base
• Modifier la base par du code Python -
Fonctionnement • Le code: <app>/migration/00xx_*.py 0001_initial.py 0002_auto__add_field_product_task_status.py • La base:
SELECT * FROM south_migrationhistory; app_name | migration | applied projects | 0001_initial | 2012-06-20 09:15:01 projects | 0002_auto__add_field_..._status | 2012-06-21 06:12:25 -
Voir l’état courant $ python manage.py migrate --list accounts (*)
0001_initial projects (*) 0001_initial ( ) 0002_auto__add_field_product_task_status djcelery (*) 0001_initial (*) 0002_v25_changes -
Jouer une migration $ python manage.py migrate projects 0002 -
Soft matched migration 0002 to 0002_auto__add_[...]_status. Running migrations for projects: - Migrating forwards to 0002_auto__add_ [...]_status. > projects:0002_auto__add_ [...]_status $ python manage.py migrate projects --list projects (*) 0001_initial (*) 0002_auto__add_field_product_task_status -
Créer une migration $ python manage.py schemamigration projects --initial +
Added model projects.EventLog + Added model projects.Project + Added M2M table for project_manager on projects.Project [...] Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate projects -
Exemple 1) Ajouter les tables de traduction 2) Remplir les
traductions 3) Supprimer les colonnes non traduites -
Modèle class Product(models.Model): name = models.CharField(max_length=32) description = models.TextField(blank=True, null=True)
class Product(models.Model): pass class TranslatedProduct(models.Model): master = models.ForeignKey(Product) language = models.CharField(max_length=8) name = models.CharField(max_length=32) description = models.TextField(blank=True, null=True) -
Nouvelle structure class Product(models.Model): name = models.CharField(max_length=32) description = models.TextField(blank=True,
null=True) class TranslatedProduct(models.Model): master = models.ForeignKey(Product) language = models.CharField(max_length=8) name = models.CharField(max_length=32) description = models.TextField(blank=True, null=True) $ python manage.py schemamigration shop --auto -
Migrations des données class Migration(DataMigration): def forwards(self, orm): for product
in orm.Product.objects.all(): trans = orm.ProductTranslation() trans.name = product.name trans.description = product.description trans.language_code = 'fr' trans.master = product trans.save() $ python manage.py datamigration shop default_translation Created 0003_default_translation.py -
Nettoyage class Product(models.Model): pass class TranslatedProduct(models.Model): master = models.ForeignKey(Product) language
= models.CharField(max_length=8) name = models.CharField(max_length=32) description = models.TextField(blank=True, null=True) $ python manage.py schemamigration shop --auto