Skip to main content

Pydantic model migrations and schemas

Project description

pyrmute

ci pypi versions license

Pydantic model migrations and schema management with semantic versioning.

Pyrmute helps you evolve your data models over time without breaking changes. Version your Pydantic models, define migrations between versions, and automatically transform legacy data to current schemas. Export JSON schemas for all versions to maintain API contracts.

Key features:

  • Version your models - Use semantic versioning to track model evolution
  • Automatic migrations - Chain migrations across multiple versions (1.0.0 → 2.0.0 → 3.0.0)
  • Validated transformations - Migrations return validated Pydantic models by default
  • Schema export - Generate JSON schemas for all versions, with support for $ref to external schemas and custom schema generators
  • Nested model support - Automatically migrates nested Pydantic models

Help

See documentation for more details.

Installation

Install using pip install -U pyrmute.

Simple Example

from pydantic import BaseModel
from pyrmute import ModelManager, MigrationData

manager = ModelManager()


@manager.model("User", "1.0.0")
class UserV1(BaseModel):
    """Version 1.0.0: Initial user model."""
    name: str
    age: int


@manager.model("User", "2.0.0")
class UserV2(BaseModel):
    """Version 2.0.0: Split name into first/last."""
    first_name: str
    last_name: str
    age: int


@manager.model("User", "3.0.0")
class UserV3(BaseModel):
    """Version 3.0.0: Add email, make age optional."""
    first_name: str
    last_name: str
    email: str
    age: int | None = None


# Define migrations
@manager.migration("User", "1.0.0", "2.0.0")
def split_name(data: MigrationData) -> MigrationData:
    parts = data["name"].split(" ", 1)
    return {
        "first_name": parts[0],
        "last_name": parts[1] if len(parts) > 1 else "",
        "age": data["age"],
    }


@manager.migration("User", "2.0.0", "3.0.0")
def add_email(data: MigrationData) -> MigrationData:
    return {**data, "email": f"{data['first_name'].lower()}@example.com"}


# Migrate old data forward, from raw data or dumped from the Pydantic model
legacy_data = {"name": "John Doe", "age": 30}
# Returns a validated Pydantic model
current_user = manager.migrate(legacy_data, "User", "1.0.0", "3.0.0")

print(current_user)
# first_name='John' last_name='Doe' email='john@example.com' age=30

# Export schemas for all versions
manager.dump_schemas("schemas/")
# Creates: schemas/User_v1.0.0.json, schemas/User_v2.0.0.json, schemas/User_v3.0.0.json

Contributing

For guidance on setting up a development environment and how to make a contribution to pyrmute, see Contributing to pyrmute.

Reporting a Security Vulnerability

See our security policy.

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

pyrmute-0.1.0.tar.gz (51.3 kB view details)

Uploaded Source

Built Distribution

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

pyrmute-0.1.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file pyrmute-0.1.0.tar.gz.

File metadata

  • Download URL: pyrmute-0.1.0.tar.gz
  • Upload date:
  • Size: 51.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyrmute-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a678d416a937a10b089014f3b53154d54ba9f182cccdc981e7bde66517b43400
MD5 5ead4ee146a2f2629b1d89e3cf1efb2f
BLAKE2b-256 739bb2eddf12883e4f179aafb99e8234b1a1278d71d9e79af2b232464fc84c4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrmute-0.1.0.tar.gz:

Publisher: publish.yml on mferrera/pyrmute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrmute-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyrmute-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyrmute-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 69fc58421f9bf5cef6a337fb54ca597f2c775164794faac5f109addc9556328a
MD5 76f9c03a45c7ac91531f16f5633cd663
BLAKE2b-256 ddbe6d8fc917bd1daffb6fb3f01f49e824207ce45f024cde0c233aeccd182001

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrmute-0.1.0-py3-none-any.whl:

Publisher: publish.yml on mferrera/pyrmute

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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