Schema Versioning
QuantaDB provides automatic schema versioning and migration support through its code generation system.
Basic Usage
Simply specify the version in your @QuantaEntity
annotation:
@QuantaEntity(version: 1)
class User {
final String id;
final String name;
final String email;
}
When you need to make changes, increment the version:
@QuantaEntity(version: 2)
class User {
final String id;
final String name;
final String email;
final String? phone; // New field
}
Automatic Migrations
QuantaDB automatically handles schema changes:
- Field Additions: New fields are added with their default values
- Field Removals: Removed fields are safely dropped
- Type Changes: Type conversions are handled automatically
- Index Updates: Indexes are updated to reflect schema changes
Migration Process
- Version Detection: QuantaDB detects schema version changes
- Schema Analysis: Changes are analyzed automatically
- Migration Generation: Migration code is generated
- Data Migration: Data is migrated to the new schema
- Version Update: Schema version is updated
Example
// Version 1
@QuantaEntity(version: 1)
class User {
final String id;
final String name;
final String email;
}
// Version 2 - Adding a new field
@QuantaEntity(version: 2)
class User {
final String id;
final String name;
final String email;
final String? phone; // New optional field
}
// Version 3 - Modifying a field
@QuantaEntity(version: 3)
class User {
final String id;
final String name;
final String email;
final String? phone;
final DateTime createdAt; // New required field
}
Migration Features
- Automatic Version Tracking: Schema versions are tracked automatically
- Safe Migrations: All migrations are performed safely with rollback support
- Type Safety: Migrations are type-safe and validated at compile time
- Performance: Migrations are optimized for performance
- Atomic Operations: All migrations are atomic
Best Practices
-
Version Management
- Increment version for any schema changes
- Document changes in version comments
- Test migrations thoroughly
-
Field Changes
- Add new fields as nullable when possible
- Provide default values for required fields
- Consider backward compatibility
-
Migration Testing
- Test migrations with real data
- Verify data integrity after migration
- Test rollback scenarios
-
Performance
- Migrate data in batches for large datasets
- Schedule migrations during low-usage periods
- Monitor migration progress
Limitations
- Migrations must be sequential
- Complex schema changes may require manual intervention
- Large datasets may take time to migrate
Future Enhancements
- Parallel migration support
- Custom migration scripts
- Migration preview and validation
- Migration progress tracking