4.6 KiB
4.6 KiB
Data Conversion Summary
Overview
This document summarizes the data conversion implementation from the old tldraw sync format to the new automerge sync format.
Conversion Paths
The system handles three data formats automatically:
1. Automerge Array Format
- Format:
[{ state: { id: "...", ... } }, ...] - Conversion:
convertAutomergeToStore() - Handles: Raw Automerge document format
2. Store Format (Already Converted)
- Format:
{ store: { "recordId": {...}, ... }, schema: {...} } - Conversion: None needed - already in correct format
- Handles: Previously converted documents
3. Old Documents Format (Legacy)
- Format:
{ documents: [{ state: {...} }, ...] } - Conversion:
migrateDocumentsToStore() - Handles: Old tldraw sync format
Validation & Error Handling
Record Validation
- ✅ Validates
stateproperty exists - ✅ Validates
state.idexists and is a string - ✅ Validates
state.typeNameexists (for documents format) - ✅ Skips invalid records with detailed logging
- ✅ Preserves valid records
Shape Migration
- ✅ Ensures required properties (x, y, rotation, opacity, isLocked, meta, index)
- ✅ Moves
w/hfrom top-level topropsfor geo shapes - ✅ Fixes richText structure
- ✅ Preserves custom shape properties (ObsNote, Holon, etc.)
- ✅ Tracks and verifies custom shapes
Custom Records
- ✅ Preserves
obsidian_vault:records - ✅ Tracks custom record count
- ✅ Logs custom record IDs for verification
Logging & Statistics
All conversion functions now provide comprehensive statistics:
Conversion Statistics Include:
- Total records processed
- Successfully converted count
- Skipped records (with reasons)
- Errors encountered
- Custom records preserved
- Shape types distribution
- Custom shapes preserved
Log Levels:
- Info: Conversion statistics, successful conversions
- Warn: Skipped records, warnings (first 10 shown)
- Error: Conversion errors with details
Data Preservation Guarantees
What is Preserved:
- ✅ All valid shape data
- ✅ All custom shape properties (ObsNote, Holon, etc.)
- ✅ All custom records (obsidian_vault)
- ✅ All metadata
- ✅ All text content
- ✅ All richText content (structure fixed, content preserved)
What is Fixed:
- 🔧 Missing required properties (defaults added)
- 🔧 Invalid property locations (w/h moved to props)
- 🔧 Malformed richText structure
- 🔧 Missing typeName (inferred where possible)
What is Skipped:
- ⚠️ Records with missing
stateproperty - ⚠️ Records with missing
state.id - ⚠️ Records with invalid
state.idtype - ⚠️ Records with missing
state.typeName(for documents format)
Testing
Unit Tests
test-data-conversion.ts: Tests edge cases with malformed data- Covers: missing fields, null records, invalid types, custom records
Integration Testing
- Test with real R2 data (see
test-r2-conversion.md) - Verify data integrity after conversion
- Check logs for warnings/errors
Migration Safety
Safety Features:
- Non-destructive: Original R2 data is not modified until first save
- Error handling: Invalid records are skipped, not lost
- Comprehensive logging: All actions are logged for debugging
- Fallback: Creates empty document if conversion fails completely
Rollback:
- Original data remains in R2 until overwritten
- Can restore from backup if needed
- Conversion errors don't corrupt existing data
Performance
- Conversion happens once per room (cached)
- Statistics logging is efficient (limited to first 10 errors)
- Shape migration only processes shapes (not all records)
- Custom record tracking is lightweight
Next Steps
- ✅ Conversion logic implemented and validated
- ✅ Comprehensive logging added
- ✅ Custom records/shapes preservation verified
- ✅ Edge case handling implemented
- ⏳ Test with real R2 data (manual process)
- ⏳ Monitor production conversions
Files Modified
worker/AutomergeDurableObject.ts: Main conversion logicgetDocument(): Format detection and routingconvertAutomergeToStore(): Automerge array conversionmigrateDocumentsToStore(): Old documents format conversionmigrateShapeProperties(): Shape property migration
Key Improvements
- Validation: All records are validated before conversion
- Logging: Comprehensive statistics for debugging
- Error Handling: Graceful handling of malformed data
- Preservation: Custom records and shapes are tracked and verified
- Safety: Non-destructive conversion with fallbacks