Safely run migrations before deployment
Project description
django-safemigrate adds a safemigrate command to Django to allow for safely running a migration command when deploying.
Usage
Install django-safemigrate, then add this to the INSTALLED_APPS in the settings file:
INSTALLED_APPS = [
# ...
"django_safemigrate.apps.SafeMigrateConfig",
]
Then mark any migration that may be run during a pre-deployment stage, such as a migration to add a column.
from django_safemigrate import Safe
class Migration(migrations.Migration):
safe = Safe.before_deploy
At this point you can run the safemigrate Django command to run the migrations, and only these migrations will run. However, if migrations that are not safe to run before the code is deployed are dependencies of this migration, then these migrations will be blocked, and the safemigrate command will fail with an error.
When the code is fully deployed, just run the normal migrate Django command, which still functions normally. For example, you could add the command to the release phase for your Heroku app, and the safe migrations will be run automatically when the new release is promoted.
Safety Options
There are three options for the value of the safe property of the migration.
Safe.before_deploy
This migration is only safe to run before the code change is deployed. For example, a migration that adds a new field to a model.
Safe.after_deploy
This migration is only safe to run after the code change is deployed. This is the default that is applied if no safe property is given. For example, a migration that removes a field from a model.
Safe.always
This migration is safe to run before and after the code change is deployed. For example, a migration that changes the help_text of a field.
Pre-commit Hook
To get the most from django-safemigrate, it is important to make sure that all migrations are marked with the appropriate safe value. To help with this, we provide a hook for use with pre-commit. Install and configure pre-commit, then add this to the repos key of your .pre-commit-config.yaml:
repos:
- repo: https://github.com/aspiredu/django-safemigrate
rev: "4.2"
hooks:
- id: check
Nonstrict Mode
Under normal operation, if there are migrations that must run before the deployment that depend on any migration that is marked to run after deployment (or is not marked), the command will raise an error to indicate that there are protected migrations that should have already been run, but have not been, and are blocking migrations that are expected to run.
In development, however, it is common that these would accumulate between developers, and since it is acceptable for there to be downtime during the transitional period in development, it is better to allow the command to continue without raising.
To enable nonstrict mode, add the SAFEMIGRATE setting:
SAFEMIGRATE = "nonstrict"
In this mode safemigrate will run all the migrations that are not blocked by any unsafe migrations. Any remaining migrations can be run after the fact using the normal migrate Django command.
Contributing
To get started contributing, you’ll want to clone the repository, install dependencies via poetry, and set up pre-commit.
git clone git@github.com:aspiredu/django-safemigrate.git
cd django-safemigrate
poetry install
pre-commit install
To run the tests use:
poetry run tox
To publish a new version:
Find and replace all instances of the previous version with the new version.
Commit and push that to origin.
Tag the commit with the new version git tag 1.0 and push that to origin.
Create the new release on GitHub.
Publish the new version to PyPI with poetry publish.
See Poetry’s docs on how to configure your local environment to publish to PyPI. Key your PyPI token to only django-safemigrate.
poetry config pypi-token.pypi <my-token>
Project details
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_safemigrate-4.2.tar.gz
.
File metadata
- Download URL: django_safemigrate-4.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd0ee8c258757fcc4b774b876cf587bd4df5f946ae6eb68643f05a5f88daaa20 |
|
MD5 | b92ce834ba75cb03e9d8ff5adc6d41d1 |
|
BLAKE2b-256 | b586542534fe74f9aeb99716d63646f91c7965bcd5cc2772f4d2d27b59c4b829 |
File details
Details for the file django_safemigrate-4.2-py3-none-any.whl
.
File metadata
- Download URL: django_safemigrate-4.2-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a18f961f812d0c90ebd652a17cf80c68560461ca07e62a0ef8b4ff51bec62b29 |
|
MD5 | 409ab688360f0f67a4288af045e78b1b |
|
BLAKE2b-256 | 1e84c8b03322f9d85012ef2a7b99f3bd1d90942ef46d082a44b08b0d95988061 |