Skip to main content

No project description provided

Project description

Build Status Coverage status

Django application providing database migration tooling to automate their deployment.

Inspired by a 2015 post from Ludwig Hähne and experience dealing with migration at Zapier.

Installation

pip install django-syzygy

Usage

Add 'syzygy' to your INSTALLED_APPS

# settings.py
INSTALLED_APPS = [
    ...
    'syzygy',
    ...
]

Setup you deployment pipeline to run migrate --pre-deploy before rolling out your code changes and migrate afterwards to apply the postponed migrations.

Concept

Syzygy introduces a notion of prerequisite and postponed migrations with regards to deployment.

A migration is assumed to be a prerequisite to deployment unless it contains a destructive operation or the migration has its stage class attribute set to Stage.POST_DEPLOY. When this attribute is defined it will bypass operations based heuristics.

e.g. this migration would be considered a prerequisite

class Migration(migrations.Migration):
    operations = [
        AddField('model', 'field', models.IntegerField(null=True))
    ]

while the following migrations would be postponed

class Migration(migrations.Migration):
    operations = [
        RemoveField('model', 'field'),
    ]
from syzygy import Stage

class Migration(migrations.Migration):
    stage = Stage.POST_DEPLOY

    operations = [
        RunSQL(...),
    ]

In order to prevent the creation of migrations mixing operations of different stages this package registers system checks. These checks will generate an error for every migration with an ambiguous stage.

e.g. this migration would result in a check error

class Migration(migrations.Migration):
    operations = [
        AddField('model', 'other_field', models.IntegerField(null=True)),
        RemoveField('model', 'field'),
    ]

Migration revert are also supported and result in inverting the nature of migrations. A migration that is normally considered a prerequisite would then be postponed when reverted.

With this new notion of migration stage it’s possible for the migrate command to target only migrations meant to be run before a deployment using the –pre-deploy flag or error out in the case on an ambiguous plan.

Third-party migrations

As long as the adoption of migration stages concept not generalized your project might depend on third-party apps containing migrations with an ambiguous sequence of operations.

Since an explicit stage cannot be explicitly assigned by editing these migrations a fallback or an override stage can be specified through the respective MIGRATION_STAGES_FALLBACK and MIGRATION_STAGES_OVERRIDE settings.

By default third-party app migrations with an ambiguous sequence of operations will fallback to Stage.PRE_DEPLOY but this behavior can be changed by setting MIGRATION_THIRD_PARTY_STAGES_FALLBACK to Stage.POST_DEPLOY or disabled by setting it to None.

Migration quorum

When deploying migrations to multiple clusters sharing the same database it’s important that:

  1. Migrations are applied only once

  2. Pre-deployment migrations are applied before deployment in any clusters is takes place

  3. Post-deployment migrations are only applied once all clusters are done deploying

The built-in migrate command doesn’t offer any guarantees with regards to serializability of invocations, in other words naively calling migrate from multiple clusters before or after a deployment could cause some migrations to be attempted to be applied twice.

To circumvent this limitation Syzygy introduces a --quorum <N:int> flag to the migrate command that allow clusters coordination to take place.

When specified the migrate --quorum <N:int> command will wait for at least N number invocations of migrate for the planned migrations before proceeding with applying them once and blocking on all callers until the operation completes.

In order to use the --quorum feature you must configure the MIGRATE_QUORUM_BACKEND setting to point to a quorum backend such as cache based one provided by Sygyzy

MIGRATE_QUORUM_BACKEND = 'syzygy.quorum.backends.cache.CacheQuorum'

or

CACHES = {
    ...,
    'quorum': {
        ...
    },
}
MIGRATE_QUORUM_BACKEND = {
    'backend': 'syzygy.quorum.backends.cache.CacheQuorum',
    'alias': 'quorum',
}

Development

Make your changes, and then run tests via tox:

tox

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-syzygy-0.1.0a8.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

django_syzygy-0.1.0a8-py2.py3-none-any.whl (19.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-syzygy-0.1.0a8.tar.gz.

File metadata

  • Download URL: django-syzygy-0.1.0a8.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django-syzygy-0.1.0a8.tar.gz
Algorithm Hash digest
SHA256 cb2edf8f1a1d8319291496ae349a58860655bc2f955a50641d2a703da18b5e4d
MD5 156e1caa266d3d4e788065e079e5b77f
BLAKE2b-256 9fafd0e3fb7fbc63cdb22736630525fe0c72a0ba9506544d7f8ca4f6b578326d

See more details on using hashes here.

File details

Details for the file django_syzygy-0.1.0a8-py2.py3-none-any.whl.

File metadata

  • Download URL: django_syzygy-0.1.0a8-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django_syzygy-0.1.0a8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bcb6115a3d815e6b182556b10d5644426b075ae033ac9878775ae438604f94ee
MD5 903fd39c02226425923b91406ca38f9d
BLAKE2b-256 48a1088d6751ae06d4e449d7c0647bfb4d5d8b38bea0309d1f53b757a882c58a

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