Talk about squashing migrations :)
This commit is contained in:
parent
850c0381aa
commit
e8dda4c60d
|
@ -323,6 +323,153 @@ class Migration(migrations.Migration):
|
|||
]
|
||||
----
|
||||
|
||||
=== Liste des migrations
|
||||
|
||||
L'option `showmigrations` de `manage.py` permet de lister toutes les migrations du projet, et d'identifier celles qui n'auraient pas encore été appliquées:
|
||||
|
||||
[source,bash]
|
||||
----
|
||||
$ python manage.py showmigrations
|
||||
admin
|
||||
[X] 0001_initial
|
||||
[X] 0002_logentry_remove_auto_add
|
||||
[X] 0003_logentry_add_action_flag_choices
|
||||
auth
|
||||
[X] 0001_initial
|
||||
[X] 0002_alter_permission_name_max_length
|
||||
[X] 0003_alter_user_email_max_length
|
||||
[X] 0004_alter_user_username_opts
|
||||
[X] 0005_alter_user_last_login_null
|
||||
[X] 0006_require_contenttypes_0002
|
||||
[X] 0007_alter_validators_add_error_messages
|
||||
[X] 0008_alter_user_username_max_length
|
||||
[X] 0009_alter_user_last_name_max_length
|
||||
[X] 0010_alter_group_name_max_length
|
||||
[X] 0011_update_proxy_permissions
|
||||
[X] 0012_alter_user_first_name_max_length
|
||||
contenttypes
|
||||
[X] 0001_initial
|
||||
[X] 0002_remove_content_type_name
|
||||
library
|
||||
[X] 0001_initial
|
||||
[X] 0002_remove_book_category_book_category
|
||||
[ ] 0003_book_summary
|
||||
sessions
|
||||
[X] 0001_initial
|
||||
----
|
||||
|
||||
|
||||
=== Squash
|
||||
|
||||
Finalement, lorsque vous développez sur votre propre branche (cf. <<git>>), vous serez peut-être tentés de créer plusieurs migrations en fonction de l'évolution de ce que vous mettez en place.
|
||||
Dans ce cas précis, il peut être intéressant d'utiliser la méthode `squashmigrations`, qui permet _d'aplatir_ plusieurs fichiers en un seul.
|
||||
|
||||
Nous partons dans deux migrations suivantes:
|
||||
|
||||
[source,python]
|
||||
----
|
||||
# library/migrations/0002_remove_book_category.py
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('library', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='book',
|
||||
name='category',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='book',
|
||||
name='category',
|
||||
field=models.ManyToManyField(to='library.Category'),
|
||||
),
|
||||
]
|
||||
----
|
||||
|
||||
[source,python]
|
||||
----
|
||||
# library/migrations/0003_book_summary.py
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('library', '0002_remove_book_category_book_category'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='book',
|
||||
name='summary',
|
||||
field=models.TextField(blank=True),
|
||||
),
|
||||
]
|
||||
----
|
||||
|
||||
La commande `python manage.py squashmigrations library 0002 0003` appliquera une fusion entre les migrations numérotées `0002` et `0003`:
|
||||
|
||||
[source,bash]
|
||||
----
|
||||
$ python manage.py squashmigrations library 0002 0003
|
||||
Will squash the following migrations:
|
||||
- 0002_remove_book_category_book_category
|
||||
- 0003_book_summary
|
||||
Do you wish to proceed? [yN] y
|
||||
Optimizing...
|
||||
No optimizations possible.
|
||||
Created new squashed migration /home/fred/Sources/gwlib/library/migrations/0002_remove_book_category_book_category_squashed_0003_book_summary.py
|
||||
You should commit this migration but leave the old ones in place;
|
||||
the new migration will be used for new installs. Once you are sure
|
||||
all instances of the codebase have applied the migrations you squashed,
|
||||
you can delete them.
|
||||
----
|
||||
|
||||
WARNING: Dans le cas où vous développez proprement (bis), il est sauf de purement et simplement supprimer les anciens fichiers; dans le cas où il pourrait exister au moins une instance ayant appliqué ces migrations, les anciens **ne peuvent surtout pas être modifiés**.
|
||||
|
||||
Nous avons à présent un nouveau fichier intitulé `0002_remove_book_category_book_category_squashed_0003_book_summary`:
|
||||
|
||||
[source,bash]
|
||||
----
|
||||
$ cat library/migrations/0002_remove_book_category_book_category_squashed_0003_book_summary.py
|
||||
# Generated by Django 4.0.3 on 2022-03-15 18:01
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('library', '0002_remove_book_category_book_category'), ('library', '0003_book_summary')]
|
||||
|
||||
dependencies = [
|
||||
('library', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='book',
|
||||
name='category',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='book',
|
||||
name='category',
|
||||
field=models.ManyToManyField(to='library.category'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='book',
|
||||
name='summary',
|
||||
field=models.TextField(blank=True),
|
||||
),
|
||||
]
|
||||
|
||||
----
|
||||
|
||||
=== Réinitialisation d'une ou plusieurs migrations
|
||||
|
||||
|
|
Loading…
Reference in New Issue