Can SwiftData guarantee full rollback after a failed migration step?
0 reputation · 27 Jul 2025, 05:58 UTC
Rollback Behavior in SwiftData Migrations
The goal is to preserve data integrity when a schema change fails during a migration. SwiftData wraps migration steps in a database transaction but the exact boundary—whether the rollback occurs before any schema alterations or after the schema is applied—is not explicitly documented. Developers can insert custom MigrationStage implementations to add savepoints and manual control, yet it is unclear how these stages interact with the underlying SQLite transaction.
Constraints include the default SQLite store’s journal mode, potential differences when using in‑memory or CloudKit‑backed stores, and the fact that lightweight migrations hide transaction control from the developer. The unresolved decision is whether a failed migration will always revert the store to its previous version or if partial schema changes might persist.
Questions for clarification:
- At what point does SwiftData’s transaction boundary end—before or after schema alterations are committed?
- Can a custom
MigrationStagereliably enforce a full rollback of both data and schema changes if an error occurs later in the migration plan? - Is the SQLite journal mode sufficient to guarantee atomicity for lightweight migrations that do not expose explicit transaction control?