Token导航 LogoToken导航TokenDH.com
开发需要联网github未标认证来源可访问clear审计通过

axiom-swiftdata-migration-diagaxiom swiftdata 迁移诊断

Agent Skill

用于辅助数据整理、表格处理、CSV/Excel 分析、指标计算和图表准备。它适合让 Agent 清洗字段、汇总数据、发现异常、生成统计口径或把分析结果转成可读说明。使用时需要确认数据来源、字段含义和时间范围,避免把样本数据当全量事实;涉及敏感数据、导出文件或批量写回时,应先确认权限和脱敏边界。

总安装

3,765

周安装

175

GitHub Stars

873

下载量

1,403
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

3

许可证

MIT

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:axiom-swiftdata-migration-diag(axiom swiftdata 迁移诊断)
来源仓库:https://github.com/charleswiltgen/axiom
仓库路径:skills/axiom-swiftdata-migration-diag
安装命令:
npx skills add https://github.com/charleswiltgen/axiom --skill axiom-swiftdata-migration-diag
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/charleswiltgen/axiom --skill axiom-swiftdata-migration-diag

简介

用于诊断 SwiftData 迁移失败问题,定位配置错误和数据丢失原因。

  • 适用于启动崩溃、模型不兼容和存储损坏等迁移故障场景。
  • 提供版本化 schema 检查、关系逆配置验证和测试路径建议。
  • 安装前建议确认权限范围和维护状态,避免触发联网或命令执行操作。
  • axiom-swiftdata-migration-diag 属于开发类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

SwiftData Migration Diagnostics

Overview

SwiftData migration failures manifest as production crashes, data loss, corrupted relationships, or simulator-only success. Core principle 90% of migration failures stem from missing models in VersionedSchema, relationship inverse issues, or untested migration paths—not SwiftData bugs.

Red Flags — Suspect SwiftData Migration Issue

If you see ANY of these, suspect a migration configuration problem:

  • App crashes on launch after schema change
  • "Expected only Arrays for Relationships" error
  • "The model used to open the store is incompatible with the one used to create the store"
  • "Failed to fulfill faulting for [relationship]"
  • Migration works in simulator but crashes on real device
  • Data exists before migration, gone after
  • Relationships broken after migration (nil where they shouldn't be)
  • FORBIDDEN "SwiftData migrations are broken, we should use Core Data"

- SwiftData handles millions of migrations in production apps - Schema mismatches and relationship errors are always configuration, not framework - Do not rationalize away the issue—diagnose it

Critical distinction Simulator deletes the database on each rebuild, hiding schema mismatch issues. Real devices keep persistent databases and crash immediately on schema mismatch. MANDATORY: Test migrations on real device with real data before shipping.

Mandatory First Steps

ALWAYS run these FIRST (before changing code):

// 1. Identify the crash/issue type
// Screenshot the crash message and note:
//   - "Expected only Arrays" = relationship inverse missing
//   - "incompatible model" = schema version mismatch
//   - "Failed to fulfill faulting" = relationship integrity broken
//   - Simulator works, device crashes = untested migration path
// Record: "Error type: [exact message]"

// 2. Check schema version configuration
// In your migration plan:
enum MigrationPlan: SchemaMigrationPlan {
    static var schemas: [any VersionedSchema.Type] {
        // ✅ VERIFY: All versions in order?
        // ✅ VERIFY: Latest version matches container?
        [SchemaV1.self, SchemaV2.self, SchemaV3.self]
    }

    static var stages: [MigrationStage] {
        // ✅ VERIFY: Migration stages match schema transitions?
        [migrateV1toV2, migrateV2toV3]
    }
}

// In your app:
let schema = Schema(versionedSchema: SchemaV3.self)  // ✅ VERIFY: Matches latest in plan?
let container = try ModelContainer(
    for: schema,
    migrationPlan: MigrationPlan.self  // ✅ VERIFY: Plan is registered?
)
// Record: "Schema version: latest is [version]"

// 3. Check all models included in VersionedSchema
enum SchemaV2: VersionedSchema {
    static var models: [any PersistentModel.Type] {
        // ✅ VERIFY: Are ALL models listed? (even unchanged ones)
        [Note.self, Folder.self, Tag.self]
    }
}
// Record: "Missing models? Yes/no"

// 4. Check relationship inverse declarations
@Model
final class Note {
    @Relationship(deleteRule: .nullify, inverse: \Folder.notes)  // ✅ VERIFY: inverse specified?
    var folder: Folder?

    @Relationship(deleteRule: .nullify, inverse: \Tag.notes)  // ✅ VERIFY: inverse specified?
    var tags: [Tag] = []
}
// Record: "Relationship inverses: all specified? Yes/no"

// 5. Enable SwiftData debug logging
// In Xcode scheme, add argument:
// -com.apple.coredata.swiftdata.debug 1
// Run and check Console for SQL queries
// Record: "Debug log shows: [what you see]"

What this tells you

  • "Expected only Arrays for Relationships" → Proceed to Pattern 1 (relationship inverse fix)
  • "incompatible model" → Proceed to Pattern 2 (schema version mismatch)
  • Missing models in VersionedSchema → Proceed to Pattern 3 (complete schema snapshot)
  • Simulator works, device crashes → Proceed to Pattern 4 (migration testing)
  • Data lost after migration → Proceed to Pattern 5 (willMigrate/didMigrate misuse)

MANDATORY INTERPRETATION

Before changing ANY code, identify ONE of these:

  1. If error is "Expected only Arrays" AND relationship inverse missing → Relationship configuration issue
  2. If error mentions "incompatible" AND schema versions don't match → Version mismatch
  3. If models are missing from VersionedSchema → Incomplete schema snapshot
  4. If simulator succeeds but device fails → Untested migration path
  5. If data exists before but not after → willMigrate/didMigrate limitation violated

If diagnostics are contradictory or unclear

  • STOP. Do NOT proceed to patterns yet
  • Add -com.apple.coredata.swiftdata.debug 1 and examine SQL output
  • Check file system: does.sqlite file exist? What size?
  • Establish baseline: what's actually happening vs. what you assumed

Verifying Migration Completed Successfully

Use this section when migration appears to complete without errors, but you want to verify data integrity.

Quick Verification Checklist

After migration runs without crashing:

// 1. Verify record count matches pre-migration
let context = container.mainContext
let postMigrationCount = try context.fetch(FetchDescriptor<Note>()).count
print("Post-migration count: \(postMigrationCount)")
// Compare to pre-migration count

// 2. Spot-check specific records
let sampleNote = try context.fetch(
    FetchDescriptor<Note>(predicate: #Predicate { $0.id == "known-test-id" })
).first
print("Sample note title: \(sampleNote?.title ?? "MISSING")")

// 3. Verify relationships intact
if let note = sampleNote {
    print("Folder relationship: \(note.folder != nil ? "✓" : "✗")")
    print("Tags count: \(note.tags.count)")

    // Verify inverse relationships
    if let folder = note.folder {
        let folderHasNote = folder.notes.contains { $0.id == note.id }
        print("Inverse relationship: \(folderHasNote ? "✓" : "✗")")
    }
}

// 4. Check for orphaned data
let orphanedNotes = try context.fetch(
    FetchDescriptor<Note>(predicate: #Predicate { $0.folder == nil })
)
print("Orphaned notes (should be 0 if cascade delete worked): \(orphanedNotes.count)")

What Successful Migration Looks Like

Console Output:

Post-migration count: 1523  // Matches pre-migration
Sample note title: Test Note  // Not "MISSING"
Folder relationship: ✓
Tags count: 3
Inverse relationship: ✓
Orphaned notes: 0

If you see:

  • Record count differs → Data loss (check willMigrate logic)
  • "MISSING" records → Schema mismatch or fetch error
  • Relationships nil → Inverse configuration or prefetching issue
  • Orphaned records >0 → Cascade delete rule not working

See patterns below for specific fixes.


Decision Tree

SwiftData migration problem suspected?
├─ Error: "Expected only Arrays for Relationships"?
│  └─ YES → Relationship inverse missing
│     ├─ Many-to-many relationship? → Pattern 1a (explicit inverse)
│     ├─ One-to-many relationship? → Pattern 1b (verify both sides)
│     └─ iOS 17.0 alphabetical bug? → Pattern 1c (default value workaround)
│
├─ Error: "incompatible model" or crash on launch?
│  └─ YES → Schema version mismatch
│     ├─ Latest schema not in plan? → Pattern 2a (add to schemas array)
│     ├─ Migration stage missing? → Pattern 2b (add stage)
│     └─ Container using wrong schema? → Pattern 2c (verify version)
│
├─ Migration runs but data missing?
│  └─ YES → Data loss during migration
│     ├─ Used didMigrate to access old models? → Pattern 3a (use willMigrate)
│     ├─ Forgot to save in willMigrate? → Pattern 3b (add context.save())
│     └─ Custom migration logic wrong? → Pattern 3c (debug transformation)
│
├─ Works in simulator but crashes on device?
│  └─ YES → Untested migration path
│     ├─ Never tested on real device? → Pattern 4a (real device testing)
│     ├─ Never tested upgrade path? → Pattern 4b (test v1 → v2 upgrade)
│     └─ Production data differs from test? → Pattern 4c (test with prod data)
│
└─ Relationships nil after migration?
   └─ YES → Relationship integrity broken
      ├─ Forgot to prefetch relationships? → Pattern 5a (add prefetching)
      ├─ Inverse relationship wrong? → Pattern 5b (fix inverse)
      └─ Delete rule caused cascade? → Pattern 5c (check delete rules)

Common Patterns

Pattern 1a: Fix "Expected only Arrays for Relationships"

PRINCIPLE Many-to-many relationships require explicit inverse declarations.

❌ WRONG (Causes "Expected only Arrays" error)

@Model
final class Note {
    var tags: [Tag] = []  // ❌ Missing inverse
}

@Model
final class Tag {
    var notes: [Note] = []  // ❌ Missing inverse
}

✅ CORRECT (Explicit inverse)

@Model
final class Note {
    @Relationship(deleteRule: .nullify, inverse: \Tag.notes)
    var tags: [Tag] = []  // ✅ Inverse specified
}

@Model
final class Tag {
    @Relationship(deleteRule: .nullify, inverse: \Note.tags)
    var notes: [Note] = []  // ✅ Inverse specified
}

Why this works SwiftData requires explicit inverse for many-to-many to create junction table correctly.

Time cost 2 minutes to add inverse declarations


Pattern 1b: iOS 17.0 Alphabetical Bug Workaround

PRINCIPLE In iOS 17.0, many-to-many relationships could fail if model names were in alphabetical order.

❌ WRONG (Crashes in iOS 17.0)

@Model
final class Actor {
    @Relationship(deleteRule: .nullify, inverse: \Movie.actors)
    var movies: [Movie]  // ❌ No default value
}

@Model
final class Movie {
    @Relationship(deleteRule: .nullify, inverse: \Actor.movies)
    var actors: [Actor]  // ❌ No default value
}
// Crashes if "Actor" < "Movie" alphabetically

✅ CORRECT (Works in iOS 17.0+)

@Model
final class Actor {
    @Relationship(deleteRule: .nullify, inverse: \Movie.actors)
    var movies: [Movie] = []  // ✅ Default value
}

@Model
final class Movie {
    @Relationship(deleteRule: .nullify, inverse: \Actor.movies)
    var actors: [Actor] = []  // ✅ Default value
}

Fixed in iOS 17.1+

Time cost 1 minute to add default values


Pattern 2a: Schema Version Mismatch

PRINCIPLE Migration plan's schemas array must include ALL versions in order.

❌ WRONG (Missing version causes crash)

enum MigrationPlan: SchemaMigrationPlan {
    static var schemas: [any VersionedSchema.Type] {
        [SchemaV1.self, SchemaV3.self]  // ❌ Missing V2!
    }

    static var stages: [MigrationStage] {
        [migrateV1toV2, migrateV2toV3]  // References V2 but not in schemas
    }
}

✅ CORRECT (All versions in order)

enum MigrationPlan: SchemaMigrationPlan {
    static var schemas: [any VersionedSchema.Type] {
        [SchemaV1.self, SchemaV2.self, SchemaV3.self]  // ✅ All versions
    }

    static var stages: [MigrationStage] {
        [migrateV1toV2, migrateV2toV3]
    }
}

Time cost 2 minutes to add missing version


Pattern 3a: Data Loss from willMigrate/didMigrate Misuse

PRINCIPLE Old models only accessible in willMigrate, new models only in didMigrate.

❌ WRONG (Tries to access old models in didMigrate)

static let migrate = MigrationStage.custom(
    fromVersion: SchemaV1.self,
    toVersion: SchemaV2.self,
    willMigrate: nil,
    didMigrate: { context in
        // ❌ CRASH: SchemaV1.Note doesn't exist here
        let oldNotes = try context.fetch(FetchDescriptor<SchemaV1.Note>())

        // Data lost because transformation never ran
    }
)

✅ CORRECT (Transform in willMigrate)

static let migrate = MigrationStage.custom(
    fromVersion: SchemaV1.self,
    toVersion: SchemaV2.self,
    willMigrate: { context in
        // ✅ SchemaV1.Note exists here
        let oldNotes = try context.fetch(FetchDescriptor<SchemaV1.Note>())

        // Transform data while old models still accessible
        for note in oldNotes {
            note.transformed = transformLogic(note.oldValue)
        }

        try context.save()  // ✅ Save before migration completes
    },
    didMigrate: nil
)

Time cost 5 minutes to move logic to correct closure


Pattern 4a: Real Device Testing

PRINCIPLE Simulator deletes database on rebuild. Real devices keep persistent databases.

Testing Workflow

# 1. Install v1 on real device
# Build with SchemaV1 as current version
# Run app, create sample data (100+ records)

# 2. Verify data exists
# Check app: should see 100+ records

# 3. Install v2 with migration
# Build with SchemaV2 as current version + migration plan
# Install over existing app (don't delete)

# 4. Verify migration succeeded
# App launches without crash
# Data still exists (100+ records)
# Relationships intact

Migration Test Code

import Testing
import SwiftData

@Test func testMigrationOnRealDevice() throws {
    // This test MUST run on real device, not simulator
    #if targetEnvironment(simulator)
    throw XCTSkip("Migration test requires real device")
    #endif

    let container = try ModelContainer(
        for: Schema(versionedSchema: SchemaV2.self),
        migrationPlan: MigrationPlan.self
    )

    let context = container.mainContext
    let notes = try context.fetch(FetchDescriptor<SchemaV2.Note>())

    // Verify data preserved
    #expect(notes.count > 0)

    // Verify relationships
    for note in notes {
        if note.folder != nil {
            #expect(note.folder?.notes.contains { $0.id == note.id } == true)
        }
    }
}

Time cost 15 minutes to test on real device


Pattern 5a: Relationship Prefetching to Preserve Integrity

PRINCIPLE Fetch relationships eagerly during migration to avoid faulting errors.

❌ WRONG (Relationships may fault and break)

static let migrate = MigrationStage.custom(
    fromVersion: SchemaV1.self,
    toVersion: SchemaV2.self,
    willMigrate: { context in
        let notes = try context.fetch(FetchDescriptor<SchemaV1.Note>())

        for note in notes {
            // ❌ May trigger fault, relationship not loaded
            let folderName = note.folder?.name
        }
    },
    didMigrate: nil
)

✅ CORRECT (Prefetch relationships)

static let migrate = MigrationStage.custom(
    fromVersion: SchemaV1.self,
    toVersion: SchemaV2.self,
    willMigrate: { context in
        var fetchDesc = FetchDescriptor<SchemaV1.Note>()

        // ✅ Prefetch relationships
        fetchDesc.relationshipKeyPathsForPrefetching = [\.folder, \.tags]

        let notes = try context.fetch(fetchDesc)

        for note in notes {
            // ✅ Relationships already loaded
            let folderName = note.folder?.name
            let tagCount = note.tags.count
        }

        try context.save()
    },
    didMigrate: nil
)

Time cost 3 minutes to add prefetching


Quick Reference: Error → Fix Mapping

Error MessageRoot CauseFixTime
"Expected only Arrays for Relationships"Many-to-many inverse missingAdd @Relationship(inverse:) to both sides2 min
"The model used to open the store is incompatible"Schema version mismatchAdd missing version to schemas array2 min
"Failed to fulfill faulting for [relationship]"Relationship not prefetchedAdd relationshipKeyPathsForPrefetching3 min
App crashes after schema changeMissing model in VersionedSchemaInclude ALL models in models array2 min
Data lost after migrationTransformation in wrong closureMove logic from didMigrate to willMigrate5 min
Simulator works, device crashesUntested migration pathTest on real device with real data15 min
Relationships nil after migrationInverse relationship wrongFix @Relationship(inverse:) keypath3 min

Debugging Checklist

When migration fails, verify ALL of these:

  • All models included in VersionedSchema.models array
  • All schema versions included in SchemaMigrationPlan.schemas array
  • Migration stages match schema transitions (V1→V2, V2→V3)
  • Many-to-many relationships have explicit inverse: on both sides
  • Container initialized with correct latest schema version
  • Migration plan registered in ModelContainer initialization
  • Tested on real device (not just simulator)
  • Tested upgrade path (v1 → v2), not just fresh install
  • SwiftData debug logging enabled (-com.apple.coredata.swiftdata.debug 1)
  • Data transformation logic in willMigrate (not didMigrate)

When You're Stuck After 30 Minutes

If you've spent >30 minutes and the migration issue persists:

STOP. You either

  1. Skipped mandatory diagnostics (most common)
  2. Misidentified the actual problem
  3. Applied wrong pattern for your symptom
  4. Haven't tested on real device/real data
  5. Have complex edge case requiring two-stage migration

MANDATORY checklist before claiming "skill didn't work"

  • I ran all Mandatory First Steps diagnostics
  • I identified the problem type (relationship, schema mismatch, data loss, testing gap)
  • I enabled SwiftData debug logging and examined SQL output
  • I tested on real device with real data (not simulator)
  • I applied the FIRST matching pattern from Decision Tree
  • I verified all models included in VersionedSchema
  • I checked relationship inverse declarations

If ALL boxes are checked and still broken

  • You need two-stage migration (covered in axiom-swiftdata-migration skill)
  • Time cost: 30-60 minutes for complex type change migration
  • Ask: "What data transformation is actually needed?" and implement two-stage pattern

Time Cost Transparency

  • Pattern 1 (relationship inverse): 2-3 minutes
  • Pattern 2 (schema version): 2-5 minutes
  • Pattern 3 (willMigrate fix): 5-10 minutes
  • Pattern 4 (real device testing): 15-30 minutes
  • Pattern 5 (relationship prefetching): 3-5 minutes

Real-World Impact

Before SwiftData migration debugging 2-8 hours per issue

  • App crashes on launch in production
  • Data loss for existing users
  • Relationships broken after migration
  • Simulator success, device failure
  • Customer trust damaged

After 15-45 minutes with systematic diagnosis

  • Identify problem type with diagnostics (5 min)
  • Apply correct pattern (5-10 min)
  • Test on real device (15-30 min)
  • Deploy with confidence

Key insight SwiftData has well-established patterns for every common migration issue. The problem is developers don't know which diagnostic applies to their error.


Resources

WWDC: 2025-291, 2023-10195

Docs: /swiftdata

Skills: axiom-swiftdata-migration, axiom-swiftdata, axiom-database-migration


Created 2025-12-09 Status Production-ready diagnostic patterns Framework SwiftData (Apple) Swift 5.9+

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

04

需要参考平台分布和安装热度时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

补充不同宿主或平台的使用分布数据

能力 5

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Claude Code

26.06%
按下载量换算366

OpenCode

24.1%
按下载量换算338

Codex

19.46%
按下载量换算273

Antigravity

11.6%
按下载量换算163

Cursor

7.58%
按下载量换算106

Gemini CLI

3.66%
按下载量换算51

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。

来源信息

继续浏览同类 Skills