Django a un chouette système de migrations pour répercuter sur la db les changements effectués sur les modèles.
Mais des fois, oups, on oublie de déclarer les nouvelles migrations.
Avec ce simple test case, vous pouvez détecter le problème via les tests unitaires et ainsi vous assurez que votre CI/CD ne déploit jamais du code où les modèles ne sont plus synchronisés par rapport à la db.
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # Version 2, December 2004 ## Copyright (C) 2004 Sam Hocevar ## Everyone is permitted to copy and distribute verbatim or modified # copies of this license document, and changing it is allowed as long # as the name is changed. ## DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION ## 0. You just DO WHAT THE FUCK YOU WANT TO.fromdjango.core.managementimportcall_commandfromdjango.testimportTestCaseclassMissingMigrationTestCase(TestCase):deftest_is_a_migration_missing(self):try:call_command("makemigrations","--check","--verbosity=0")exceptSystemExit:self.fail("You have updated the models but forget to create a migration.")
Démo :
$ ./manage.py test tests.windfit.models.test_missing_migration.MissingMigrationTestCase.test_is_a_migration_missing
Found 1 test(s).
Creating test database foralias'default'...
System check identified no issues (0 silenced).
F======================================================================
FAIL: test_is_a_migration_missing (tests.windfit.models.test_missing_migration.MissingMigrationTestCase.test_is_a_migration_missing)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/jtremesay/projects/webfit/tests/windfit/models/test_missing_migration.py", line 8, in test_is_a_migration_missing
call_command("makemigrations", "--check", "--verbosity=0")
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jtremesay/projects/webfit/.direnv/python-3.13/lib/python3.13/site-packages/django/core/management/__init__.py", line 194, in call_command
return command.execute(*args, **defaults)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "/home/jtremesay/projects/webfit/.direnv/python-3.13/lib/python3.13/site-packages/django/core/management/base.py", line 459, in execute
output= self.handle(*args, **options)
File "/home/jtremesay/projects/webfit/.direnv/python-3.13/lib/python3.13/site-packages/django/core/management/base.py", line 107, in wrapper
res= handle_func(*args, **kwargs)
File "/home/jtremesay/projects/webfit/.direnv/python-3.13/lib/python3.13/site-packages/django/core/management/commands/makemigrations.py", line 261, in handle
sys.exit(1)
~~~~~~~~^^^
SystemExit: 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/jtremesay/projects/webfit/tests/windfit/models/test_missing_migration.py", line 10, in test_is_a_migration_missing
self.fail("You have updated the models but forget to create a migration.")
~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: You have updated the models but forget to create a migration.
----------------------------------------------------------------------
Ran 1test in 0.246s
FAILED (failures=1)
Destroying test database foralias'default'...
Commentaires :voir le flux Atomouvrir dans le navigateur