Skip to content

Commit 3701acc

Browse files
committed
tightened up migration code
1 parent cf8947c commit 3701acc

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Sources/Segment/Utilities/Utils.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,22 @@ private func migrateFromOldLocations(writeKey: String, to newLocation: URL) {
9898
// If segment dir already exists in app support, we're done
9999
guard !fm.fileExists(atPath: newSegmentDir.path) else { return }
100100

101-
// Look for old segment directories to move
102-
let oldLocations = [
103-
fm.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("segment"),
104-
fm.urls(for: .cachesDirectory, in: .userDomainMask).first?.appendingPathComponent("segment")
105-
].compactMap { $0 }
101+
// Only check the old location that was actually used on this platform
102+
#if (os(iOS) || os(watchOS)) && !targetEnvironment(macCatalyst)
103+
let oldSearchPath = FileManager.SearchPathDirectory.documentDirectory
104+
#else
105+
let oldSearchPath = FileManager.SearchPathDirectory.cachesDirectory
106+
#endif
106107

107-
for oldSegmentDir in oldLocations {
108-
guard fm.fileExists(atPath: oldSegmentDir.path) else { continue }
109-
110-
do {
111-
try fm.moveItem(at: oldSegmentDir, to: newSegmentDir)
112-
Analytics.segmentLog(message: "Migrated analytics data from \(oldSegmentDir.path)", kind: .debug)
113-
return // Success!
114-
} catch {
115-
Analytics.segmentLog(message: "Failed to migrate from \(oldSegmentDir.path): \(error)", kind: .error)
116-
}
108+
guard let oldBaseURL = fm.urls(for: oldSearchPath, in: .userDomainMask).first else { return }
109+
let oldSegmentDir = oldBaseURL.appendingPathComponent("segment")
110+
111+
guard fm.fileExists(atPath: oldSegmentDir.path) else { return }
112+
113+
do {
114+
try fm.moveItem(at: oldSegmentDir, to: newSegmentDir)
115+
Analytics.segmentLog(message: "Migrated analytics data from \(oldSegmentDir.path)", kind: .debug)
116+
} catch {
117+
Analytics.segmentLog(message: "Failed to migrate from \(oldSegmentDir.path): \(error)", kind: .error)
117118
}
118119
}

0 commit comments

Comments
 (0)