Skip to content

Update to django 4.2 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
identification within third-party archives.

Copyright 2019 3YOURMIND GmbH
Copyright 2024 GitGuardian

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# django-replace-migrations

This package is an extension to djangos `makemigrations.py`.
It can be used to get rid of old migrations as an alternative to djangos `squashmigration` command.
This package offers a new django command: `replace_all_migrations`.
It can be use to get rid of old migrations as an alternative to django's `squashmigration` command.

## Reasoning

In big django projects, migration files easily pile up and get an increasing problem.
Django comes with the squashmigration command - however, it is hard to handle because of multiple reasons.
Especially, it can not handle circular dependencies - they must be resolved [manually and with great care](https://stackoverflow.com/questions/37711402/circular-dependency-when-squashing-django-migrations).

Django comes with the squashmigration command - however, it is hard to handle because of multiple reasons. Especially, it can not handle circular dependencies - they must be resolved [manually and with great care](https://stackoverflow.com/questions/37711402/circular-dependency-when-squashing-django-migrations).

One possible solution is to:

Expand All @@ -17,13 +17,11 @@ One possible solution is to:

This workflow might work fine, if you have only few (production) servers - however, it becomes hard, when you have many environments with different versions of your application.

With django-replace-migrations also creates new initial migrations, but also, additionally, adds the already existing migrations to the `replace` list of the new migration
(That list is used by `squashmigrations` as well). By doing that, faking migrations is not needed anymore.
django-replace-migrations also creates new initial migrations, but also, additionally, it adds the already existing migrations to the `replace` list of the new migration (That list is used by `squashmigrations` as well). By doing that, faking migrations is not needed anymore.

## Warning

The new replacing migrations will not consider any `RunPython` or `RunSQL` operations.
That might be acceptable depending on your use of those operations and if you need those to prepare a fresh database.
The new replacing migrations will add not elidable special operations (`RunPython`, `RunSQL` or `SeparateDatabaseAndState`) at the end of the squash files. You will have to manually add them when suitable.

## Installation

Expand All @@ -42,10 +40,16 @@ and add `django_replace_migrations` to your list of installed apps.
If your apps are not depending on each other, you can use django-replace-migrations like this:

```
./manage.py makemigratons --replace-all --name replace [app1, app2, ...]
./manage.py replace_all_migrations --name replace [app1, app2, ...]
```

Note, that you will need to list all of your apps explicitly - otherwise django will also try to replace migrations from dependencies:

```
from django.apps import apps
print(" ".join(map(str, sorted({model._meta.app_label for model in apps.get_models()}))))
```

Note, that you will need to [list all of your apps](https://stackoverflow.com/questions/4111244/get-a-list-of-all-installed-applications-in-django-and-their-attributes) explicitly - otherwise django will also try to replace migrations from dependencies.
While `--name` could be omitted, it is highly recommended to use it so that you can easily recognize the new migrations.

If for any of your apps there are not one but two or more migrations created, your apps are depending on each other (see below).
Expand All @@ -71,7 +75,7 @@ The workflow for this would be:
- `git checkout 2.0`
- create a new branch `git checkout -b 2-0-replace-migrations`
- Install `django-replace-migration` here.
- run `./manage.py makemigrations --replace-all --name replace_2_0 app1, app2, ...` ([How to get all apps](https://stackoverflow.com/questions/4111244/get-a-list-of-all-installed-applications-in-django-and-their-attributes))
- run `./manage.py replace_all_migrations --name replace_2_0 app1, app2, ...` ([How to get all apps](https://stackoverflow.com/questions/4111244/get-a-list-of-all-installed-applications-in-django-and-their-attributes))
- commit and note the commit hash
- `git checkout [your main/feature branch]`
- `git cherry-pick [commit-hash from 2-0-delete-migrations]`
Expand All @@ -87,7 +91,3 @@ If your app is below 2.0 and you want to update to something after 2.0, you firs
- upgrading from 1.0 to 1.5 will be possible
- upgrading from 2.0 to 3.0 will be possible
- upgrading from 1.0 to 3.0 will be **not** possible

## `makemigration.py` compatibility

This package requires deep integration into `makemigrations.py` so that I needed to copy the whole `makemigrations.py` here. Currently the version of `makemigrations.py` is copied from Django 2.1, however it is also tested with Django 3.0 and works there as well. If you encounter problems, please write what version of Django you are using.
Loading