Move SDK file storage to Application Support directory#514
Move SDK file storage to Application Support directory#514
Conversation
| } | ||
| } | ||
|
|
||
| environment.logger.error("Successfully migrated \(migratedFiles.count) file(s) to Application Support") |
There was a problem hiding this comment.
Success message logged at error level
Medium Severity
The successful migration message is logged via environment.logger.error, which in production calls os_log with .error type. Every successful migration will appear as an error in the device's system logs and Xcode console. Since LoggerClient only exposes an error method, the success log either needs a new log level added or the message needs to be removed entirely to avoid polluting error diagnostics.
| if environment.fileClient.fileExists(newStateFile.path) { | ||
| // Migration already completed | ||
| return newDirectory | ||
| } |
There was a problem hiding this comment.
Redundant filesystem calls on every state save
Low Severity
migrateFilesIfNeeded is called on every debounced state save (via klaviyoStateFile), not just at initialization. After migration completes, every save still calls createDirectory (line 30) before the fileExists check (line 40), resulting in two unnecessary filesystem syscalls per save. Reordering the "already migrated" check before the createDirectory call, or caching the resolved directory, would eliminate the repeated I/O on the hot path.
Additional Locations (1)
Fixes #179 This change moves all Klaviyo SDK files from the Library/ directory to Library/Application Support/com.klaviyo/ following Apple's File System Programming Guide best practices. ## Changes - Add automatic file migration on SDK initialization - Files are migrated transparently with zero user impact - Migration is transactional with rollback on failure - Idempotent and safe to run multiple times ## Implementation - Extended FileClient with createDirectory and copyItem methods - Added FileClientMigration module with migration logic - Updated KlaviyoState to use migrated directory - Added comprehensive test coverage (10 new tests) ## Migration Behavior New installations: Files created directly in new location Existing installations: Automatic migration on first init after update Migration failure: Falls back to old location, SDK continues working Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
037d6a8 to
d41e8ab
Compare
| return oldDirectory | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Migration permanently stuck if destination file pre-exists
Low Severity
FileManager.copyItem throws (error code 516) when the destination file already exists. The migration loop doesn't check for or remove a pre-existing destination before calling copyItem. If a previous migration partially failed AND rollbackMigration also partially failed (leaving orphaned files in the new directory), all subsequent migration attempts will permanently fail on those orphaned files, since rollbackMigration only cleans up files from the current migratedFiles list, not leftover files from prior runs. The SDK falls back to the old directory gracefully, but migration never succeeds.


Description
Moves Klaviyo SDK file storage from
Library/toLibrary/Application Support/com.klaviyo/to follow Apple's File System Programming Guide best practices. The change includes automatic migration for existing installations with zero user impact.Fixes #179
Due Diligence
Release/Versioning Considerations
PatchContains internal changes or backwards-compatible bug fixes.MinorContains changes to the public API.MajorContains breaking changes.Changelog / Code Overview
What Changed
Before: SDK files stored at
Library/klaviyo-{apiKey}-state.jsonAfter: SDK files stored at
Library/Application Support/com.klaviyo/klaviyo-{apiKey}-state.jsonImplementation Details
Extended FileClient (
Sources/KlaviyoCore/Utils/FileUtils.swift)createDirectorymethod for creating directory hierarchiescopyItemmethod for file operationsNew Migration Module (
Sources/KlaviyoCore/Utils/FileClientMigration.swift)klaviyoApplicationSupportDirectory()- Returns proper Application Support pathmigrateFilesIfNeeded(apiKey:)- Handles automatic migrationUpdated State Management (
Sources/KlaviyoSwift/StateManagement/KlaviyoState.swift)klaviyoStateFile()now calls migration functionComprehensive Test Coverage (
Tests/KlaviyoSwiftTests/FileClientMigrationTests.swift)Test Infrastructure Updates
Migration Behavior
New Installations:
Existing Installations:
klaviyo-*files (state.json + legacy .plist files)Failure Handling:
Test Plan
Unit Tests
✅ All 224 tests passing (including 10 new migration tests)
New test coverage:
Manual Testing Steps
Test 1: Fresh Install
Library/Application Support/com.klaviyo/Library/klaviyo-*Test 2: Existing Installation Migration
Library/)Library/Application Support/com.klaviyo/Library/Test 3: Migration Idempotency
Verification Commands
Related Issues/Tickets