2.4 KiB
2.4 KiB
Storage And Migrations
Local Product Store
The product store is abstracted by LocalDataStore.
Implementations:
HiveLocalDataStoreSharedPrefsLocalDataStoreInMemoryLocalDataStore
Provider:
lib/app/data/storage/local_data_store_provider.dart
Default:
USE_HIVE_LOCAL_DB=true
Sync Store
The sync queue has a separate store abstraction:
lib/features/sync/data/storage/sync_state_store.dartHiveSyncStateStoreSharedPrefsSyncStateStoreInMemorySyncStateStore
This separation is intentional. Product state and sync queue state have different recovery and migration needs.
Migration Behavior
On startup, LocalRepository:
- Reads the configured product store.
- If Hive is enabled and empty, attempts to import legacy shared preferences.
- Writes the migrated data into Hive.
- Clears the legacy store.
- Parses
LocalDataState. - Reconciles reminder schedules.
If stored JSON is invalid, the app falls back to empty seeded state.
SyncQueueRepository follows the same Hive-first import pattern for sync state.
Adding Persisted Fields
Checklist:
- Add the field to the feature-owned model.
- Update constructor, field declaration,
copyWith,toJson, andfromJson. - Use default values or nullable parsing for backward compatibility.
- Add the field to
LocalDataStateif it is a new aggregate collection. - Update repository operations that should create, update, merge, or delete it.
- Update merge behavior if the field is person-owned.
- Update sync payloads if the backend should receive it.
- Add tests for old JSON and new JSON.
Increase _schemaVersion in LocalRepository only when the migration needs
explicit logic or when a stored shape changes in a way that cannot be handled by
default parsing.
Data Loss Traps
- Do not remove unknown lists from
LocalDataState.fromJsonwithout a migration. - Do not clear Hive/shared preferences to "fix" parse issues except in tests.
- When deleting a person, check all person-owned collections and source links.
- When merging people, move facts, dates, signals, messages, inbox candidates, reminders, ideas, moments, and source links to the target profile.
- Reminder scheduling is a side effect of repository writes; failures are swallowed by design.
Useful Tests
flutter test test/features/local
flutter test test/features/local/storage
flutter test test/features/sync/storage