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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_multiple_db-3.0.0.tar.gz (57.2 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_multiple_db-3.0.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_multiple_db-3.0.0.tar.gz.

File metadata

  • Download URL: sqlalchemy_multiple_db-3.0.0.tar.gz
  • Upload date:
  • Size: 57.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for sqlalchemy_multiple_db-3.0.0.tar.gz
Algorithm Hash digest
SHA256 3ca0835075a5f9bcb3e353fa2c727bee2c888cadbf79815a9e08d58645ce68ab
MD5 49248959fb3fad46765d6232df4c709d
BLAKE2b-256 fe821d212343f721e2febe91884acffb0123ee16b344a6bbd97a3aadc193654a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_multiple_db-3.0.0.tar.gz:

Publisher: publish.yml on bigbag/sqlalchemy-multiple-db

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

File details

Details for the file sqlalchemy_multiple_db-3.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_multiple_db-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01b737c8643e5dbefe2ed8587dc1f396e76c35496209926aca49e72bab5bb083
MD5 79148a2c1772a8fffb88d0f12cd7d61e
BLAKE2b-256 799c3a9d2d3f90460a2a2ea54583eacaab84ec8030d56d08bd07af7e047fa66b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_multiple_db-3.0.0-py3-none-any.whl:

Publisher: publish.yml on bigbag/sqlalchemy-multiple-db

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