Management command to attempt Django migrations and rollback on failure
Project description
django-migrate-or-rollback
While single migrations in Django are atomic (as long as they have the default atomic=True),
a group of migrations are not. Thus, when running migrations, you can have a partial
failure where some but not all of your migrations succeed. This library fixes that.
This library provides a new management command migrate_or_rollback. It's a drop-in
replacement for the Django builtin management command migrate. Here's how it works:
- Checks your database and current migration files for the latest migrations run per Django app.
- Runs migrations as normal.
- If migrations fail, it rolls back to the migrations identified in step 1.
Instalation
pip install django-migrate-or-rollback
Alternatively, add django-migrate-or-rollback to your requirements.txt file.
Then, add "django_migrate_or_rollback" to your INSTALLED_APPS in settings.py, like so:
# settings.py
INSTALLED_APPS = [
# ...
"django_migrate_or_rollback",
]
Usage
Run python manage.py migrate_or_rollback instead of the standard migrate command.
In particular, you should use migrate_or_rollback in place of migrate in your deployment scripts or CI/CD system.
migrate_or_rollback has all of the same options as migrate, such as the --noinput flag.
Warning
This library assumes that your migrations are reversable. Not all migrations are reversible. Additionally, rolling back migrations only reverses schema doesn't rewind the database contents.
In particular:
- Deleted data (such as dropping columns or tables) won't be restored by rolling back the migration that deletes it. To avoid this, you should make fields nullable in one deploy and delete them in the next.
RunPythonstatements that are missing areversefunction will error on rollback. At a minimum, addmigrations.RunPython.noopas a reverse function. Additionally, RunPython reverse functions can be used to rewind changes to database contents on migration rollback.- A migration that deletes a non-nullable field will error on rollback. To avoid this, make the field nullable in one deploy and delete it in the next.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_migrate_or_rollback-0.1.0.tar.gz.
File metadata
- Download URL: django_migrate_or_rollback-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0592d4862225d8fac1c534bfd9f3fd72d98384cadbdc94a016691ae8f9c7c62c
|
|
| MD5 |
927e41bc2c7445032abc7bc20ddbcb28
|
|
| BLAKE2b-256 |
85e535a6e56275ddb2fda95469a8173f08f0f0a623535e5dd805f05fd95309d0
|
File details
Details for the file django_migrate_or_rollback-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_migrate_or_rollback-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e712baac471913e137081e68dce8e22beb8bc3cbcda3236eca62f0242d706d9
|
|
| MD5 |
6af3fc43576e8430853c0f7fb60feba6
|
|
| BLAKE2b-256 |
233303ab6f0d853a10693fe475b92c920d1949af967f1d1d21242a0f8c52ef13
|