Skip to main content

MySQL backend implementation for rhosocial-activerecord, providing a robust and optimized MySQL database support.

Project description

rhosocial-activerecord-mysql ($\rho_{\mathbf{AR}\text{-mysql}}$)

PyPI version Python Tests Coverage Status Apache 2.0 License Powered by vistart

rhosocial ActiveRecord Logo

MySQL Backend for rhosocial-activerecord

Production-Ready MySQL Support · Sync & Async · Native Driver Integration

Note: This is a backend implementation for rhosocial-activerecord. It cannot be used standalone.

Why This Backend?

1. MySQL-Specific Optimizations

Feature This Backend Generic Solutions
Full-Text Search Native MATCH ... AGAINST LIKE-based workarounds
JSON Operations JSON_EXTRACT, ->>, -> Serialize/deserialize overhead
Upsert ON DUPLICATE KEY UPDATE Manual check-then-insert
Connection Pooling Built-in with mysql-connector External pooling required

2. True Sync-Async Parity

Same API surface for both sync and async operations:

# Sync
users = User.query().where(User.c.age >= 18).all()

# Async - just add await
users = await User.query().where(User.c.age >= 18).all()

3. Built for Production

  • Connection pooling with configurable pool sizes
  • Transaction support with proper isolation levels
  • Error mapping from MySQL error codes to Python exceptions
  • Type adapters for MySQL-specific data types

Quick Start

Installation

pip install rhosocial-activerecord-mysql

Basic Usage

from rhosocial.activerecord.model import ActiveRecord
from rhosocial.activerecord.backend.impl.mysql import MySQLBackend
from rhosocial.activerecord.backend.impl.mysql.config import MySQLConnectionConfig
from typing import Optional

class User(ActiveRecord):
    __table_name__ = "users"
    id: Optional[int] = None
    name: str
    email: str

# Configure
config = MySQLConnectionConfig(
    host="localhost",
    port=3306,
    database="myapp",
    username="user",
    password="password"
)
User.configure(config, MySQLBackend)

# Use
user = User(name="Alice", email="alice@example.com")
user.save()

# Query with MySQL full-text search
results = User.query().where(
    "MATCH(name, email) AGAINST(? IN BOOLEAN MODE)",
    ("+Alice",)
).all()

💡 AI Prompt: "Show me how to use JSON operations in MySQL with this backend"

MySQL-Specific Features

Full-Text Search

Native MySQL full-text search support:

# Boolean mode full-text search
Article.query().where(
    "MATCH(title, content) AGAINST(? IN BOOLEAN MODE)",
    ("+python -java",)
).all()

# Natural language mode
Article.query().where(
    "MATCH(title, content) AGAINST(?)",
    ("database optimization",)
).all()

JSON Operations

Query JSON columns using MySQL's native JSON functions:

# Extract JSON value
User.query().where("settings->>'$.theme' = ?", ("dark",)).all()

# JSON contains
Product.query().where("JSON_CONTAINS(tags, ?)", ('"featured"',)).all()

Upsert (ON DUPLICATE KEY UPDATE)

Efficient insert-or-update operations:

# Will update on duplicate key
User.insert_or_update(
    name="Alice",
    email="alice@example.com",
    update_fields=["name"]  # Only update name on conflict
)

Requirements

  • Python: 3.8+ (including 3.13t/3.14t free-threaded builds)
  • Core: rhosocial-activerecord>=1.0.0
  • Driver: mysql-connector-python>=9.0.0

Get Started with AI Code Agents

This project supports AI-assisted development. Clone and open in your preferred tool:

git clone https://github.com/rhosocial/python-activerecord-mysql.git
cd python-activerecord-mysql

Example AI Prompts

  • "How do I configure connection pooling for MySQL?"
  • "Show me the differences between MySQL and PostgreSQL backends"
  • "How do I use MySQL-specific JSON operators?"
  • "Create a model with a FULLTEXT index"

For Any LLM

Feed the documentation files in docs/ to your preferred LLM for context-aware assistance.

Testing

⚠️ CRITICAL: Tests MUST run serially. Do NOT use pytest -n auto or parallel execution.

# Run all tests
PYTHONPATH=src pytest tests/

# Run specific feature tests
PYTHONPATH=src pytest tests/rhosocial/activerecord_mysql_test/feature/basic/
PYTHONPATH=src pytest tests/rhosocial/activerecord_mysql_test/feature/query/

See the Testing Documentation for details.

Documentation

Comparison with Other Backends

Feature MySQL PostgreSQL SQLite
Full-Text Search ✅ Native ✅ Native ⚠️ FTS5 extension
JSON Type ✅ JSON ✅ JSONB ⚠️ JSON1 extension
Arrays ✅ Native
Upsert ✅ ON DUPLICATE KEY ✅ ON CONFLICT ✅ ON CONFLICT
Returning ✅ RETURNING ✅ RETURNING

💡 AI Prompt: "When should I choose MySQL over PostgreSQL for my project?"

Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

License

Apache License 2.0 — Copyright © 2026 vistart


Built with ❤️ by the rhosocial team

GitHub · Documentation · PyPI

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

rhosocial_activerecord_mysql-1.0.0.dev11.tar.gz (92.6 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file rhosocial_activerecord_mysql-1.0.0.dev11.tar.gz.

File metadata

File hashes

Hashes for rhosocial_activerecord_mysql-1.0.0.dev11.tar.gz
Algorithm Hash digest
SHA256 7f71cbea1597eb5187b15a5859ac5abce72c361d3d4be4b25ca1f9ea034ffa80
MD5 af94238eb42d0342cb04ab0e588ae019
BLAKE2b-256 2b7b0b96ece865dcdda0b6f3ac12d464c1115f810fe6fdcca11b3cb1eeb74f0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rhosocial_activerecord_mysql-1.0.0.dev11.tar.gz:

Publisher: publish.yml on rhosocial/python-activerecord-mysql

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

File details

Details for the file rhosocial_activerecord_mysql-1.0.0.dev11-py3-none-any.whl.

File metadata

File hashes

Hashes for rhosocial_activerecord_mysql-1.0.0.dev11-py3-none-any.whl
Algorithm Hash digest
SHA256 030dffff5e692f359360b93788aa0c40162d4cd80b0f0b3fd57ce8bc0f272269
MD5 8d70531fc4415a358d8cdb79261cb1f8
BLAKE2b-256 72b11425b5cf1e370f8ef773fcd0d93f896793ead7365c9988bed8aaeb4bf56f

See more details on using hashes here.

Provenance

The following attestation bundles were made for rhosocial_activerecord_mysql-1.0.0.dev11-py3-none-any.whl:

Publisher: publish.yml on rhosocial/python-activerecord-mysql

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