Drift Detection
Drift detection catches accidental modifications to frozen schemas before they cause SwiftData crashes in production. It’s your first line of defense against schema-related bugs.What is Schema Drift?
Schema drift occurs when you accidentally modify a schema version that’s already been frozen and shipped:Why Drift is Dangerous
SwiftData’s Internal Hashing
SwiftData generates internal hashes for each schema version. When a user opens your app:- SwiftData reads the existing database
- Compares the stored schema hash with current schema hash
- If hashes match → no migration needed
- If hashes don’t match → trigger migration
Fresh Installs Hide the Problem
Your simulator tests won’t catch this:How Drift Detection Works
1. Checksum Generation
When you freeze a schema, FreezeRay generates a SHA256 checksum of the schema structure:- The JSON fixture file
- The Swift code (via macro expansion)
2. Drift Test Generation
FreezeRay scaffolds a drift test that runs on every test suite execution:3. Checksum Comparison
The__freezeray_check_1_0_0() function:
- Computes current schema checksum
- Compares with frozen checksum
- Throws descriptive error if they don’t match
4. Clear Error Messages
When drift is detected, you get an actionable error:What Triggers Drift Detection
Drift is detected when you change any part of a frozen schema:Adding Models
Removing Models
Modifying Model Properties
Changing Property Types
Modifying Relationships
Changing Attributes
Running Drift Tests
Local Development
Drift tests run automatically with your test suite:Continuous Integration
Add drift detection to your CI pipeline:Fixing Drift
Step 1: Identify the Drift
Run tests to see which schema drifted:Step 2: Revert Changes
Undo modifications to the frozen schema:Step 3: Create New Version
Create a new schema version with your changes:Step 4: Add Migration
Define how to migrate from V1 → V2:Step 5: Freeze New Version
Best Practices
Run Tests Before Every Commit
Fail Fast in CI
Configure CI to fail immediately on drift:Use Type Aliases
Type aliases help prevent accidental modifications:Edge Cases
Intentional Schema Updates (Dev Only)
During development, you might want to update an unfrozen schema:Force Overwriting (Last Resort)
If you absolutely must refreeze (e.g., fixtures corrupted):Troubleshooting
False Positives
Symptom: Drift detected but schema hasn’t changed Causes:- File encoding changes (LF vs CRLF)
- Whitespace differences
- Comment changes
Drift Not Detected
Symptom: Modified frozen schema but tests pass Causes:- Drift test not added to test target
- Test file not committed to git
- Test target not building
Next Steps
Schema Freezing
Learn how to freeze schemas and generate checksums
Migration Testing
Test migrations after detecting drift
CI Integration
Automate drift detection in your CI pipeline
First Freeze
Start using drift detection in your project