Skip to main content

This package provides validation for Alembic migrations.

Project description


pylembic

📝 Overview

This package provides validation of Alembic migrations for Python projects.

It will check:

  • Linearity: Ensures a clean and predictable migration chain.
  • Circular dependencies: Prevents migration failures due to loops in the dependency chain.
  • Orphan migrations: Identifies migrations improperly created without linking to any other migration.
  • Multiple bases/heads: Identifies multiple bases or heads in the migration graph.
  • Branching: Detects branching in the migration graph.
  • Graph visualization: Provides a visual way to catch anomalies and understand the migration flow.

📦 Installation

You can install this package using pip with different feature sets:

Standard Installation (Validation Only)

For basic migration validation without graph visualization:

pip install pylembic[standard]

Full Installation (All Features)

For complete functionality including graph visualization:

pip install pylembic[all]

Basic Installation

You can also install the basic package without optional dependencies:

pip install pylembic

Note: The show_graph functionality requires the [all] installation option as it depends on matplotlib. The core validation features (including branch detection) work with all installation options as they use networkx which is included by default.

⚙️ Usage

🧪 Testing

You can use this module with your preferred testing framework as follows:

from os import path

from pytest import fixture

from pylembic.validator import Validator


@fixture
def with_alembic_config_path():
    # We assume the migrations folder is at the root of the project,
    # and this test file is in the tests folder, also at the root of the project.
    # TODO: Feel free to adjust the path to your project's migrations folder.
    return path.abspath(
        path.join(path.dirname(path.dirname(__file__)), "migrations")
    )


def test_migrations(with_alembic_config_path):
    migration_validator = Validator(with_alembic_config_path)
    assert migration_validator.validate()

📊 Visualizing the migration graph

You can show the migrations graph by calling the method show_graph:

from os import path

from pylembic.validator import Validator

alembic_config_path = path.abspath(path.join("your path", "migrations"))

migration_validator = Validator(alembic_config_path)

migration_validator.show_graph()

💻 Command line interface

You can also use the command line for:

  • Validating migrations:

    pylembic ./path/to/migrations validate
    
  • Validating migrations with branch detection:

    pylembic ./path/to/migrations validate --detect-branches
    
  • Visualizing the migration graph:

    pylembic ./path/to/migrations show-graph
    

CLI is implemented using typer, so you can use the --help flag to get more information about the available options in every command.

  • Show general help:

    pylembic --help
    
  • Show help for a specific command:

    pylembic validate --help
    

⚠️ Caveats

📦 Using project imports in migrations

When you are using the command line interface to validate the migrations and you have specific imports from your project in the migrations, you will probably need to add the path to your project to the PYTHONPATH environment variable. Otherwise, the command line interface will not be able to find the modules.

🤝 Contributors

Contributions are welcome! Please feel free to submit a Pull Request.



📧 Contact

(c) 2025, Created with ❤️ by Marco Espinosa

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

pylembic-0.8.0.tar.gz (74.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylembic-0.8.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pylembic-0.8.0.tar.gz.

File metadata

  • Download URL: pylembic-0.8.0.tar.gz
  • Upload date:
  • Size: 74.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.15

File hashes

Hashes for pylembic-0.8.0.tar.gz
Algorithm Hash digest
SHA256 d3687a8832828908f7d44b5da59a6e52546506dda47b1e412346e2b2b9887c42
MD5 eb4efc78d856d0c36128ed6b7b2c2878
BLAKE2b-256 f50c61d7d93577c441ecd885681f830912e37d4ffb6c025ec7083e0084347b13

See more details on using hashes here.

File details

Details for the file pylembic-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: pylembic-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.15

File hashes

Hashes for pylembic-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd55998e35a430745ea11a9b9eac6ff7281b2273c8fb38d56837045d60597d04
MD5 f520a6865d548628cae87bd03a00e809
BLAKE2b-256 aab0e081a2e2de6a28ba31d3142c6ac248f1ac6e1e6adae508dddea0ccf0ec94

See more details on using hashes here.

Supported by

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