A migration squasher that doesn't care how Humpty Dumpty was put together.
Project description
django-squash
“django-squash” is a migration enhancement built on top of Django’s standard migration classes. It aims to eliminate bloat and slowness in migration processes by replacing certain commands. The vision and architecture of Django migrations remain unchanged.
Before using “django-squash,” it’s important to understand the normal Django makemigrations and squashmigrations commands. Migration files consist of operations that may or may not affect the database table for a model. “elidable” operations can be eliminated when squashing migrations, while “non-elidable” operations cannot. Best way to think about the word “elidable” is to simply think “forgetable” or “disgardable” – can this operation be disgarded once it’s been ran?
The package introduces a command named squash_migrations as an alternative to Django’s squashmigrations. This command minimizes the number of operations needed to build the database’s schema, resulting in faster testing pipelines and deployments, especially in scenarios with multiple tenants.
The catch lies in proper usage of elidable vs. non-elidable operations and the requirement that databases must not fall behind to the point where eliminated migration operations are needed. The squash_migrations command removes all elidable operations and preserves non-elidable ones.
It’s crucial to run the squash_migrations command once per release after cutting the release. All databases must be on the current release, the prior release, or somewhere in between. Databases before the prior release cannot directly upgrade to the current release; they must first apply the prior release’s migrations and then the current release’s minimal operations.
This approach is not a tutorial on migration strategy but emphasizes the need for understanding multi-app systems, avoiding circular dependencies, and designing efficient migration processes. The tool is developed based on experience and frustration, aiming to automate and improve the migration squashing process.
You can read more about our motivation to creating this tool.
Setup
pip install django-squash
Add django_squash to your INSTALLED_APPS.
(optional) There are some settings you can customize
Run ./manage.py squash_migrations once after each release
Profit!
Developing
clone the repo
cd into repo
(optional) run inside docker environment that way you can change the python version quickly and iterate faster
docker run --rm -it -v .:/app -v django-squash-pip-cache:/root/.cache/pip -e PYTHONDONTWRITEBYTECODE=1 python:3.12 bash -c "cd app; pip install -e .[test,lint]; echo \"alias linters=\\\"echo '> isort'; isort .; echo '> black'; black .; echo '> ruff'; ruff check .;echo '> flake8'; flake8 .; echo '> rst-lint'; rst-lint README.rst docs/*\\\"\" >> ~/.bash_profile; printf '\n\n\nrun **pytest** to run tests, **linters** to run linters\n\n'; exec bash --init-file ~/.bash_profile"
Alternatively, you can also create a virtual environment and run
python3 -m venv venv
source venv/bin/activate
pip install -e '.[test]'
Run tests
pytest
Before making a commit, make sure that the formatter and linter tools do not detect any issues.
isort .
black .
flake8 .
ruff check .
rst-lint .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_squash-0.0.12.tar.gz
.
File metadata
- Download URL: django_squash-0.0.12.tar.gz
- Upload date:
- Size: 26.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01da1bb7a05555d2cd5b9a119e6622049981c33e40bb2527c633e4ff2902d9be |
|
MD5 | 37cde0b9218586fd28636c76535cc3d6 |
|
BLAKE2b-256 | 5ab1707c7ce29373bb1a7df61fb85bed30fd4a2ce7a1c9192d3ea4d7cea94efe |
File details
Details for the file django_squash-0.0.12-py3-none-any.whl
.
File metadata
- Download URL: django_squash-0.0.12-py3-none-any.whl
- Upload date:
- Size: 20.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92c5958be474e1897b210ddd7f2ef180d9081413a8ec4450c88afb11b033b409 |
|
MD5 | a67478d30a5ed21b8fea282ef491882f |
|
BLAKE2b-256 | c195e9a56395fd503b95ad2365c5fcefd3c3b387d3b7c02784e290ee674964e2 |