Taille: 5738
Commentaire:
|
Taille: 5832
Commentaire:
|
Texte supprimé. | Texte ajouté. |
Ligne 1: | Ligne 1: |
= Chargement de l'environnement virtuel = | = Django : SGBD inspectDb et South = <<TableOfContents(3)>> == Chargement de l'environnement virtuel == |
Ligne 10: | Ligne 14: |
= Création d'un projet Django = | == Création d'un projet Django == |
Ligne 13: | Ligne 17: |
== Boostrap == | === Boostrap === |
Ligne 31: | Ligne 35: |
== Base de données MySQL == | === Base de données MySQL === |
Ligne 42: | Ligne 46: |
== Activation de l'admin == | === Activation de l'admin === |
Ligne 59: | Ligne 63: |
= Exploiter l'existant = | == Exploiter l'existant == |
Ligne 66: | Ligne 70: |
== Câblage db == | === Câblage db === |
Ligne 86: | Ligne 90: |
== Câblage de l'ancien dans l'admin == | === Câblage de l'ancien dans l'admin === |
Ligne 92: | Ligne 96: |
= Nouveau système = | == Nouveau système == |
Ligne 100: | Ligne 104: |
== initialisation du framework de migration pour l'app == | === initialisation du framework de migration pour l'app === |
Ligne 107: | Ligne 111: |
== Création du script de modification de BD selon le modèle == | === Création du script de modification de BD selon le modèle === |
Ligne 121: | Ligne 125: |
== Itération 1 (création des formations à partir de l'existant) == | === Itération 1 (création des formations à partir de l'existant) === |
Ligne 129: | Ligne 133: |
== Itération 2 (création des universités à partir de l'existant) == | === Itération 2 (création des universités à partir de l'existant) === |
Ligne 144: | Ligne 148: |
== Itération 3 (création des domaines à partir de l'existant) == | === Itération 3 (création des domaines à partir de l'existant) === |
Ligne 160: | Ligne 164: |
= Commandes utiles = | == Commandes utiles == |
Ligne 163: | Ligne 167: |
transactionnelle, il est bon re resetter l'application dans un état stable, | transactionnelles, il est bon resetter l'application dans un état stable, |
Django : SGBD inspectDb et South
Sommaire
Chargement de l'environnement virtuel
(Voir les prérequis de l'atelier)
source atelier/bin/activtate
Création d'un projet Django
Boostrap
python manage.py startproject foad cd foad python manage.py runserver (CTRL+C) echo "*.pyc" > .gitignore git init git add . git commit -m 'nouveau projet'
à chaque modification correcte au cours de l'atelier, commiter-les, vous pourrez plus facilement voir les modifications de fichiers, revenir en arrière, etc...
Base de données MySQL
- dans 'foad/settings.py':
- variable DATABASES, configurer le 'default' avec une bd vierge nommée 'atelier'
- variable INSTALLED_APPS, ajouter 'south'
https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/foad/settings.py
python manage.py syncdb
(créer le superuser)
Activation de l'admin
- dans 'foad/settings.py':
- variable INSTALLED_APPS, décommenter 'django.contrib.admin'
https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/foad/settings.py
- dans 'foad/urls.py',décommenter:
from django.contrib import admin admin.autodiscover() url(r'^admin/', include(admin.site.urls)),
https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/foad/urls.py
python manage.py runserver (127.0.0.1:8000/admin)
Exploiter l'existant
Création de l'application
django-admin.py startapp ancien
Câblage db
- dans 'foad/settings.py':
- variable DATABASES, ajouter une nouvelle entrée au dictionnaire avec comme clef 'consultation-ancien'
- variable INSTALLED_APPS, ajouter votre nouvelle application: 'ancien'
- python manage.py inspectdb --database=consultation-ancien
python manage.py inspectdb --database=consultation-ancien > ancien/models.py
- ouvrir le fichier ancien/models.py et ne garder que le modèle 'Atelier'
ajouter une méthode unicode() pour afficher l'intitulé quand on inspecte un objet
https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/ancien/models.py
- cabler le router de base de données (défini la politque d'accès aux bds)
- python manage.py shell
from ancien.models import Atelier; Atelier.objects.all()
Câblage de l'ancien dans l'admin
- création d'un fichier ancien/admin.py
Nouveau système
python manage.py startapp catalogue
- dans 'foad/settings.py':
- variable INSTALLED_APPS, ajouter votre nouvelle application: 'catalogue'
initialisation du framework de migration pour l'app
python manage.py schemamigration catalogue --initial python manage.py migrate catalogue
Création du script de modification de BD selon le modèle
* création du modèle Formation dans catalogue/models.py
python manage.py schemamigration catalogue --auto python manage.py migrate catalogue
* création d'un fichier catalogue/admin.py https://github.com/olarcheveque/atelier-south-inspectdb/blob/6d153c1c3c4329720fe70a4dc5de6df5a91d41fd/catalogue/admin.py
Itération 1 (création des formations à partir de l'existant)
python manage.py datamigration catalogue recuperation_id_intitule --freeze ancien
Itération 2 (création des universités à partir de l'existant)
* ajout du modèle Universite dans catalogue/models.py
python manage.py schemamigration catalogue --auto python manage.py migrate catalogue
python manage.py datamigration catalogue creation_universite --freeze ancien
python manage.py migrate catalogue
Itération 3 (création des domaines à partir de l'existant)
* ajout du modèle Universite dans catalogue/models.py https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/catalogue/models.py#L20
python manage.py schemamigration catalogue --auto python manage.py migrate catalogue
python manage.py datamigration catalogue creation_domaine --freeze ancien
python manage.py migrate catalogue
Commandes utiles
En cas, d'une migration échouée, comme les modifications ne sont pas transactionnelles, il est bon resetter l'application dans un état stable, corrélé avec les modèles.
python manage.py sqlclear catalogue > del_catalogue mysql -u <user> -p --database=atelier < del_catalogue python manage.py dbshell - delete from south_migrationhistory where app_name = 'catalogue';