Skip to main content

DuckDB migrations tool

Project description

DuckDB Flyway (migration manager)

A simple migration manager for DuckDB databases. It's called flyway to follow the obligatory duck-oriented naming conventions.

Features

  • Simple and lightweight Python-based migrations
  • Automatic migration discovery from directory
  • Transaction safety - each migration runs in its own transaction
  • Migration version validation ensures correct ordering
  • Customizable logging via standard Python logging

Installation

pip install duckdb-flyway

Usage

  1. Create a migrations directory in your project. Migration files must:
    • Start with 'm'
    • End with '.py'
    • Export a 'migration' object
    • Have unique, sortable IDs (typically timestamps)
migrations/
  m20240320000001_create_users.py
  m20240320000002_add_email.py
  1. Each migration file should export a migration object:
from duckdb_flyway import Migration
from duckdb import DuckDBPyConnection

def run(con: DuckDBPyConnection) -> None:
    """Create the users table.

    Args:
        con: DuckDB connection to use for the migration.
            Transaction management is handled automatically.
    """
    con.execute("""
        CREATE TABLE users (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            created_at TIMESTAMP DEFAULT now()
        );
    """)

# ID must be unique and sortable (typically a timestamp)
migration = Migration("20240320000001", run)
  1. Run migrations in your app:
import duckdb
from duckdb_flyway import DuckDBFlyway, MigrationError

try:
    # Connect to your database
    con = duckdb.connect("path/to/db.duckdb")

    # Create migrations service - migrations_dir is required
    flyway = DuckDBFlyway(con, migrations_dir="path/to/migrations")

    # Find and run all pending migrations
    flyway.find_and_run_migrations()

except MigrationError as e:
    print(f"Migration failed: {e}")
    # Handle migration failure

How it Works

  • Migrations are discovered from Python files in the migrations directory
  • Each migration runs in its own transaction for safety
  • Migrations are tracked in a schema_migrations table
  • New migrations must have higher IDs than previously applied ones
  • Failed migrations are rolled back automatically

Development

  1. Clone the repository and install dependencies:
git clone https://github.com/aluxian/duckdb-flyway.git
cd duckdb-flyway
uv venv
source .venv/bin/activate
uv sync
  1. Run linting checks:
uv run ruff check .
  1. Run tests:
uv run pytest
  1. Start Aider:
uvx --python 3.12 --from 'aider-chat[playwright]' --with 'aider-chat[help]' aider

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

duckdb_flyway-0.1.1.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

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

duckdb_flyway-0.1.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file duckdb_flyway-0.1.1.tar.gz.

File metadata

  • Download URL: duckdb_flyway-0.1.1.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for duckdb_flyway-0.1.1.tar.gz
Algorithm Hash digest
SHA256 266f77737c0f4eaa1697a4c4325882bb795cd4b29f8f4af078a6cbd2968a806f
MD5 04ef94680241d02234e3dc210d689d5d
BLAKE2b-256 fd2129be6493d8bec3ec34150a0a62df7e9141ea6a1d41ab54c6501c9a224a65

See more details on using hashes here.

Provenance

The following attestation bundles were made for duckdb_flyway-0.1.1.tar.gz:

Publisher: python-publish.yml on aluxian/duckdb-flyway

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

File details

Details for the file duckdb_flyway-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: duckdb_flyway-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for duckdb_flyway-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4d7e5b0b9ae7b590ed8b55ececaa8ce79c7e1be66bf7c0e1c4c7f74cdfb99229
MD5 e0490b80e90e7fd99085a9e1967907b2
BLAKE2b-256 50559dab36dde33af416270a32192fa222f59d08c7fa09752ac64a5f0e8b54a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for duckdb_flyway-0.1.1-py3-none-any.whl:

Publisher: python-publish.yml on aluxian/duckdb-flyway

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