CoreData to SwiftData Persistence Layer Migration Spec
Author a persistence migration specification to convert CoreData NSManagedObject models to SwiftData schemas safely.
Use this template when modernizing an iOS storage layer from CoreData to SwiftData. It creates an actionable schema transition spec that handles data migration stages, relationship integrity, and rollback safeguards.
Role: Senior iOS Infrastructure Engineer specializing in local database persistence and schema migrations.
Context
- Storage Subsystem: {{target_subsystem}}
- Origin Schema Version: {{storage_schema_version}}
- Data Graph Structure: {{entity_graph_complexity}}
- CloudKit Integration: {{cloudkit_sync_enabled}}
- Max DB Size Baseline: {{local_db_size_mb}}
- Rollback Threshold: {{migration_rollback_threshold}}
Task
Generate a database architecture specification for migrating {{target_subsystem}} from CoreData ({{storage_schema_version}}) to SwiftData @Model declarations, managing {{entity_graph_complexity}} while preserving {{cloudkit_sync_enabled}} compatibility within {{local_db_size_mb}}.
Method
- Analyze legacy .xcdatamodeld entities and define equivalent SwiftData @Model classes and attribute macros.
- Map explicit relationships, delete rules, and inverse relationships across {{entity_graph_complexity}}.
- Formulate SchemaMigrationPlan stages, distinguishing between lightweight transformations and custom SchemaMigrationStages.
- Design fallback initialization logic when store opening fails beyond {{migration_rollback_threshold}}.
- Specify concurrency isolation patterns utilizing ModelContainer and ModelContext on background threads.
- Define sync rules and conflict-resolution strategies tailored to {{cloudkit_sync_enabled}} requirements.
- Draft automated schema verification tests verifying store size integrity against {{local_db_size_mb}}.
Constraints
- MUST guarantee zero data loss during sequential and non-sequential schema jumps.
- MUST NOT perform synchronous migration routines on the main UI thread during app launch.
- CloudKit field naming and optionality rules must be strictly preserved if sync is active.
- Schema stages must include explicit pre-migration and post-migration validation assertions.
Output format
Deliver the technical specification across the following four sections:
- Model Declaration Matrix (SwiftData @Model definitions with indices and relationship rules)
- Migration Plan Configuration (VersionedSchema enum definitions and SchemaMigrationPlan stages)
- Threading and ModelContext Architecture (Actor isolation, context sharing, and background save flows)
- Rollback and Integrity Safeguards (Recovery pipelines, error handling, and test validation checklist) Length must be strictly between 400 and 700 words.
Self-review
- Check that {{target_subsystem}}, {{storage_schema_version}}, and {{local_db_size_mb}} are clearly specified.
- Validate that CloudKit constraints match the {{cloudkit_sync_enabled}} state.
- Ensure all four required section headers match the formatting contract verbatim.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.