Skip to main content

enables environment and state savvy DataMigration tools for Django

Project description

https://travis-ci.org/cgdeboer/environize.svg?branch=master https://img.shields.io/pypi/v/iteround.svg https://raw.githubusercontent.com/cgdeboer/environize/master/docs/environize.png

Environize enables environment and state savvy DataMigration tools for Django

  1. Decorators
    • adds a pair of decorators (only_in and except_in) for use with the migrations.RunPython method.

    • use django’s DataMigrations to allow dev data and prod data to be different.

    • exclude large data creation migrations from tests which may define their own data.

  2. Command(s)
    • adds a loaddata method that loads JSON fixtures into the database based on the state of the migration.

    • different from ./manage.py loaddata or call_command("loaddata"), which both load fixtures to the database based on the current state of models.

    • no more updating fixtures to keep them current. The data model in the fixture is tied to the state of your models in the migration.

Decorators - Usage

Exclude Tests

from django.db import migrations
import environize

# runs in every env except testing
@environize.except_in(envs=["test"])
def except_test_data(apps, schema_editor):
    Ham = apps.get_model("app", "Ham")
    Ham.objects.create(name="not-test")

@environize.except_in(envs=["test"])
def remove_hams(apps, schema_editor):
    Ham = apps.get_model("app", "Ham")
    Ham.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('app', '0002_auto_20180916_1122'),
    ]

    operations = [
        migrations.RunPython(except_test_data, remove_hams)
    ]

Production Only Data

from django.db import migrations
import environize

# runs in every env except testing
@environize.only_in(envs=["production"])
def add_prod_data(apps, schema_editor):
    Ham = apps.get_model("app", "Ham")
    Ham.objects.create(name="not-test")

@environize.only_in(envs=["production"])
def remove_hams(apps, schema_editor):
    Ham = apps.get_model("app", "Ham")
    Ham.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('app', '0002_auto_20180916_1122'),
    ]

    operations = [
        migrations.RunPython(add_prod_data, remove_hams)
    ]

Commands - Usage

import os
from django.db import migrations
import environize

PATH = 'path/to/fixtures/'

def load_fixture(apps, schema_editor):
    fixture_file = os.path.join(PATH, 'myfixture.json')
    environize.loaddata(apps, fixture_file)


class Migration(migrations.Migration):

    dependencies = [
        ('app', '0003_auto_20180916_1122'),
    ]

    operations = [
        migrations.RunPython(load_fixture, lambda x, y: None)
    ]

Feature Support

Environize officially supports:
  • Python 2.7 on Django 1.11 LTS

  • Python 3.5+ on Django 2+

Installation

To install Environize, use pipenv (or pip, of course):

$ pipenv install environize

Inside various settings files set an ENVIRONMENT variable.

# /settings_production.py
ENVIRONMENT = 'production'

# /settings_qa.py
ENVIRONMENT = 'qa'

# /settings_dev.py
ENVIRONMENT = 'dev'

The default env keys are listed below but can be overridden by setting ENVIRONIZE_ENVS in your django settings:

# these are the default env keys
ENVIRONIZE_ENVS = ("dev", "test", "ci", "qa", "staging", "production")

# by default this key will be used if one is not set in a settings file.
DEFAULT_ENV = "dev"

Documentation

TBD

How to Contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.

  2. Fork the repository on GitHub to start making your changes to the master branch (or branch off of it).

  3. Write a test which shows that the bug was fixed or that the feature works as expected.

  4. Send a pull request. Make sure to add yourself to AUTHORS.

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

environize-0.0.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

environize-0.0.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file environize-0.0.1.tar.gz.

File metadata

  • Download URL: environize-0.0.1.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.8 CPython/2.7.10

File hashes

Hashes for environize-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e2f1f55f245f408d9013e82126253981009571d1c9b21f6f4d4da12559b15c2e
MD5 e17819c928ee3c74a6b9b13d1017dc69
BLAKE2b-256 0ca4f1f864376b415e48093b8a2d40379e1229684b5bcc83aa105bb1d35f2e7e

See more details on using hashes here.

File details

Details for the file environize-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: environize-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.8 CPython/2.7.10

File hashes

Hashes for environize-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 41e1f23d5f9b7f8253354f544d7b76400ff7b7751eb6cb71b2523537b453ea0b
MD5 051fa3481436e289667a97dd9d3e5ef0
BLAKE2b-256 a395820effc5f174c9ddf45c762c45a48038ef61d54e0e1b1fa7c6aa7d53f22f

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