Skip to content

Commit

Permalink
fix: changes after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy committed Jul 19, 2023
1 parent b36587f commit c889b60
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
35 changes: 19 additions & 16 deletions Sources/Amplitude/Migration/RemnantDataMigration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,43 @@ class RemnantDataMigration {
}

if maxEventId > 0 {
let currentLastEventId = amplitude.sessions.lastEventId
if currentLastEventId <= 0 {
amplitude.sessions.lastEventId = maxEventId
let currentLastEventId: Int64? = amplitude.storage.read(key: StorageKey.LAST_EVENT_ID)
if currentLastEventId == nil || currentLastEventId! <= 0 {
try? amplitude.storage.write(key: StorageKey.LAST_EVENT_ID, value: maxEventId)
}
}
}

private func moveDeviceAndUserId() {
let deviceId = storage.getValue(RemnantDataMigration.DEVICE_ID_KEY)
let userId = storage.getValue(RemnantDataMigration.USER_ID_KEY)

if deviceId != nil {
amplitude.setDeviceId(deviceId: deviceId)
let currentDeviceId: String? = amplitude.storage.read(key: StorageKey.DEVICE_ID)
if currentDeviceId == nil || currentDeviceId! == "" {
if let deviceId = storage.getValue(RemnantDataMigration.DEVICE_ID_KEY) {
try? amplitude.storage.write(key: StorageKey.DEVICE_ID, value: deviceId)
}
}

if userId != nil {
amplitude.setUserId(userId: userId)
let currentUserId: String? = amplitude.storage.read(key: StorageKey.USER_ID)
if currentUserId == nil || currentUserId == "" {
if let userId = storage.getValue(RemnantDataMigration.USER_ID_KEY) {
try? amplitude.storage.write(key: StorageKey.USER_ID, value: userId)
}
}
}

private func moveSessionData() {
let currentSessionId = amplitude.sessions.sessionId
let currentLastEventTime = amplitude.sessions.lastEventTime
let currentSessionId: Int64? = amplitude.storage.read(key: StorageKey.PREVIOUS_SESSION_ID)
let currentLastEventTime: Int64? = amplitude.storage.read(key: StorageKey.LAST_EVENT_TIME)

let previousSessionId = storage.getLongValue(RemnantDataMigration.PREVIOUS_SESSION_ID_KEY)
let lastEventTime = storage.getLongValue(RemnantDataMigration.PREVIOUS_SESSION_TIME_KEY)

if currentSessionId < 0 && previousSessionId != nil && previousSessionId! >= 0 {
amplitude.sessions.sessionId = previousSessionId!
if (currentSessionId == nil || currentSessionId! < 0) && previousSessionId != nil && previousSessionId! >= 0 {
try? amplitude.storage.write(key: StorageKey.PREVIOUS_SESSION_ID, value: previousSessionId)
storage.removeLongValue(RemnantDataMigration.PREVIOUS_SESSION_ID_KEY)
}

if currentLastEventTime < 0 && lastEventTime != nil && lastEventTime! >= 0 {
amplitude.sessions.lastEventTime = lastEventTime!
if (currentLastEventTime == nil || currentLastEventTime! < 0) && lastEventTime != nil && lastEventTime! >= 0 {
try? amplitude.storage.write(key: StorageKey.LAST_EVENT_TIME, value: lastEventTime)
storage.removeLongValue(RemnantDataMigration.PREVIOUS_SESSION_TIME_KEY)
}
}
Expand Down
10 changes: 3 additions & 7 deletions Tests/AmplitudeTests/AmplitudeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ final class AmplitudeTests: XCTestCase {
let amplitude = Amplitude(configuration: configurationWithFakeStorage)
XCTAssertEqual(amplitude.getDeviceId() != nil, true)
let deviceIdUuid = amplitude.getDeviceId()!
XCTAssertEqual(storage.haveBeenCalledWith, [
"read(key: device_id)",
"read(key: user_id)",
"write(key: \(StorageKey.DEVICE_ID.rawValue), \(deviceIdUuid))"
])
XCTAssertEqual(storage.haveBeenCalledWith.last, "write(key: \(StorageKey.DEVICE_ID.rawValue), \(deviceIdUuid))")
}

func testContext() {
Expand Down Expand Up @@ -119,7 +115,7 @@ final class AmplitudeTests: XCTestCase {

amplitude.setUserId(userId: "test-user")
XCTAssertEqual(amplitude.getUserId(), "test-user")
XCTAssertEqual(storage.haveBeenCalledWith[3], "write(key: \(StorageKey.USER_ID.rawValue), test-user)")
XCTAssertEqual(storage.haveBeenCalledWith.last, "write(key: \(StorageKey.USER_ID.rawValue), test-user)")
}

func testSetDeviceId() {
Expand All @@ -129,7 +125,7 @@ final class AmplitudeTests: XCTestCase {

amplitude.setDeviceId(deviceId: "test-device")
XCTAssertEqual(amplitude.getDeviceId(), "test-device")
XCTAssertEqual(storage.haveBeenCalledWith[3], "write(key: \(StorageKey.DEVICE_ID.rawValue), test-device)")
XCTAssertEqual(storage.haveBeenCalledWith.last, "write(key: \(StorageKey.DEVICE_ID.rawValue), test-device)")
}

func testInterceptedIdentifyIsSentOnFlush() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ final class RemnantDataMigrationTests: XCTestCase {
let configuration = Configuration(
apiKey: apiKey,
instanceName: instanceName,
storageProvider: PersistentStorage(apiKey: apiKey, storagePrefix: "\(instanceName)-default"),
identifyStorageProvider: PersistentStorage(apiKey: apiKey, storagePrefix: "\(instanceName)-identify"),
storageProvider: PersistentStorage(storagePrefix: "\(instanceName)-default"),
identifyStorageProvider: PersistentStorage(storagePrefix: "\(instanceName)-identify"),
migrateLegacyData: migrateLegacyData
)
let amplitude = Amplitude(configuration: configuration)
Expand Down

0 comments on commit c889b60

Please sign in to comment.