| Tool | Version | Purpose |
|---|---|---|
| Python | 3.11+ | Runtime |
| uv | 0.2+ | Package manager and workspace tool |
| Docker | 24+ | Running CockroachDB for integration tests |
| Git | 2.30+ | Version control |
git clone https://github.com/cockroachdb/cockroachdb-python-library.git
cd cockroachdb-python-libraryThis project uses uv workspaces to manage the monorepo. All six packages are declared in the root pyproject.toml.
# Create venv and install all workspace packages + dev dependencies
uv sync --all-packages
# Verify installation
uv run python -c "import cockroachdb; print(cockroachdb.__version__)"# Install only the base package
uv sync --package cockroachdb
# Install base + core
uv sync --package cockroachdb --package cockroachdb-coresource .venv/bin/activatecockroachdb-python-library/
├── pyproject.toml # Workspace root (uv, ruff, mypy, pytest config)
├── packages/
│ ├── cockroachdb/ # Base: decorators, types, config, exceptions
│ ├── cockroachdb-core/ # Engine: retry, circuit breaker, regions, middleware
│ ├── cockroachdb-sqlalchemy/ # SQLAlchemy 2.0 adapter
│ ├── cockroachdb-cdc/ # CDC changefeed webhook handlers
│ ├── cockroachdb-ai/ # Vector search, RAG, embeddings
│ └── cockroachdb-testing/ # Testcontainers fixtures, simulators
├── tests/
│ ├── unit/ # Unit tests (no CockroachDB required)
│ └── integration/ # Integration tests (requires Docker)
├── examples/ # Usage examples
└── docs/ # Architecture diagrams
# All unit tests
uv run pytest tests/unit/ -v
# Single package tests
uv run pytest tests/unit/test_decorators.py -v
# With coverage
uv run pytest tests/unit/ --cov=cockroachdb --cov-report=term-missingIntegration tests spin up a CockroachDB container via Testcontainers.
# Start Docker daemon first, then:
uv run pytest tests/integration/ -v
# Run a specific integration test
uv run pytest tests/integration/test_retry_integration.py -vuv run pytest -v# Check for lint errors
uv run ruff check .
# Auto-fix lint errors
uv run ruff check . --fix
# Format code
uv run ruff format .
# Check formatting without modifying
uv run ruff format . --check# Type check all packages
uv run mypy packages/
# Type check a single package
uv run mypy packages/cockroachdb/
# Strict mode is enabled by default in pyproject.tomluv run ruff check . && uv run ruff format . --check && uv run mypy packages/ && uv run pytest -vEach package builds independently using Hatch.
# Build a single package
cd packages/cockroachdb
uv build
# Build all packages from root
for pkg in packages/*/; do
echo "Building $(basename $pkg)..."
(cd "$pkg" && uv build)
done
# Built artifacts land in packages/<name>/dist/
ls packages/cockroachdb/dist/
# cockroachdb-0.1.0.tar.gz
# cockroachdb-0.1.0-py3-none-any.whlcd packages/cockroachdb
# Upload to Test PyPI
uv publish --index-url https://test.pypi.org/legacy/
# Verify install from Test PyPI
pip install --index-url https://test.pypi.org/simple/ cockroachdb# Ensure you have a PyPI token configured
# Option 1: environment variable
export UV_PUBLISH_TOKEN=pypi-xxxxx
# Option 2: pass directly
uv publish --token pypi-xxxxx
# Publish all packages (in dependency order)
for pkg in cockroachdb cockroachdb-core cockroachdb-sqlalchemy cockroachdb-cdc cockroachdb-ai cockroachdb-testing; do
echo "Publishing $pkg..."
(cd "packages/$pkg" && uv publish)
donePublish in this order to satisfy dependencies:
cockroachdb(base, no deps on other packages)cockroachdb-core(depends on cockroachdb)cockroachdb-sqlalchemy,cockroachdb-cdc,cockroachdb-ai(depend on core)cockroachdb-testing(depends on core)
Create examples/fastapi_demo.py:
"""Minimal FastAPI app with CockroachDB retry handling."""
from fastapi import FastAPI
from cockroachdb import crdb_retry
from cockroachdb.config import CockroachDBConfig
app = FastAPI()
config = CockroachDBConfig()
@app.get("/")
async def health():
return {"status": "ok", "retry_config": config.retry.model_dump()}
@app.post("/transfer")
@crdb_retry(max_retries=config.retry.max_retries)
async def transfer():
# Replace with real DB logic
return {"status": "transferred"}Run it:
uv add fastapi uvicorn
uv run uvicorn examples.fastapi_demo:app --reload --port 8000uv run python -c "
from cockroachdb import crdb_retry
from cockroachdb.config import CockroachDBConfig
config = CockroachDBConfig()
print(f'Retry config: {config.retry.model_dump()}')
print(f'Circuit breaker: {config.circuit_breaker.model_dump()}')
print(f'Region config: {config.region.model_dump()}')
"You are not running inside the uv virtualenv. Either:
- Prefix commands with
uv run - Activate the venv:
source .venv/bin/activate - Re-sync:
uv sync --all-packages
CockroachDBTemplate lives in cockroachdb-core, not the base cockroachdb package:
uv sync --package cockroachdb-corefrom cockroachdb.core import CockroachDBTemplate # correctEnsure Docker Desktop (or Docker Engine) is running:
docker info
docker run --rm cockroachdb/cockroach:latest versionAdd stubs or ignore rules in pyproject.toml:
[[tool.mypy.overrides]]
module = "psycopg2.*"
ignore_missing_imports = trueRuff's isort configuration takes precedence. The root pyproject.toml defines known-first-party = ["cockroachdb"]. If adding new third-party deps, ruff will auto-sort them.
Increase the Docker pull timeout or pre-pull the image:
docker pull cockroachdb/cockroach:latestEnsure all six packages are listed in the root pyproject.toml under [tool.uv.workspace]:
[tool.uv.workspace]
members = [
"packages/cockroachdb",
"packages/cockroachdb-core",
"packages/cockroachdb-sqlalchemy",
"packages/cockroachdb-cdc",
"packages/cockroachdb-ai",
"packages/cockroachdb-testing",
]Then re-sync:
uv sync --all-packages