feat(database): add MySQL storage backend integration (#163) #211
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements MySQL database integration for MemU, enabling memory data synchronization with MySQL for import, export, and backup use cases.
Changes
New Files
MySQL Database Module (src/memu/database/mysql/):
init.py - Module entry point with build_mysql_database() factory
mysql.py - MySQLStore class implementing the Database interface
session.py - SessionManager for MySQL connection handling
schema.py - SQLAlchemy model generation and caching
models.py - SQLModel definitions for MySQL tables
migration.py - DDL management (create/validate modes)
Repository Layer (src/memu/database/mysql/repositories/):
base.py - MySQLRepoBase with JSON embedding serialization
resource_repo.py - Resource CRUD operations
memory_category_repo.py - Category CRUD operations
memory_item_repo.py - Memory item CRUD with vector search
category_item_repo.py - Category-item relationship management
Documentation:
docs/mysql-integration.md - Setup guide, configuration, usage examples
Tests:
tests/test_mysql.py - 17 unit tests covering all components
Modified Files
src/memu/app/settings.py - Added mysql to MetadataStoreConfig.provider literal
src/memu/database/factory.py - Added MySQL provider branch in build_database()
Implementation Details
Architecture
Follows the existing Postgres pattern for consistency
Uses SQLModel/SQLAlchemy for ORM
Embeddings stored as JSON TEXT (MySQL lacks native vector type)
Brute-force cosine similarity for vector search
Configuration
from memu.app.settings import DatabaseConfig, MetadataStoreConfig
config = DatabaseConfig(
metadata_store=MetadataStoreConfig(
provider="mysql",
dsn="mysql+pymysql://user:password@localhost:3306/memu_db",
)
)
Database Schema
Creates 4 tables: resources, memory_items, memory_categories, category_items
Testing
All 17 tests pass:
pytest tests/test_mysql.py -v
==================================== 17 passed ====================================
Checklist
Complete MySQL integration that can be run in practice
Vector storage support (via JSON serialization + local cosine search)
Documentation with setup and usage instructions
Unit tests for all components
No breaking changes to existing functionality
Closes #163