Skip to content

Commit e92f6ed

Browse files
authored
fix: Do not migrate remnant data from legacy SDK when sandboxing is not enabled (#127)
* fix: Do not migrate remnant data from legacy SDK when sandboxing is not enabled
1 parent dc544fb commit e92f6ed

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

Sources/Amplitude/Amplitude.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Amplitude {
4141

4242
migrateApiKeyStorages()
4343
migrateDefaultInstanceStorages()
44-
if configuration.migrateLegacyData && getStorageVersion() < .API_KEY_AND_INSTANCE_NAME {
44+
if configuration.migrateLegacyData && getStorageVersion() < .API_KEY_AND_INSTANCE_NAME && isSandboxEnabled() {
4545
RemnantDataMigration(self).execute()
4646
}
4747
migrateInstanceOnlyStorages()

Tests/AmplitudeTests/AmplitudeTests.swift

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,82 @@ final class AmplitudeTests: XCTestCase {
535535
}
536536
#endif
537537

538+
func testRemnantDataNotMigratedInNonSandboxedApps() throws {
539+
let instanceName = "legacy_v3_\(UUID().uuidString)".lowercased()
540+
let bundle = Bundle(for: type(of: self))
541+
let legacyDbUrl = bundle.url(forResource: "legacy_v3", withExtension: "sqlite")
542+
let dbUrl = LegacyDatabaseStorage.getDatabasePath(instanceName)
543+
let fileManager = FileManager.default
544+
let legacyDbExists = legacyDbUrl != nil ? fileManager.fileExists(atPath: legacyDbUrl!.path) : false
545+
XCTAssertTrue(legacyDbExists)
546+
547+
try fileManager.copyItem(at: legacyDbUrl!, to: dbUrl)
548+
549+
addTeardownBlock {
550+
let fileManager = FileManager.default
551+
if fileManager.fileExists(atPath: dbUrl.path) {
552+
try fileManager.removeItem(at: dbUrl)
553+
}
554+
}
555+
556+
let apiKey = "test-api-key"
557+
let configuration = Configuration(
558+
apiKey: apiKey,
559+
instanceName: instanceName,
560+
migrateLegacyData: true
561+
)
562+
let amplitude = Amplitude(configuration: configuration)
563+
564+
let deviceId = "9B574574-74A7-4EDF-969D-164CB151B6C3"
565+
let userId = "ios-sample-user-legacy"
566+
567+
#if os(macOS)
568+
// We don't want to transfer remnant data in non-sanboxed apps
569+
XCTAssertFalse(amplitude.isSandboxEnabled())
570+
XCTAssertNotEqual(amplitude.getDeviceId(), deviceId)
571+
XCTAssertNotEqual(amplitude.getUserId(), userId)
572+
#else
573+
XCTAssertEqual(amplitude.getDeviceId(), deviceId)
574+
XCTAssertEqual(amplitude.getUserId(), userId)
575+
#endif
576+
}
577+
578+
#if os(macOS)
579+
func testRemnantDataNotMigratedInSandboxedMacApps() throws {
580+
let instanceName = "legacy_v3_\(UUID().uuidString)".lowercased()
581+
let bundle = Bundle(for: type(of: self))
582+
let legacyDbUrl = bundle.url(forResource: "legacy_v3", withExtension: "sqlite")
583+
let dbUrl = LegacyDatabaseStorage.getDatabasePath(instanceName)
584+
let fileManager = FileManager.default
585+
let legacyDbExists = legacyDbUrl != nil ? fileManager.fileExists(atPath: legacyDbUrl!.path) : false
586+
XCTAssertTrue(legacyDbExists)
587+
588+
try fileManager.copyItem(at: legacyDbUrl!, to: dbUrl)
589+
590+
addTeardownBlock {
591+
let fileManager = FileManager.default
592+
if fileManager.fileExists(atPath: dbUrl.path) {
593+
try fileManager.removeItem(at: dbUrl)
594+
}
595+
}
596+
597+
let apiKey = "test-api-key"
598+
let configuration = Configuration(
599+
apiKey: apiKey,
600+
instanceName: instanceName,
601+
migrateLegacyData: true
602+
)
603+
let amplitude = FakeAmplitudeWithSandboxEnabled(configuration: configuration)
604+
605+
let deviceId = "9B574574-74A7-4EDF-969D-164CB151B6C3"
606+
let userId = "ios-sample-user-legacy"
607+
608+
XCTAssertTrue(amplitude.isSandboxEnabled())
609+
XCTAssertEqual(amplitude.getDeviceId(), deviceId)
610+
XCTAssertEqual(amplitude.getUserId(), userId)
611+
}
612+
#endif
613+
538614
func testInit_Offline() {
539615
XCTAssertEqual(Amplitude(configuration: configuration).configuration.offline, false)
540616
}

Tests/AmplitudeTests/Migration/RemnantDataMigrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class RemnantDataMigrationTests: XCTestCase {
4646
instanceName: instanceName,
4747
migrateLegacyData: migrateLegacyData
4848
)
49-
let amplitude = Amplitude(configuration: configuration)
49+
let amplitude = FakeAmplitudeWithSandboxEnabled(configuration: configuration)
5050

5151
let deviceId = "9B574574-74A7-4EDF-969D-164CB151B6C3"
5252
let userId = "ios-sample-user-legacy"

0 commit comments

Comments
 (0)