Fix launch crash after SwiftData schema upgrade - #28
Draft
gsbernstein wants to merge 1 commit into
Draft
Conversation
When UserPreferences schema changes can't be lightweight-migrated (e.g. maxSleepHoursPerNight removal in #20), delete the stale store files and recreate the container instead of fatalError on launch. Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Users upgrading from a build with the old
UserPreferencesschema (maxSleepHoursPerNight/minSleepHoursPerNight) to the current schema (earliestReasonableBedtime) crash on launch. SwiftData cannot lightweight-migrate that change, andBedtimeAppcurrently callsfatalErrorwhenModelContainercreation fails.This matches a TestFlight report where the app crashes repeatedly on first open after updating (HealthKit/Garmin sync is unrelated — the crash happens during SwiftData init, before any HealthKit code runs).
Fix
Port the store-reset recovery that already exists on
cursor/sleep-bank-insights-023c(70fb413) but was never merged tomaster:-shm/-walsidecarsUserPreferencesonly holds settings (sleep goal, wake time, etc.) with sensible defaults, so resetting the store is an acceptable tradeoff vs. a hard crash. Source exclusions and other prefs stored in UserDefaults are unaffected.Verification
On macOS/Xcode, install a build with the old schema, set custom preferences, then upgrade to this build and confirm the app launches (settings reset to defaults) instead of crashing.