Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add remnant events migration plugin #40

Merged
merged 33 commits into from
May 26, 2023
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
85a28b1
feat: add remnant events migration plugin
liuyang1520 Jun 24, 2022
2f998b3
delete events after migration
liuyang1520 Jun 25, 2022
68dfbd8
fix lint errors and add test file
liuyang1520 Jun 25, 2022
8cf06c2
slight refactor and add unit tests
liuyang1520 Jun 27, 2022
63debca
attempt to fix unit test failure
liuyang1520 Jun 27, 2022
2bfb684
attempt to fix unit test failure
liuyang1520 Jun 27, 2022
1bb7ab4
Merge branch 'main' of github.com:amplitude/Amplitude-Kotlin into add…
liuyang1520 Jun 27, 2022
576e2c6
use testImplementation instead of core, remove extension of Storage i…
liuyang1520 Jun 30, 2022
aec8802
Merge branch 'main' of github.com:amplitude/Amplitude-Kotlin into add…
liuyang1520 Jun 30, 2022
acc6857
Merge branch 'main' into add-remnant-events-migration-plugin
falconandy May 11, 2023
d760de4
added getDeviceId()/getUserId() methods, added instance name support
falconandy May 12, 2023
2843610
fix lint warnings
falconandy May 12, 2023
6c573cd
fix plugin logic (database version, library format)
falconandy May 12, 2023
3f3dee0
extend plugin logic (device/user id, identifies, intercepted identifies)
falconandy May 16, 2023
4a40c43
added support for db version 3
falconandy May 17, 2023
1706974
added test for db version 3
falconandy May 18, 2023
62f4337
move migration logic to 'migration' package
falconandy May 18, 2023
f7870ed
added test for missing legacy db
falconandy May 18, 2023
c0558c4
move MigrationPlugin to Initializer
falconandy May 18, 2023
9df4087
restore session data, do not clean device/user id, support first/next…
falconandy May 18, 2023
dfece83
Merge branch 'main' into add-remnant-events-migration-plugin
falconandy May 19, 2023
f97b7f7
fix tests
falconandy May 19, 2023
866cd6d
add short delay after each test to complete asynchronous handlers - t…
falconandy May 19, 2023
334b115
remove short delay after each test to complete asynchronous handlers …
falconandy May 22, 2023
3efc51e
add try/catch for each migrated event
falconandy May 22, 2023
d0cad5e
refactoring: added flag 'migrateLegacyData' instead of initializers
falconandy May 23, 2023
7ad4ad5
add test for migrateLegacyData=false
falconandy May 23, 2023
089c4fc
remove unnecessary permission READ_PHONE_STATE
falconandy May 24, 2023
d2386f7
move legacy 'uuid' field to 'insert_id'
falconandy May 24, 2023
2469017
make `build()` protected open for backward compatibility
falconandy May 24, 2023
848e1e9
write converted legacy event instead of json representation
falconandy May 25, 2023
d6c13d8
fix some "simple" build warnings
falconandy May 26, 2023
104e91d
make migrateLegacyData writable to set in Java code
falconandy May 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix lint errors and add test file
  • Loading branch information
liuyang1520 committed Jun 25, 2022
commit 68dfbd890a902e59e72aa0664c8d67b59831fc71
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.amplitude.android.utilities

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class DatabaseStorageTest {
var databaseStorage: DatabaseStorage? = null

@Before
@Throws(Exception::class)
fun setUp() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
databaseStorage = DatabaseStorage(appContext)
}

@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Assert.assertEquals("com.amplitude.android.test", appContext.packageName)
}
}
Original file line number Diff line number Diff line change
@@ -5,12 +5,13 @@ import com.amplitude.android.utilities.DatabaseStorageProvider
import com.amplitude.common.android.AndroidContextProvider
import com.amplitude.common.android.LogcatLogger
import com.amplitude.core.Amplitude
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.events.Plan
import com.amplitude.core.platform.Plugin
import org.json.JSONObject
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.utilities.*
import com.amplitude.core.utilities.optionalJSONObject
import com.amplitude.core.utilities.optionalString
import org.json.JSONArray
import org.json.JSONObject
import java.math.BigDecimal

/**
@@ -33,6 +34,7 @@ class RemnantEventsMigrationPlugin : Plugin {
AndroidContextProvider(configuration.context, configuration.locationListening)

val databaseStorage = DatabaseStorageProvider().getStorage(amplitude)

@Suppress("UNCHECKED_CAST")
val remnantEvents = databaseStorage.readEventsContent() as List<JSONObject>
var maxMigratedEventId: Long = 0
@@ -92,7 +94,7 @@ internal fun JSONObject.toBaseEvent(): BaseEvent {
event.idfv = this.optionalString("idfv", null)
event.adid = this.optionalString("adid", null)
event.androidId = this.optionalString("android_id", null)
event.appSetId = this.optString("android_app_set_id", null)
event.appSetId = this.optionalString("android_app_set_id", null)
event.eventId = if (this.has("event_id")) this.getLong("event_id") else null
event.sessionId = this.getLong("session_id")
event.insertId = this.optionalString("uuid", null) // name change
Original file line number Diff line number Diff line change
@@ -6,15 +6,18 @@ import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteException
import android.database.sqlite.SQLiteOpenHelper
import com.amplitude.common.android.LogcatLogger
import com.amplitude.core.*
import com.amplitude.core.Amplitude
import com.amplitude.core.Configuration
import com.amplitude.core.Storage
import com.amplitude.core.StorageProvider
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.platform.EventPipeline
import com.amplitude.core.utilities.ResponseHandler
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import org.json.JSONObject
import java.io.File
import java.util.*
import java.util.LinkedList

/**
* Store the database related constants.
@@ -73,10 +76,7 @@ class DatabaseStorage(context: Context) : Storage, SQLiteOpenHelper(

private fun convertIfCursorWindowException(e: java.lang.RuntimeException) {
val message = e.message
if (!message.isNullOrEmpty() && (message.startsWith("Cursor window allocation of") || message.startsWith(
"Could not allocate CursorWindow"
))
) {
if (!message.isNullOrEmpty() && (message.startsWith("Cursor window allocation of") || message.startsWith("Could not allocate CursorWindow"))) {
throw CursorWindowAllocationException(message)
} else {
throw e
@@ -145,7 +145,7 @@ class DatabaseStorage(context: Context) : Storage, SQLiteOpenHelper(
"read events from ${DatabaseConstants.EVENT_TABLE_NAME} failed: ${e.message}"
)
delete()
} catch (e: IllegalStateException) { // put before Runtime since IllegalState extends
} catch (e: IllegalStateException) { // put before Runtime since IllegalState extends
handleIfCursorRowTooLargeException(e)
} catch (e: RuntimeException) {
convertIfCursorWindowException(e)