Menu

1 / 2

Making SwiftData migrations safe to ship

DEV Community: ios·Chethan Nayak·3 days ago
#OhcSUHxL
#devtoios#devto#dev#migration#store
Long article (715 words) — no AI summary yet.
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.