Package: django-safemigrate
django-safemigrate adds asafemigratecommand to Django
to allow for safely running a migration command when deploying.UsageInstalldjango-safemigrate, then add this to theINSTALLED_APPSin 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.fromdjango_safemigrateimportSafeclassMigration(migrations.Migration):safe=Safe.before_deployAt this point you can run thesafemigrateDjango 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 normalmigrateDjango 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 OptionsThere are three options for the value of thesafeproperty of the migration.Safe.before_deployThis 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_deployThis migration is only safe to run after the code change is deployed.
This is the default that is applied if nosafeproperty is given.
For example, a migration that removes a field from a model.Safe.alwaysThis migration is safe to run beforeandafter
the code change is deployed.
For example, a migration that changes thehelp_textof a field.Pre-commit HookTo get the most from django-safemigrate,
it is important to make sure that all migrations
are marked with the appropriatesafevalue.
To help with this, we provide a hook for use withpre-commit.Install and configure pre-commit,
then add this to thereposkey of your.pre-commit-config.yaml:repos:-repo:https://github.com/aspiredu/django-safemigraterev:"4.2"hooks:-id:checkNonstrict ModeUnder 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 theSAFEMIGRATEsetting:SAFEMIGRATE="nonstrict"In this modesafemigratewill run all the migrations
that are not blocked by any unsafe migrations.
Any remaining migrations can be run after the fact
using the normalmigrateDjango command.ContributingTo get started contributing, you’ll want to clone the repository,
install dependencies viapoetry,
and set uppre-commit.gitclonegit@github.com:aspiredu/django-safemigrate.gitcddjango-safemigratepoetryinstallpre-commitinstallTo run the tests use:poetryruntoxTo 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 versiongit tag 1.0and push that to origin.Create thenew releaseon GitHub.Publish the new version to PyPI withpoetry publish.SeePoetry’s docson how to configure your local environment to publish to PyPI. Key your PyPI
token to only django-safemigrate.poetryconfigpypi-token.pypi<my-token>