Skip to main content

Safely run migrations before deployment

Project description

Latest Version Build status Code Coverage Code style: black

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.

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.

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

django-safemigrate-3.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

django_safemigrate-3.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file django-safemigrate-3.0.tar.gz.

File metadata

  • Download URL: django-safemigrate-3.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.3 Linux/5.4.0-48-generic

File hashes

Hashes for django-safemigrate-3.0.tar.gz
Algorithm Hash digest
SHA256 0d446268322ae411a10c8b49dd52a9f4d182db69f39ef839280edb4b84dad232
MD5 3516c2700c1466ab993d12cdfad7a5f1
BLAKE2b-256 8df54ff306bdf89f583613156937bdb3e7dc2dadfe48e5101605a0aefdf83c95

See more details on using hashes here.

File details

Details for the file django_safemigrate-3.0-py3-none-any.whl.

File metadata

  • Download URL: django_safemigrate-3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.3 Linux/5.4.0-48-generic

File hashes

Hashes for django_safemigrate-3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b5c7ab7ae430f6dbd9e97bb1897ab7e90efc3687dc0a29b7d65914da5111486
MD5 a0e1070e7ece4753b49a0ec13521682a
BLAKE2b-256 033886a77c788d2b15575d490d6bf0dcf72762d14ba1b05d887dcad47d5c5e36

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page