A SwiftData migration runs once, on your users' devices, against data you can't see. If it fails halfway, there's no second try: the store may be half-migrated, and the app may not open at all.
SchemaMigrationPlan gives you the hooks, but not the safety net. Rename a property without originalName: and SwiftData drops the old column's data. A thrown error in didMigrate leaves you with whatever was written so far. And testing a migration against a real old-version store takes a lot of setup.
I built Strata to cover those gaps. Version 0.4.0 adds crash recovery and CloudKit support.
The short version
let plan = MigrationPlan(schemas: [SchemaV1.self, SchemaV2.self]) {
Stage(from: SchemaV1.self, to: SchemaV2.self) {
Rename(\PostV1.body, to: \PostV2.content,
sourceKey: \PostV1.id, destinationKey: \PostV2.id)
Backfill(\PostV2.slug) { slugify($0.title) }
Assert.unique(\PostV2.slug)
}
}
let container = try await SafeModelContainer.make(
for: Schema(versionedSchema: SchemaV2.self),
plan: plan,
storeURL: storeURL
)
Before migrating, Strata copies the store with SQLite's online backup API, so the copy is consistent even with a write-ahead log. It then runs the stages; if anything throws, including a failed Assert, it puts the backup back and gives you a structured MigrationError. When there was nothing to migrate, it deletes the backup again.
What else it does
-
Renamethat keeps data, matched by an id you choose, so it doesn't depend on SwiftData internals. -
Backfill,TransformandDeleteWhere, which fetch in batches so large stores don't load into memory at once, plusCustomOperationfor anything else. -
Assertions (
noNulls,unique,count, or your own) that run after the data work and fail the migration when an invariant breaks. - Schema diffs and drift detection: compare two schema versions, or a declared schema against the store on disk.
Crash recovery
A backup only helps if something uses it. If the app is killed in the middle of a migration (a crash, jetsam, a dead battery), there's no catch block to run.
So Strata writes a small journal next to the store: migration started, backup taken, migration finished. On the next launch, before anything else, it checks for a migration that started but never finished, and restores the backup taken for that migration. Each interruption is recovered once and then closed in the journal.
CloudKit stores
Rolling back a CloudKit-synced store is risky, because the schema change may already have reached iCloud. With cloudKitDatabase: set, Strata still takes the backup but skips automatic rollback, so you can recover by hand if you need to.
Testing a migration
final class PostMigrationTests: XCTestCase, MigrationTestCase {
func test_body_survives_rename() async throws {
let store = try fixture(schema: SchemaV1.self) { context in
context.insert(SchemaV1.Post(id: "p1", title: "Hello", body: "World"))
}
let container = try await migrate(store: store, to: SchemaV2.self, plan: plan)
let posts = try ModelContext(container).fetch(FetchDescriptor<SchemaV2.Post>())
XCTAssertEqual(posts.first?.content, "World")
}
}
fixture builds a real SQLite store at the old schema; migrate runs your plan against it. It runs in CI like any other test.
Try it
Add https://github.com/GRimAce11/Strata in Swift Package Manager (from 0.4.0). Link StrataCore in your app and StrataTesting in your tests. It supports iOS 17, macOS 14, watchOS 10, tvOS 17 and visionOS 1, uses Swift 6 language mode, and has no third-party dependencies in the core library.
Feedback and issues are very welcome. If you've been burned by a SwiftData migration, I'd like to hear what happened. And if Strata is useful to you, a star helps other people find it.
Top comments (1)
The journal for the killed-mid-migration case is the part most tools skip, nice. One thing I'd check on the restore path: when the backup goes back, the old
-waland-shmfiles next to the store have to go too, or SQLite replays the half-migrated WAL on top of the restored file on next open. Same if a ModelContainer from the failed attempt is still alive in that process, since it can keep the WAL open. Is the restore done before any container exists on the next launch?