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
- 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
- Each migration file should export a
migrationobject:
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)
- 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_migrationstable - New migrations must have higher IDs than previously applied ones
- Failed migrations are rolled back automatically
Development
- 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
- Run linting checks:
uv run ruff check .
- Run tests:
uv run pytest
- Start Aider:
uvx --python 3.12 --from 'aider-chat[playwright]' --with 'aider-chat[help]' aider
Project details
Release history Release notifications | RSS feed
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
266f77737c0f4eaa1697a4c4325882bb795cd4b29f8f4af078a6cbd2968a806f
|
|
| MD5 |
04ef94680241d02234e3dc210d689d5d
|
|
| BLAKE2b-256 |
fd2129be6493d8bec3ec34150a0a62df7e9141ea6a1d41ab54c6501c9a224a65
|
Provenance
The following attestation bundles were made for duckdb_flyway-0.1.1.tar.gz:
Publisher:
python-publish.yml on aluxian/duckdb-flyway
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
duckdb_flyway-0.1.1.tar.gz -
Subject digest:
266f77737c0f4eaa1697a4c4325882bb795cd4b29f8f4af078a6cbd2968a806f - Sigstore transparency entry: 157360016
- Sigstore integration time:
-
Permalink:
aluxian/duckdb-flyway@aa3e672b4754ca0f33b4449f08001c9f2aaf4bcb -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/aluxian
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@aa3e672b4754ca0f33b4449f08001c9f2aaf4bcb -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d7e5b0b9ae7b590ed8b55ececaa8ce79c7e1be66bf7c0e1c4c7f74cdfb99229
|
|
| MD5 |
e0490b80e90e7fd99085a9e1967907b2
|
|
| BLAKE2b-256 |
50559dab36dde33af416270a32192fa222f59d08c7fa09752ac64a5f0e8b54a3
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
duckdb_flyway-0.1.1-py3-none-any.whl -
Subject digest:
4d7e5b0b9ae7b590ed8b55ececaa8ce79c7e1be66bf7c0e1c4c7f74cdfb99229 - Sigstore transparency entry: 157360018
- Sigstore integration time:
-
Permalink:
aluxian/duckdb-flyway@aa3e672b4754ca0f33b4449f08001c9f2aaf4bcb -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/aluxian
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@aa3e672b4754ca0f33b4449f08001c9f2aaf4bcb -
Trigger Event:
release
-
Statement type: