Skip to content

Commit

Permalink
fix: Do not migrate remnant data from legacy SDK when sandboxing is n…
Browse files Browse the repository at this point in the history
…ot enabled (#127)

* fix: Do not migrate remnant data from legacy SDK when sandboxing is not enabled
  • Loading branch information
izaaz committed Feb 28, 2024
1 parent dc544fb commit e92f6ed
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/Amplitude/Amplitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Amplitude {

migrateApiKeyStorages()
migrateDefaultInstanceStorages()
if configuration.migrateLegacyData && getStorageVersion() < .API_KEY_AND_INSTANCE_NAME {
if configuration.migrateLegacyData && getStorageVersion() < .API_KEY_AND_INSTANCE_NAME && isSandboxEnabled() {
RemnantDataMigration(self).execute()
}
migrateInstanceOnlyStorages()
Expand Down
76 changes: 76 additions & 0 deletions Tests/AmplitudeTests/AmplitudeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,82 @@ final class AmplitudeTests: XCTestCase {
}
#endif

func testRemnantDataNotMigratedInNonSandboxedApps() throws {
let instanceName = "legacy_v3_\(UUID().uuidString)".lowercased()
let bundle = Bundle(for: type(of: self))
let legacyDbUrl = bundle.url(forResource: "legacy_v3", withExtension: "sqlite")
let dbUrl = LegacyDatabaseStorage.getDatabasePath(instanceName)
let fileManager = FileManager.default
let legacyDbExists = legacyDbUrl != nil ? fileManager.fileExists(atPath: legacyDbUrl!.path) : false
XCTAssertTrue(legacyDbExists)

try fileManager.copyItem(at: legacyDbUrl!, to: dbUrl)

addTeardownBlock {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: dbUrl.path) {
try fileManager.removeItem(at: dbUrl)
}
}

let apiKey = "test-api-key"
let configuration = Configuration(
apiKey: apiKey,
instanceName: instanceName,
migrateLegacyData: true
)
let amplitude = Amplitude(configuration: configuration)

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

#if os(macOS)
// We don't want to transfer remnant data in non-sanboxed apps
XCTAssertFalse(amplitude.isSandboxEnabled())
XCTAssertNotEqual(amplitude.getDeviceId(), deviceId)
XCTAssertNotEqual(amplitude.getUserId(), userId)
#else
XCTAssertEqual(amplitude.getDeviceId(), deviceId)
XCTAssertEqual(amplitude.getUserId(), userId)
#endif
}

#if os(macOS)
func testRemnantDataNotMigratedInSandboxedMacApps() throws {
let instanceName = "legacy_v3_\(UUID().uuidString)".lowercased()
let bundle = Bundle(for: type(of: self))
let legacyDbUrl = bundle.url(forResource: "legacy_v3", withExtension: "sqlite")
let dbUrl = LegacyDatabaseStorage.getDatabasePath(instanceName)
let fileManager = FileManager.default
let legacyDbExists = legacyDbUrl != nil ? fileManager.fileExists(atPath: legacyDbUrl!.path) : false
XCTAssertTrue(legacyDbExists)

try fileManager.copyItem(at: legacyDbUrl!, to: dbUrl)

addTeardownBlock {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: dbUrl.path) {
try fileManager.removeItem(at: dbUrl)
}
}

let apiKey = "test-api-key"
let configuration = Configuration(
apiKey: apiKey,
instanceName: instanceName,
migrateLegacyData: true
)
let amplitude = FakeAmplitudeWithSandboxEnabled(configuration: configuration)

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

XCTAssertTrue(amplitude.isSandboxEnabled())
XCTAssertEqual(amplitude.getDeviceId(), deviceId)
XCTAssertEqual(amplitude.getUserId(), userId)
}
#endif

func testInit_Offline() {
XCTAssertEqual(Amplitude(configuration: configuration).configuration.offline, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class RemnantDataMigrationTests: XCTestCase {
instanceName: instanceName,
migrateLegacyData: migrateLegacyData
)
let amplitude = Amplitude(configuration: configuration)
let amplitude = FakeAmplitudeWithSandboxEnabled(configuration: configuration)

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

0 comments on commit e92f6ed

Please sign in to comment.