Skip to main content

Database migration system for Python/SQLAlchemy projects

Project description

DBWarden

Everything you loved about Django migrations, in FastAPI.

PyPI version Python versions License


Documentation: DBWarden

Source Code: https://github.com/emiliano-gandini-outeda/DBWarden


DBWarden is a database toolkit for FastAPI and SQLAlchemy projects. It handles migrations, async sessions, startup validation, and health checks: all from a single configuration source.

Experimental: DBWarden is under active development. Breaking changes may occur between versions.

Key Features

  • SQL-first: Migrations are plain SQL. What you write is exactly what runs against your database. No DSL, no surprises.
  • Rollback included: Every migration carries both upgrade and rollback SQL. Rolling back is a first-class operation, not an afterthought.
  • FastAPI-native: Async session dependency, lifespan helpers, and a mountable health router designed around FastAPI's patterns.
  • One config, everything: The same database_config(...) call that defines your migrations also drives your sessions and health checks. No split configs.
  • Dev mode: Run SQLite locally against a PostgreSQL production schema. DBWarden translates the SQL automatically.
  • Multi-database: Configure and migrate multiple databases from a single project, with full isolation and uniqueness guarantees.
  • Safe by default: Migration locking, checksum integrity, collision detection, and schema drift checks before you deploy. DBWarden protects your database, from accidents and from itself.

Requirements

  • Python 3.10+
  • SQLAlchemy

Installation

pip install dbwarden

With FastAPI integration:

pip install "dbwarden[fastapi]"

Quickstart

1. Configure

Run dbwarden init to scaffold your config file, then edit dbwarden.py:

from dbwarden import database_config

database_config(
    database_name="primary",
    default=True,
    database_type="postgresql",
    database_url="postgresql://user:password@localhost:5432/myapp",
    dev_database_type="sqlite",
    dev_database_url="sqlite:///./dev.db",
)

2. Define your models

from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    email = Column(String(255), unique=True, nullable=False)

3. Generate a migration

dbwarden make-migrations "create users table"

DBWarden creates a versioned SQL file with both upgrade and rollback:

-- upgrade

CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE
);

-- rollback

DROP TABLE users;

4. Apply

dbwarden migrate

5. Check status

dbwarden status

That's it. Your schema is versioned, reviewable, and reversible.


FastAPI Integration

from contextlib import asynccontextmanager
from typing import Annotated

from fastapi import Depends, FastAPI
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from dbwarden.fastapi import DBWardenHealthRouter, get_session, migration_context


@asynccontextmanager
async def lifespan(app: FastAPI):
    async with migration_context(mode="check"):
        yield


app = FastAPI(lifespan=lifespan)
app.include_router(DBWardenHealthRouter(), prefix="/health")

SessionDep = Annotated[AsyncSession, Depends(get_session())]


@app.get("/users")
async def list_users(session: SessionDep):
    result = await session.execute(select(User))
    return result.scalars().all()

One config source. Sessions, health checks, and startup validation, all handled.


Multi-Database

from dbwarden import database_config

database_config(
    database_name="primary",
    default=True,
    database_type="postgresql",
    database_url="postgresql://user:password@localhost:5432/main",
    model_paths=["models/primary"],
)

database_config(
    database_name="analytics",
    database_type="postgresql",
    database_url="postgresql://user:password@localhost:5432/analytics",
    model_paths=["models/analytics"],
)
dbwarden migrate --all

Dev Mode

Use SQLite locally while targeting PostgreSQL in production. DBWarden translates backend-specific SQL automatically:

dbwarden --dev migrate

The dev_database_type and dev_database_url fields in your config define the local target. Your migration files stay unchanged.


Supported Databases

Database database_type value
PostgreSQL postgresql
MySQL mysql
MariaDB mariadb
SQLite sqlite
ClickHouse clickhouse

License

MIT

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

dbwarden-0.7.tar.gz (553.8 kB view details)

Uploaded Source

Built Distribution

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

dbwarden-0.7-py3-none-any.whl (69.5 kB view details)

Uploaded Python 3

File details

Details for the file dbwarden-0.7.tar.gz.

File metadata

  • Download URL: dbwarden-0.7.tar.gz
  • Upload date:
  • Size: 553.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for dbwarden-0.7.tar.gz
Algorithm Hash digest
SHA256 cd6c4d2eaa262094670e315022fad2b5e667ba34ba25a3b9f718b364498ee595
MD5 8d08d58ef3595cb45c89eeb7220d71f4
BLAKE2b-256 6fe61569afbbcc4d3235778ac721f0e9ee94a4964770d40ef8ffe50a62a1f79a

See more details on using hashes here.

File details

Details for the file dbwarden-0.7-py3-none-any.whl.

File metadata

  • Download URL: dbwarden-0.7-py3-none-any.whl
  • Upload date:
  • Size: 69.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for dbwarden-0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 ce7126335d252e345b5910bdcf396e47190ad851abf65b7292888812d5e1311d
MD5 5ab2c04f4f94bf4c59e7c7277788f3d4
BLAKE2b-256 7a22951a84bb684be91c827309a14d3d06ca27fe73fe198d0e13f58cd6423690

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