Squashing Data Migrations

23 views
Skip to first unread message

Saksham Bajaj

unread,
Dec 28, 2022, 9:21:36 PM12/28/22
to Django users
Hi,

This feels like a very common use case but I can't figure out the best practice or any guidance online so would really appreciate any advice.

Django Version: 3.1
Python: 3.7


Here is the scenario:
  1. I have a custom data migration that loads custom data into a migration. Following exactly the pattern in the given documentation on data migrations.
    In this case, this migration file was called 'add_areas.py' and exists in the `migrations/ ` folder as expected. The 'add_areas' migration has a single RunPython(add_areas, reverse_code=remove_areas) operation.
  2. I squashed all my migrations, including 'add_areas' into  'all_squashed.py'. Now, the RunPython command in 'all_squashed.py' references the custom `add_areas` and `remove_areas` function in the 'add_areas.py' migration file.
  3. As the migration is squashed, and applied in my environments, I now need to delete the 'add_areas' data migration it replaces as recommended at the end of the section in the documentation on squashing migrations
  4. I can't delete the 'add_areas' data migration now as it has the custom functions.

The few options that I have come up with to have this to work, but I feel there is a cleaner pattern which will let me keep the custom functions in the migrations/ folder and squash migrations easily:
  1. Move all the custom functions for any data migrations to the squashed migration. This will let me delete the data migration `add_areas`. I however want to keep these functions in different files for readability rather than having to copy and modify them every time I squash migrations.
  2. I tested simply deleting migration class/having an empty migration class/having a migration class with no dependencies/operations from 'add_areas' and only leaving the custom functions in those files. All of these raised errors, either BadMigration or multiple leaves.
  3. The failure in option 2 leaves me to Option 3 where I can simply move these functions outside the migrations folder and reference the files from there. This however feels clunky, and I don't want to create a new folder in my application just for a few data migrations. 
So essentially looking for a pattern that will allow me to keep custom migration function files/modules within 'migrations/' and not have to move/modify these functions everytime I squash my migrations.

Thank you for any advice :)

Regards,
Saksham
Reply all
Reply to author
Forward
0 new messages