Skip to main content

sqlalchemy-multiple-db

Explicit, typed SQLAlchemy 2.x session management for multiple named databases.

Requirements

  • Python 3.11, 3.12, 3.13, or 3.14
  • SQLAlchemy 2.x (installed automatically)

Installation

uv add sqlalchemy-multiple-db
# or
uv pip install sqlalchemy-multiple-db

Quick start

from sqlalchemy import text
from sqlalchemy_multiple_db import DatabaseConfig, DatabaseManager

configs = {
    "primary": DatabaseConfig(url="sqlite:///primary.db"),
    "analytics": DatabaseConfig(url="sqlite:///analytics.db"),
}

with DatabaseManager(configs) as databases:
    with databases.session("primary") as session:
        session.execute(text("CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY)"))
        session.execute(text("INSERT INTO events DEFAULT VALUES"))

    with databases.session("analytics") as session:
        session.execute(text("SELECT 1"))

    assert databases.healthcheck() == {"primary": True, "analytics": True}

Each session(name) call creates a new SQLAlchemy Session. A normal block commits; an exception rolls back and is re-raised; the session always closes. Call close() when not using the manager as a context manager. close() is idempotent, but the manager cannot be used afterward.

Configuration

DatabaseConfig(url, engine_options={}, session_options={}) forwards option mappings to SQLAlchemy's create_engine and sessionmaker. Options are copied at construction. The library deliberately does not choose pool sizes or deprecated autocommit behavior; configure dialect-specific options explicitly when needed.

healthcheck() runs SELECT 1 for every configured engine and returns a {name: bool} mapping. SQLAlchemy connection failures are logged and reported as False without stopping checks for other databases.

Migrating from 2.x

2.x 3.0
DBConfig(dsn="...") DatabaseConfig(url="...")
DBHelper or global db DatabaseManager(configurations)
setup() construct the manager
session_scope() session(name)
get_status_info() healthcheck()
shutdown() close() or with DatabaseManager(...)

There is no global manager or implicit default database in 3.0. Name every database explicitly.

Examples

Each example uses temporary SQLite files and needs no external database server:

Run an example from a synced checkout:

uv run python examples/basic_usage.py

Development

uv sync --all-groups
uv run pytest
uv run ruff format --check src tests
uv run ruff check src tests
uv run mypy src
uv build

Use uv add <package> to add a dependency and uv lock --upgrade to refresh all locked dependency versions. Commit every pyproject.toml and uv.lock change together.

License

Apache-2.0. See LICENSE.

Release files for sqlalchemy-multiple-db 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlalchemy-multiple-db 3.0.0
File Size Uploaded
sqlalchemy_multiple_db-3.0.0.tar.gz 57.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-multiple-db 3.0.0
File Interpreter ABI Platform
sqlalchemy_multiple_db-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 66.9 kB

Release files / sqlalchemy_multiple_db-3.0.0.tar.gz

Download URL sqlalchemy_multiple_db-3.0.0.tar.gz
Size 57.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3ca0835075a5f9bcb3e353fa2c727bee2c888cadbf79815a9e08d58645ce68ab
BLAKE2b-256 checksum
How to use checksums
fe821d212343f721e2febe91884acffb0123ee16b344a6bbd97a3aadc193654a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 20, 2026.

Transparency log

Release files / sqlalchemy_multiple_db-3.0.0-py3-none-any.whl

Download URL sqlalchemy_multiple_db-3.0.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
01b737c8643e5dbefe2ed8587dc1f396e76c35496209926aca49e72bab5bb083
BLAKE2b-256 checksum
How to use checksums
799c3a9d2d3f90460a2a2ea54583eacaab84ec8030d56d08bd07af7e047fa66b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 20, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page