Your First Freeze
This tutorial walks you through freezing your first SwiftData schema with FreezeRay, from installation to verifying drift detection works. Time required: 10-15 minutes What you’ll learn:- Install FreezeRay CLI and package
- Add
@FreezeSchemato your schema - Freeze a schema version
- Run drift detection tests
- Commit fixtures to git
Prerequisites
Before starting, ensure you have:- Xcode 15.0+ installed
- macOS 14+
- An iOS 17+ app with SwiftData
- Git for version control
- npm (for CLI installation)
- Your project uses
VersionedSchema(not just plainSchema)
If your app doesn’t use
VersionedSchema yet, you’ll need to migrate to it first. See Apple’s SwiftData migration documentation.Step 1: Install FreezeRay CLI
Install the CLI via npm:Apple Silicon users: The CLI includes pre-built binaries.Intel Mac users: The CLI will build from source automatically (requires Swift toolchain).
Step 2: Initialize FreezeRay in Your Project
Navigate to your Xcode project directory:- Creates
FreezeRay/Fixtures/andFreezeRay/Tests/directories - Adds FreezeRay Swift package to your project
- Updates your Xcode project or
Package.swift
Step 3: Update Your Schema Definition
Open your schema file (e.g.,Schemas.swift or DataModel.swift) and import FreezeRay:
@FreezeSchema to your schema:
The
@FreezeSchema(version:) string (“1.0.0”) is separate from SwiftData’s versionIdentifier. Use it to organize your fixtures.Step 4: Build Your Project
Before freezing, ensure your project builds:Step 5: Freeze Your Schema
Now freeze your schema version:- Auto-detection - Discovers project, scheme, and test target
- Source parsing - Finds
@FreezeSchema(version: "1.0.0")in your code - Test generation - Creates temporary test file
- Simulator run - Builds and runs in iOS Simulator
- Fixture extraction - Copies SQLite database from simulator
- Test scaffolding - Generates drift detection test
The freeze process takes 30-60 seconds depending on your project’s build time.
Step 6: Inspect the Fixtures
Check what was created:Examine the SQLite database
Check the drift test
OpenFreezeRay/Tests/AppSchemaV1_1_0_0_DriftTests.swift:
Step 7: Run Tests
Run your test suite to verify drift detection works:The drift test should always pass immediately after freezing. If it fails, something went wrong during the freeze process.
Step 8: Verify Drift Detection Works
Let’s test that drift detection catches schema changes:-
Modify your frozen schema (temporarily):
-
Run tests again:
-
Observe the error:
-
Revert the change:
-
Tests pass again:
Step 9: Commit to Git
Commit the fixtures and tests to version control:Step 10: Team Workflow
When other developers pull your changes:- The frozen fixtures (
FreezeRay/Fixtures/1.0.0/) - The drift test (
FreezeRay/Tests/AppSchemaV1_1_0_0_DriftTests.swift)
⌘U), drift detection verifies they haven’t accidentally modified the frozen schema.
What’s Next?
You’ve successfully frozen your first schema! Here’s what to do next:Add a Second Schema Version
When you need to make schema changes:-
Create a new schema version:
-
Generate migration test:
This creates
MigrateV1_0_0toV2_0_0_Tests.swift. Edit it to add custom data validation. -
Add migration:
- Develop and test: Run tests (⌘U) as you implement the schema and migration.
-
Freeze the new version:
This generates fixtures and drift test only. The migration test already exists from step 2.
Set Up CI Integration
Add FreezeRay to your CI pipeline to catch drift on every PR:Customize Tests
Add custom validation to drift and migration tests:Troubleshooting
”No @Freeze(version: “1.0.0”) annotation found”
Problem: You forgot to add@FreezeSchema to your schema.
Solution:
“Build failed”
Problem: Your project doesn’t compile. Solution: Fix build errors in Xcode (⌘B) before runningfreezeray freeze.
”Test failed”
Problem: Your existing tests are failing. Solution: Fix failing tests before freezing. The freeze command requires all tests to pass.”Simulator not found: iPhone 17”
Problem: You don’t have iPhone 17 simulator installed. Solution: Option 1 - Install iPhone 17 simulator:Drift test fails immediately after freezing
Problem: Something went wrong during the freeze. Solution:- Delete fixtures:
rm -rf FreezeRay/Fixtures/1.0.0/ - Freeze again:
freezeray freeze 1.0.0 - If it still fails, file a bug report at github.com/TrinsicVentures/FreezeRayCLI/issues
Summary
You’ve learned how to:- ✅ Install FreezeRay CLI and initialize your project
- ✅ Annotate schemas with
@FreezeSchema(version:) - ✅ Freeze a schema version to create immutable fixtures
- ✅ Run drift detection tests
- ✅ Commit fixtures to version control
Next Steps
Migration Testing
Learn how to test migrations between schema versions
CI Integration
Set up drift detection in your CI pipeline
Schema Freezing Concepts
Deep dive into how schema freezing works
Drift Detection Concepts
Understand how checksums catch schema changes