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
FastAPIandSQLModelbut feasible with any other framework or puresqlalchemy - 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
- Spin up databases for local integration tests:
docker-compose -f tests/docker-compose.yaml up -d - Create virtual environment & install dependencies:
poetry install - 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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| database_setup_tools-1.3.1.tar.gz | 4.5 kB | Without provenance | Details for database_setup_tools-1.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| database_setup_tools-1.3.1-py3-none-any.whl | Python 3 | none | any | Details for database_setup_tools-1.3.1-py3-none-any.whl |
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
|
Security for database-setup-tools 1.3.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.