home / skills / willsigmon / sigstack / swiftdata-migration-writer
This skill generates SwiftData migration logic to move UserDefaults to SwiftData, preserving data, enabling rollback, and validating success.
npx playbooks add skill willsigmon/sigstack --skill swiftdata-migration-writerReview the files below or copy the command above to add this skill to your agents.
---
name: SwiftData Migration Writer
description: Write UserDefaults to SwiftData migration logic for Leavn app with data preservation, rollback, and validation
allowed-tools: Read, Write, Edit
---
# SwiftData Migration Writer
Create migration from UserDefaults to SwiftData:
1. **Map keys to entity fields**
2. **Write migration method**:
```swift
func migrateXIfNeeded() async throws {
guard !hasMigrated("X") else { return }
// Read UserDefaults
// Create/update entity
// Archive old keys
// Mark migrated
}
```
3. **Add to PreferencesStore extension**
4. **Call on first load**
5. **Test data preservation**
Use when: Creating SwiftData entities, migrating preferences, data persistence
This skill generates SwiftData migration logic that moves settings and small preference data from UserDefaults into SwiftData entities for the Leavn app. It focuses on preserving user data, performing validation, and providing rollback and archival steps to keep migrations safe and reversible. The output is ready-to-integrate Swift migration methods and usage notes for calling them on app start.
The skill inspects declared SwiftData entities and a provided mapping of UserDefaults keys to entity fields, then writes an async migration method per entity that reads UserDefaults, validates values, creates or updates SwiftData objects, archives or removes old keys, and marks the migration as complete. It includes rollback hooks and simple validation logic so corrupted or partially-applied migrations can be detected and reversed. The generated code integrates into a PreferencesStore extension and includes guidance for invoking migrations on first load and for writing tests.
How does rollback work?
Generated code performs staged writes and sets a migration marker only after success; on failure it restores archived keys or clears partially created entities according to the rollback hook.
Will migration run multiple times?
No. Each migration sets a persistent migrated flag so the method returns early if the migration has already completed.
Do I need tests?
Yes. Tests should cover happy path, invalid legacy values, and simulated failures to verify rollback and data preservation.