Skip to main content

Database tools

Easy to understand and use tools that help you to create databases and interact with them.

Installation

pip install database-setup-tools

Features

  • Database creation on app startup
  • Thread-safe database session manager
  • Opinionated towards FastAPI and SQLModel but feasible with any other framework or pure sqlalchemy
  • Easily use a local database in your tests

Planned features

  • Database migrations with Alembic

Example

import random

import uvicorn
from fastapi import FastAPI, Depends
from sqlmodel import Session, SQLModel, Field

from database_setup_tools.session_manager import SessionManager
from database_setup_tools.setup import DatabaseSetup

DATABASE_URI = "sqlite:///test.db"

app = FastAPI()
session_manager = SessionManager(database_uri=DATABASE_URI)


class User(SQLModel, table=True):
    """ User model """
    id: int = Field(index=True, primary_key=True)
    name: str


model_metadata = SQLModel.metadata


@app.post('/users/', response_model=User)
def add_random_user(session: Session = Depends(session_manager.get_session)):
    """ Endpoint to add a user with a random name """
    user = User(name=f'User {random.randint(0, 100)}')
    session.add(user)
    session.commit()
    return user


@app.get('/users/', response_model=list[User])
def get_all_users(session: Session = Depends(session_manager.get_session)):
    """ Endpoint to get all users """
    return session.query(User).all()


if __name__ == '__main__':
    database_setup = DatabaseSetup(model_metadata=model_metadata, database_uri=DATABASE_URI)
    uvicorn.run(app, host='0.0.0.0', port=8080)

Example for pytest

conftest.py

database_setup = DatabaseSetup(model_metadata=model_metadata, database_uri=DATABASE_URI)


def pytest_sessionstart(session):
    database_setup.drop_database()
    database_setup.create_database()

test_users.py

session_manager = SessionManager(database_uri=DATABASE_URI)


@pytest.fixture
def session():
    with session_manager.get_session() as session:
        yield session


def test_create_user(session: Session):
    user = User(name='Test User')
    session.add(user)
    session.commit()
    assert session.query(User).count() == 1
    assert session.query(User).first().name == 'Test User'

Development

Testing

  1. Spin up databases for local integration tests: docker-compose -f tests/docker-compose.yaml up -d
  2. Create virtual environment & install dependencies: poetry install
  3. Run tests: poetry run pytest

Metadata

Release files for database-setup-tools 1.3.1

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

Source distribution (sdist)

Source distribution for database-setup-tools 1.3.1
File Size Uploaded
database_setup_tools-1.3.1.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for database-setup-tools 1.3.1
File Interpreter ABI Platform
database_setup_tools-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / database_setup_tools-1.3.1.tar.gz

Download URL database_setup_tools-1.3.1.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
16a94eafe2ea39051a8f4be108544fb9eebcffe1562033d607468b5772ad5e66
BLAKE2b-256 checksum
How to use checksums
1a2ab636cd37e2fd77df83d38f8b6624a8a6a2ffa69f338da49cd90e3e304bdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-1035-azure

Release files / database_setup_tools-1.3.1-py3-none-any.whl

Download URL database_setup_tools-1.3.1-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02b2bcb9f4240406baf89d1e929313ab442571ed4a56cc19f51166089f688ebb
BLAKE2b-256 checksum
How to use checksums
2f272c1924b73427de9437edb8e238f833f4d83e7d037c89c1b013feeb74680a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-1035-azure

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

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