Skip to content

Commit

Permalink
chore: re-add lastEventId to RemnantDataMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-fiedler committed Mar 22, 2024
1 parent 5dfba6c commit 2aa0afa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class RemnantDataMigration(
companion object {
const val DEVICE_ID_KEY = "device_id"
const val USER_ID_KEY = "user_id"
const val LAST_EVENT_ID_KEY = "last_event_id"
}

lateinit var databaseStorage: DatabaseStorage
Expand All @@ -32,6 +33,7 @@ class RemnantDataMigration(

// WARNING: We don't migrate session data as we want to reset on a new app install
moveDeviceAndUserId()
moveTimelineData()

if (firstRunSinceUpgrade) {
moveInterceptedIdentifies()
Expand Down Expand Up @@ -64,6 +66,23 @@ class RemnantDataMigration(
}
}

private suspend fun moveTimelineData() {
try {
val currentLastEventId = amplitude.storage.read(Storage.Constants.LAST_EVENT_ID)?.toLongOrNull()

val lastEventId = databaseStorage.getLongValue(LAST_EVENT_ID_KEY)

if (currentLastEventId == null && lastEventId != null) {
amplitude.storage.write(Storage.Constants.LAST_EVENT_ID, lastEventId.toString())
databaseStorage.removeLongValue(LAST_EVENT_ID_KEY)
}
} catch (e: Exception) {
LogcatLogger.logger.error(
"timeline data migration failed: ${e.message}"
)
}
}

private suspend fun moveEvents() {
try {
val remnantEvents = databaseStorage.readEventsContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RemnantDataMigrationTest {

@Test
fun `legacy data version 4 should be migrated`() {
checkLegacyDataMigration("legacy_v4.sqlite", 4)
checkLegacyDataMigration("legacy_v4.sqlite", 4, true, false)
}

@Test
Expand Down Expand Up @@ -113,7 +113,7 @@ class RemnantDataMigrationTest {
} else {
// Session start event has not been processed yet, so this is null
Assertions.assertEquals(
null,
if (!migrateLegacyData) null else 2,
amplitude.storage.read(Storage.Constants.LAST_EVENT_ID)?.toLongOrNull()
)
}
Expand Down

0 comments on commit 2aa0afa

Please sign in to comment.