Skip to main content

migrate-env

PyPI Tests Changelog License

Django-style migrations for environment files.

Your .env.example is the source of truth (like models.py). When it changes, makemigrations detects what was added and writes a numbered, reviewable migration. Each developer runs migrate against their own .env, which gains the new blocks and variables without ever touching the values they already have.

pip install migrate-env

Quick start

# Detect changes in .env.example and write env_migrations/0001_initial.py
migrate_env makemigrations

# Apply pending migrations to your local .env (creates it if missing)
migrate_env migrate .env

# See what's applied
migrate_env showmigrations .env

Later, someone adds a Postgres section to .env.example:

migrate_env makemigrations --name postgres
# Created env_migrations/0002_postgres.py
#   - Create assignment block [WHYHC_POSTGRES_DB, WHYHC_POSTGRES_USER, ...]

Everyone else pulls and runs migrate_env migrate .env. Their custom secret keys, hosts, and passwords are untouched; the new block is appended.

The data model

An env file is a sequence of blocks, and is byte-for-byte reconstructible from them:

  • CommentBlock — leading blank lines + comment lines.
  • AssignmentBlock — leading blank lines + comment lines + assignments.

An Assignment is (Key, Value, Comment | Blank) — the third element is the inline trailing comment (' # like this') or ''. A commented-out assignment such as # WHYHC_CONN_MAX_AGE=0 is parsed as an assignment with enabled=False, representing an optional variable. Prose comments that merely mention FOO=bar mid-sentence, or use spaces around =, remain comments.

from migrate_env import parse

env = parse(open(".env.example").read())
env.render() == open(".env.example").read()  # True, always

Migrations

Migrations are plain Python modules, ordered by numeric prefix with validated dependencies:

# env_migrations/0002_postgres.py
from migrate_env import migrations, ops

class Migration(migrations.Migration):
    dependencies = ['0001_initial']
    operations = [
        ops.CreateAssignmentBlock(
            comments=['# PostgreSQL database connection'],
            assignments=[
                ops.Assignment(key='WHYHC_POSTGRES_DB', value='whyhc'),
                ops.Assignment(key='WHYHC_POSTGRES_PORT', value='5432'),
            ],
        ),
    ]

Operations are additive and idempotent:

  • CreateAssignmentBlock / CreateCommentBlock — append a block (skipped if its content already exists).
  • AddAssignments — append variables to an existing block, located by an anchor key= or comment=. Keys the user already has anywhere in their file are skipped, so values are never overwritten.
  • AddComments — append documentation comments to an existing block.

Value changes and removals in .env.example are reported as warnings by makemigrations but never auto-migrated — users own their values, and destructive changes deserve a hand-written migration.

Applied state per target file lives in a sidecar (.env.migrations, the analog of Django's django_migrations table). Add it to .gitignore alongside .env.

Commands

Command Description
makemigrations [--example PATH] [--name N] [--dry-run] [--dir D] Diff the example against replayed migration state; write a migration
migrate TARGET [--plan] [--dir D] Apply unapplied migrations to TARGET
showmigrations TARGET [--dir D] List migrations with applied status

Development

To contribute to this library, first checkout the code. Then create a new virtual environment:

cd migrate-env
python -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

python -m pip install -e '.[dev]'

To run the tests:

python -m pytest

License

Apache 2.0

Metadata

Release files for migrate-env 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for migrate-env 0.3.0
File Size Uploaded Provenance Actions
migrate_env-0.3.0.tar.gz 21.2 kB With provenance Details for migrate_env-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for migrate-env 0.3.0
File Interpreter ABI Platform Actions
migrate_env-0.3.0-py3-none-any.whl Python 3 none any Details for migrate_env-0.3.0-py3-none-any.whl

Total release size: 40.3 kB

Release files / migrate_env-0.3.0.tar.gz

Download URL migrate_env-0.3.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ec3621d573009b137c3149b469d33c56cd9b932fdf82929a301c4a4cadb5f0c4
BLAKE2b-256 checksum
How to use checksums
9ba982dcb59ace27aa5c5de7fffedafba1be3ec5de2f0f0fe4eafaef2e320e1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 18, 2026.

Transparency log

Release files / migrate_env-0.3.0-py3-none-any.whl

Download URL migrate_env-0.3.0-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
857da7e65afe524cf4ab87f3c8a52e95a8f17c7479966d81a8e6288699682f3d
BLAKE2b-256 checksum
How to use checksums
34ee2841219e00cd8aa42dbc8867cebdc16929352e485e1dcab62533da54c3ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 18, 2026.

Transparency log

Security for migrate-env 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for migrate_env-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page