Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django South

Django South

Présentation de South, l'outil de migration de base de données avec Django

Avatar for Ordoquy Xavier - Linovia

Ordoquy Xavier - Linovia

November 06, 2012
Tweet

More Decks by Ordoquy Xavier - Linovia

Other Decks in Programming

Transcript

  1. Solution SOUTH • Stocker les migrations jouées dans la base

    • Modifier la base par du code Python
  2. 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
  3. 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
  4. 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
  5. 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
  6. Exemple 1) Ajouter les tables de traduction 2) Remplir les

    traductions 3) Supprimer les colonnes non traduites
  7. 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)
  8. 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
  9. 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
  10. 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