Skip to main content

Versioned schema migrations for csrd database adapters

Project description

csrd-migration

Versioned, idempotent schema migrations for csrd.repository database adapters.

Quick start

from csrd.migration import Migration, MigrationRunner
from csrd.repository import SQLiteAdapter

MIGRATIONS = [
    Migration(
        version="001",
        description="create users table",
        up="CREATE TABLE users (id INTEGER PRIMARY KEY, username TEXT NOT NULL)",
        down="DROP TABLE users",
    ),
]

async def setup_db():
    adapter = SQLiteAdapter("app.db")
    await adapter.connect()
    runner = MigrationRunner()
    await runner.apply_all(adapter, MIGRATIONS)

API

Migration

Frozen dataclass representing a single schema change.

Field Type Description
version str Unique version identifier (e.g. "001", "002")
description str Human-readable description
up str SQL to apply the migration
down str SQL to revert the migration

MigrationRunner

Applies and tracks migrations against any ABCDatabaseAdapter.

Method Description
apply_all(adapter, migrations) Apply all pending migrations in order
rollback(adapter, migrations, *, to_version=None) Roll back applied migrations in reverse order. Pass to_version to stop at a specific version, or None to roll back all
reapply_all(adapter, migrations) Roll back all then re-apply — clean schema reset without dropping the database
current_version(adapter) Return the latest applied version, or None
pending(adapter, migrations) Return migrations not yet applied

The runner creates a _csrd_migrations table automatically to track applied versions. Re-running is idempotent — already-applied migrations are skipped.

Design

  • Adapter-agnostic — works with SQLiteAdapter, PGAdapter, MariaAdapter
  • Idempotent — safe to call on every startup
  • Ordered — migrations are applied in list order
  • Future-ready — the Migration dataclass is the stable interface for a future model→SQL translation layer (see docs/AUGMENT_PLAN.md)

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

csrd_migration-0.5.4.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

csrd_migration-0.5.4-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file csrd_migration-0.5.4.tar.gz.

File metadata

  • Download URL: csrd_migration-0.5.4.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for csrd_migration-0.5.4.tar.gz
Algorithm Hash digest
SHA256 41fd3f667866c25ce9025241b7aaedc42a00e5e18f59e5a92fa7d0bdb200232c
MD5 7d34235679dbc7eb51b909583fe451da
BLAKE2b-256 85a7341c5d483bf669068d926eb296a61fc426002507f903e0463f2ff49b26fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for csrd_migration-0.5.4.tar.gz:

Publisher: release.yml on csrd-api/fastapi-common

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

File details

Details for the file csrd_migration-0.5.4-py3-none-any.whl.

File metadata

  • Download URL: csrd_migration-0.5.4-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for csrd_migration-0.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2722a0ccad14a641d24b84724bdbe6c4a8069fdea82e10fc710d3015f711ac35
MD5 075f517f9fa6126417de81e46a78a6bc
BLAKE2b-256 2b62525294bcd0d0e8eca115dc036c8472c56fd96b40c43da39bdd35f2208459

See more details on using hashes here.

Provenance

The following attestation bundles were made for csrd_migration-0.5.4-py3-none-any.whl:

Publisher: release.yml on csrd-api/fastapi-common

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