Dynamic Migration System
The SQL plugin uses a dynamic migration service that automatically creates and updates database tables based on plugin schemas. This eliminates the need for traditional migration files.How It Works
- Plugin Registration - Plugins export their schema definitions
- Schema Discovery - The migration service discovers all plugin schemas at startup
- Schema Introspection - The system analyzes existing database tables
- Dynamic Migration - Tables are created or updated as needed
- Dependency Resolution - Tables are created in the correct order based on foreign key dependencies
Key Components
Defining Plugin Schemas
To enable automatic schema management, plugins must export their Drizzle schema definitions:Plugin Structure
Core Schema Tables
The SQL plugin provides these core tables that all agents use:Agent Tables
agents
- Core agent identitymemories
- Agent memory storageentities
- People, objects, and conceptsrelationships
- Connections between entities
Communication Tables
rooms
- Conversation contextsparticipants
- Room membershipmessages
- Message history
System Tables
logs
- System event loggingcache
- Key-value cache with composite primary keytasks
- Background task managementembeddings
- Vector embeddings for similarity search
Schema Introspection
The system usesDrizzleSchemaIntrospector
to analyze database schemas:
Migration Process
The dynamic migrator handles various scenarios:Table Creation
Column Addition
Index Management
Foreign Key Constraints
Best Practices
1. Schema Design
- Use UUIDs for primary keys
- Include timestamps (created_at, updated_at)
- Use JSONB for flexible metadata
- Define proper indexes for query performance
2. Foreign Keys
- Always reference existing tables
- Consider cascade options carefully
- Be aware of circular dependencies
3. Composite Keys
4. Plugin Schema Organization
Error Handling
The migration system includes robust error handling:- Duplicate Tables - Silently skipped
- Missing Dependencies - Tables created in dependency order
- Failed Migrations - Detailed error logging with rollback
- Schema Conflicts - Clear error messages for debugging
Limitations and Considerations
- No Downgrades - The system only adds, never removes
- Column Type Changes - Not automatically handled
- Data Migrations - Must be handled separately
- Production Use - Test thoroughly before deploying schema changes