LocalData/Documentation/Migration.md
Matt Bruce 9a9889b8a0 Update Audit, Helpers, Services + tests + docs
Summary:
- Sources: Audit, Helpers, Services
- Tests: StorageCatalogTests.swift
- Docs: Design, Migration, Testing
- Added symbols: struct MyNewKey, func setupSession, func handleReceivedContext, class SessionDelegateProxy, func session, func sessionDidBecomeInactive (+2 more)

Stats:
- 10 files changed, 181 insertions(+), 1 deletion(-)
2026-01-18 14:53:28 -06:00

36 lines
1.2 KiB
Markdown

# LocalData Migration Guide
## Overview
`LocalData` provides built-in support for migrating data from legacy storage locations or keys to modern `StorageKey` definitions.
## Automatic Migration
When calling `get(_:)` on a key, the `StorageRouter` automatically:
1. Checks the primary location.
2. If not found, iterates through `migrationSources` defined on the key.
3. If data is found in a source:
- Unsecures it using the source's old policy.
- Re-secures it using the new key's policy.
- Stores it in the new location.
- Deletes the legacy data.
- Returns the value.
## Proactive Migration (Sweep)
You can trigger a sweep of all registered keys at app launch:
```swift
try await StorageRouter.shared.registerCatalog(MyCatalog.self, migrateImmediately: true)
```
This iterates through all keys in the catalog and calls `migrate(for:)` on each, ensuring all legacy data is consolidated.
## Defining Migration Sources
When defining a `StorageKey`, add legacy descriptors to the `migrationSources` array:
```swift
struct MyNewKey: StorageKey {
// ...
var migrationSources: [AnyStorageKey] {
[
.key(LegacyKey(name: "old_key_name", domain: .userDefaults(suite: nil)))
]
}
}
```