Skip to main content

A professional-grade CockroachDB wrapper with CRUD operations, migrations, and transaction management

Project description

Manticore CockroachDB

A professional-grade Python wrapper for CockroachDB, developed and maintained by Manticore Technologies. This package provides a clean and efficient interface for working with CockroachDB, featuring CRUD operations, schema migrations, and transaction management.

Features

  • 🔒 Secure connection management with SSL support
  • 🔄 Automatic transaction retries with exponential backoff
  • 📦 Easy CRUD operations with schema validation
  • 🔧 Database migrations with versioning
  • 🎯 Type hints and comprehensive test coverage
  • 📝 Detailed logging and error handling

Installation

pip install manticore-cockroachdb

Quick Start

from manticore_cockroachdb import init_db
from manticore_cockroachdb.crud import Table

# Initialize database connection
init_db("postgresql://user:password@host:26257/dbname?sslmode=verify-full")

# Define a table schema
users = Table("users", {
    "id": "UUID PRIMARY KEY DEFAULT gen_random_uuid()",
    "name": "TEXT NOT NULL",
    "email": "TEXT UNIQUE NOT NULL",
    "status": "TEXT DEFAULT 'active'",
    "created_at": "TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP"
})

# Create a record
user = users.create({
    "name": "Alice",
    "email": "alice@example.com"
})

# Read a record
found = users.read(user["id"])

# Update a record
updated = users.update(user["id"], {
    "status": "inactive"
})

# Delete a record
users.delete(user["id"])

Database Migrations

from manticore_cockroachdb.migrations import Migration, migrate

class CreateUsersTable(Migration):
    version = "20240217_01"
    name = "create_users_table"
    
    def up(self, cur):
        cur.execute("""
            CREATE TABLE users (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                name TEXT NOT NULL,
                email TEXT UNIQUE NOT NULL,
                created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
            )
        """)
    
    def down(self, cur):
        cur.execute("DROP TABLE users")

# Apply migrations
migrate([CreateUsersTable()])

Transaction Management

from manticore_cockroachdb import run_transaction

def transfer_funds(conn, from_id, to_id, amount):
    with conn.cursor() as cur:
        # Debit source account
        cur.execute(
            "UPDATE accounts SET balance = balance - %s WHERE id = %s",
            (amount, from_id)
        )
        # Credit destination account
        cur.execute(
            "UPDATE accounts SET balance = balance + %s WHERE id = %s",
            (amount, to_id)
        )

# Run with automatic retries
run_transaction(lambda conn: transfer_funds(conn, "id1", "id2", 100))

Development

  1. Clone the repository:
git clone https://github.com/manticore-tech/manticore-cockroachdb.git
cd manticore-cockroachdb
  1. Install development dependencies:
pip install -e ".[dev]"
  1. Set up test database:
# Start a local CockroachDB instance
docker run -d \
  --name=cockroach \
  -p 26257:26257 \
  -p 8080:8080 \
  cockroachdb/cockroach:latest \
  start-single-node \
  --insecure

# Set environment variable for tests
export DATABASE_URL="postgresql://root@localhost:26257/defaultdb?sslmode=disable"
  1. Run tests:
pytest
  1. Format code:
black .
isort .
  1. Run type checks:
mypy .
  1. Build package:
python -m build
  1. Run tests with coverage:
pytest --cov=manticore_cockroachdb --cov-report=term-missing

Test Coverage

The test suite aims to provide comprehensive coverage of the codebase. Current coverage levels:

  • __init__.py: 100% - Core module initialization
  • exceptions.py: 100% - Exception classes
  • database.py: 25% - Database connection management
    • Missing: Error paths and edge cases
    • TODO: Add tests for connection pool management
  • crud/table.py: 11% - Table operations
    • Missing: Error handling paths
    • TODO: Add tests for schema validation
    • TODO: Add tests for index management
  • crud/__init__.py: 50% - High-level CRUD operations
    • Missing: Error handling paths
    • TODO: Add tests for bulk operations

To improve coverage:

  1. Run tests with coverage report:
pytest --cov=manticore_cockroachdb --cov-report=html
  1. View detailed coverage report:
open htmlcov/index.html
  1. Focus on adding tests for:
    • Error handling paths
    • Edge cases
    • Concurrent operations
    • Connection pool management
    • Schema validation
    • Index management
    • Bulk operations

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

MIT License - Copyright (c) 2024 Manticore Technologies

Project details


Download files

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

Source Distribution

manticore_cockroachdb-0.1.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

manticore_cockroachdb-0.1.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file manticore_cockroachdb-0.1.0.tar.gz.

File metadata

  • Download URL: manticore_cockroachdb-0.1.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for manticore_cockroachdb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8d1fd977d378d874c882d797c00318b3f82e34bd5ba2ba8ebe932e984f60b25e
MD5 8a85075afe5bc73be60863193325e1e4
BLAKE2b-256 41787798dcc82b252c0b49fa9a9e79a2f4b6fb720d3a6cc87ccb25c5d6ffd02c

See more details on using hashes here.

File details

Details for the file manticore_cockroachdb-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for manticore_cockroachdb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5aa2250d935d5fca32d2d982aaa83ef01d46e2e8af8f343aa4d7693a7cab93b
MD5 55b6a1e8e69300a9dccefc99c752b7d2
BLAKE2b-256 af62ac06b2e5e4e63f6a66e63fb15652ddab5c364ee19616bfcca4d522168b5c

See more details on using hashes here.

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