-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into AMP-63197-migrate-legacy-data
# Conflicts: # Amplitude-Swift.xcodeproj/project.pbxproj # Sources/Amplitude/Amplitude.swift
- Loading branch information
Showing
12 changed files
with
376 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
108 changes: 108 additions & 0 deletions
108
Sources/Amplitude/Migration/StoragePrefixMigration.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import Foundation | ||
|
||
class StoragePrefixMigration { | ||
let source: PersistentStorage | ||
let destination: PersistentStorage | ||
let logger: (any Logger)? | ||
|
||
init(source: PersistentStorage, destination: PersistentStorage, logger: (any Logger)?) { | ||
self.source = source | ||
self.destination = destination | ||
self.logger = logger | ||
} | ||
|
||
func execute() { | ||
if source.storagePrefix == destination.storagePrefix { | ||
return | ||
} | ||
|
||
moveSourceEventFilesToDestination() | ||
moveUserDefaults() | ||
} | ||
|
||
private func moveUserDefaults() { | ||
moveStringProperty(StorageKey.DEVICE_ID) | ||
moveStringProperty(StorageKey.USER_ID) | ||
moveIntegerProperty(StorageKey.PREVIOUS_SESSION_ID) | ||
moveIntegerProperty(StorageKey.LAST_EVENT_TIME) | ||
moveIntegerProperty(StorageKey.LAST_EVENT_ID) | ||
moveEventsFileKey() | ||
} | ||
|
||
private func moveSourceEventFilesToDestination() { | ||
let sourceEventFiles = source.getEventFiles(includeUnfinished: true) | ||
if sourceEventFiles.count == 0 { | ||
return | ||
} | ||
// Ensure destination directory exists. | ||
_ = destination.getEventsStorageDirectory(createDirectory: true) | ||
|
||
let fileManager = FileManager.default | ||
for sourceEventFile in sourceEventFiles { | ||
var destinationEventFile = sourceEventFile.path.replacingOccurrences(of: "/\(source.eventsFileKey)/", with: "/\(destination.eventsFileKey)/") | ||
if fileManager.fileExists(atPath: destinationEventFile) { | ||
var fileExtension = sourceEventFile.pathExtension | ||
if fileExtension != "" { | ||
fileExtension = ".\(fileExtension)" | ||
} | ||
destinationEventFile = "\((destinationEventFile as NSString).deletingPathExtension)-\(NSUUID().uuidString)\(fileExtension)" | ||
} | ||
do { | ||
try fileManager.moveItem(atPath: sourceEventFile.path, toPath: destinationEventFile) | ||
} catch { | ||
logger?.warn(message: "Can't move \(sourceEventFile) to \(destinationEventFile): \(error)") | ||
} | ||
} | ||
} | ||
|
||
private func moveStringProperty(_ key: StorageKey) { | ||
guard let sourceValue: String = source.read(key: key) else { | ||
return | ||
} | ||
|
||
if destination.read(key: key) == nil { | ||
do { | ||
try destination.write(key: key, value: sourceValue) | ||
} catch { | ||
logger?.warn(message: "can't write destination \(key): \(error)") | ||
} | ||
} | ||
|
||
do { | ||
try source.write(key: key, value: nil) | ||
} catch { | ||
logger?.warn(message: "can't write source \(key): \(error)") | ||
} | ||
} | ||
|
||
private func moveIntegerProperty(_ key: StorageKey) { | ||
guard let sourceValue: Int = source.read(key: key) else { | ||
return | ||
} | ||
|
||
let destinationValue: Int? = destination.read(key: key) | ||
if destinationValue == nil || destinationValue! < sourceValue { | ||
do { | ||
try destination.write(key: key, value: sourceValue) | ||
} catch { | ||
logger?.warn(message: "can't write destination \(key): \(error)") | ||
} | ||
} | ||
|
||
do { | ||
try source.write(key: key, value: nil) | ||
} catch { | ||
logger?.warn(message: "can't clear source \(key): \(error)") | ||
} | ||
} | ||
|
||
private func moveEventsFileKey() { | ||
if let sourceEventFileKey: Int = source.userDefaults?.integer(forKey: source.eventsFileKey) { | ||
let destinationEventFileKey: Int? = destination.userDefaults?.integer(forKey: destination.eventsFileKey) | ||
if destinationEventFileKey == nil || destinationEventFileKey! < sourceEventFileKey { | ||
destination.userDefaults?.set(sourceEventFileKey, forKey: destination.eventsFileKey) | ||
} | ||
} | ||
source.userDefaults?.removeObject(forKey: source.eventsFileKey) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.