Automatic Alembic migration chaining based on git commit history
Project description
alembic-git-revisions
Automatic Alembic migration chaining based on git commit history.
The problem
When multiple developers create Alembic migrations on separate branches, they often end up with the same down_revision — the current head at the time each branch was created. When these branches merge, Alembic fails with a MultipleHeads error because two migrations point to the same predecessor.
The usual fix is manual: rebase, update down_revision, and hope nobody else merges in the meantime.
How it works
Instead of hardcoding down_revision, this library determines the migration chain automatically from git history. It uses git log --reverse --diff-filter=A to find the order in which migration files were first committed, then chains them linearly after the last "static" (hardcoded) migration.
This means:
- New migrations never conflict with each other
- The chain is always linear, regardless of branch merge order
- Existing migrations with hardcoded
down_revisioncontinue to work
Installation
pip install alembic-git-revisions
Setup
Copy the provided template to your Alembic script.py.mako:
"""${message}
Revision ID: ${up_revision}
Create Date: ${create_date}
"""
from alembic import op
from alembic_git_revisions import get_down_revision
import sqlalchemy
${imports if imports else ""}
revision = ${repr(up_revision)}
down_revision = get_down_revision(revision)
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
A reference template is included in the package at alembic_git_revisions/templates/script.py.mako.
That's it. New migrations generated with alembic revision --autogenerate will automatically chain themselves using git history.
Environments without git (Docker, CI)
In Docker images or CI environments where git history isn't available, pre-generate a revision_chain.json file before building:
# Using the CLI
alembic-git-revisions /path/to/alembic/versions
# Or as a Python module
python -m alembic_git_revisions /path/to/alembic/versions
This writes revision_chain.json next to the versions/ directory. The library uses this file automatically when it exists, falling back to git when it doesn't.
Important: The git clone must have full history (git clone or actions/checkout with fetch-depth: 0). Shallow clones produce incorrect ordering.
Add revision_chain.json to your .gitignore — it should only exist in built artifacts.
How migrations are classified
The library handles three types of migrations:
- Dynamic — uses
get_down_revision(), chained automatically by git history - Static — has a hardcoded
down_revision, managed manually (legacy migrations) - Hybrid — a static migration whose
down_revisionpoints to a dynamic one; participates in the dynamic ordering so the chain stays linear
API
get_down_revision(revision, versions_dir=None)
Returns the down_revision for the given revision ID. Auto-discovers the versions directory from the calling migration file's location. Pass versions_dir explicitly for non-standard setups or tests.
generate_chain_file(versions_dir)
Generates revision_chain.json from git history. Run this before building Docker images.
build_chain(versions_dir)
Returns the full {revision: down_revision} dict. Cached per versions_dir. Use build_chain.cache_clear() to reset in tests.
License
Apache-2.0
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file alembic_git_revisions-7.tar.gz.
File metadata
- Download URL: alembic_git_revisions-7.tar.gz
- Upload date:
- Size: 36.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93c541278dca4f778df8f753f07c9434b4f73957f2819dc1f4f3ca3e8b1e98fb
|
|
| MD5 |
5ea4dd33dc4068c1b9635c590cf9749e
|
|
| BLAKE2b-256 |
9db243091563138995e38762d39e592d54f325f7aede755743fdfb9d4ab43a75
|
Provenance
The following attestation bundles were made for alembic_git_revisions-7.tar.gz:
Publisher:
release.yaml on Mergifyio/alembic-git-revisions
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
alembic_git_revisions-7.tar.gz -
Subject digest:
93c541278dca4f778df8f753f07c9434b4f73957f2819dc1f4f3ca3e8b1e98fb - Sigstore transparency entry: 1437671053
- Sigstore integration time:
-
Permalink:
Mergifyio/alembic-git-revisions@8880b83ac1f94e8b6097ec0b7bc71fcb38e17ddd -
Branch / Tag:
refs/tags/v7 - Owner: https://github.com/Mergifyio
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@8880b83ac1f94e8b6097ec0b7bc71fcb38e17ddd -
Trigger Event:
push
-
Statement type:
File details
Details for the file alembic_git_revisions-7-py3-none-any.whl.
File metadata
- Download URL: alembic_git_revisions-7-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04ff46a365ccdf2b6b2c88b063a8c6088eecf2ae3d2d88f840825a4311ae3c66
|
|
| MD5 |
7c666c64fa665c690f2b0bac93779724
|
|
| BLAKE2b-256 |
c6969b216b1ce3bd8f7bf71d5d33e4b734c5e2492980fdef3ee3d713c36bef83
|
Provenance
The following attestation bundles were made for alembic_git_revisions-7-py3-none-any.whl:
Publisher:
release.yaml on Mergifyio/alembic-git-revisions
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
alembic_git_revisions-7-py3-none-any.whl -
Subject digest:
04ff46a365ccdf2b6b2c88b063a8c6088eecf2ae3d2d88f840825a4311ae3c66 - Sigstore transparency entry: 1437671056
- Sigstore integration time:
-
Permalink:
Mergifyio/alembic-git-revisions@8880b83ac1f94e8b6097ec0b7bc71fcb38e17ddd -
Branch / Tag:
refs/tags/v7 - Owner: https://github.com/Mergifyio
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@8880b83ac1f94e8b6097ec0b7bc71fcb38e17ddd -
Trigger Event:
push
-
Statement type: