LocalData/Documentation/Migration.md
Matt Bruce 905cabc542 Update Audit, Helpers, Services and tests, docs
Summary:
- Sources: update Audit, Helpers, Services
- Tests: update tests for StorageCatalogTests.swift
- Docs: add docs for Design, Migration, Testing

Stats:
- 10 files changed, 181 insertions(+), 1 deletion(-)
2026-01-18 13:43:09 -06:00

1.2 KiB

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:

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:

struct MyNewKey: StorageKey {
    // ...
    var migrationSources: [AnyStorageKey] {
        [
            .key(LegacyKey(name: "old_key_name", domain: .userDefaults(suite: nil)))
        ]
    }
}