Package: django-data-migrations
django-data-migrationDeveloping and maintaining a django project over many years can start to become a constant fight against time consuming tasks including execution of a test suite, recreation of a local environment or setting up a project in a new environment.Due to different flavors of deployment and/or different approaches within the same working environment migration files of long running django applications tent to be bloated and contain unnecessary code. Sometimes we even create migrations in the purpose of single-time usage to move, edit, duplicate or basically modify data.Generally speaking, the idea behind it is clever.With this approach you gained the option to trigger the execution of leaf migrations prior to starting your updated application code.Missing migration?
      /                \
yes, migrate        no, continue
      \                /
   restart app with new codeBut on the other hand you create a new node within a already giant migration graph.This is wheredjango-data-migrationcomes in place. It is a drop-in replacement for regular migrations, without the need of a dedicated node in the migration tree.It does that, by providing a “data-only” migration graph, that can optionally be maintained automatically in parallel with the existing migration graph, or executed independently, depending on your needs.InstallationInstall package:pip installdjango-data-migrationsConfigure package in Django settings:INSTALLED_APPS=[# django apps'data_migration',# your apps]ConfigurationThe package is configurable using theDATA_MIGRATION={}setting.Currently supported attributes:SQUASHABLE_APPS: a list of app(-label) names which allow squashing, you should only provide your own apps hereUsageExtended management commands:
-makemigrations-migrate-squashmigrations-data_migratemakemigrations# generate data migration file./manage.pymakemigrations--data-only[app_name]# generate data migration file with readable name "name_change"./manage.pymakemigrations--data-only[app_name]name_change# generate empty file./manage.pymakemigrations--data-only[app_name]--empty# generate without fileheader./manage.pymakemigrations--data-only[app_name]--no-headerThemakemigrationscommand generates a file[app_name]/data_migrations/[id]_[name].pywith content likeclassNode:name='0001_first'dependencies=()migration_dependencies=('testapp.0001_initial',)routines=[]migrate# apply data migration file./manage.pymigrate--data-only# revert complete data migration state./manage.pymigrate--data-onlyzero# revert partial data migration state./manage.pymigrate--data-only0002_some_big_changesquashmigrationsApp-wise squashing of data/regular migrations.# regular squashing of test_app migrations 0001-0015./manage.pysquashmigrationstest_app00010015# squash and replace test_app migrations 0001-0015 and extract data_migrations./manage.pysquashmigrations--extract-data-migrationstest_app00010015data_migrateExtended squashing. Allows squashing a single app, a list of apps, or all apps at once.# squash and replace all migrations at once./manage.pydata_migrate--all# squash and replace migrations app-wise./manage.pydata_migratetest_appDevelopmentTo develop the package further set up a local environment using the
provided./dev-requirements.txtfile.To run the test suite and generate a coverage report runcoveragerun-mpytest-vtestscoverage[html|report]