-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrating storage to version v3 (#226)
* feat: Migrate storage to v3
- Loading branch information
Showing
32 changed files
with
1,169 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
android/src/main/java/com/amplitude/android/migration/AndroidStorageMigration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.amplitude.android.migration | ||
|
||
import com.amplitude.android.storage.AndroidStorageV2 | ||
import com.amplitude.common.Logger | ||
import com.amplitude.core.Storage | ||
import com.amplitude.core.utilities.toEvents | ||
import org.json.JSONArray | ||
|
||
class AndroidStorageMigration( | ||
private val source: AndroidStorageV2, | ||
private val destination: AndroidStorageV2, | ||
private val logger: Logger | ||
) { | ||
suspend fun execute() { | ||
moveEventsToDestination() | ||
moveSimpleValues() | ||
} | ||
|
||
private suspend fun moveEventsToDestination() { | ||
try { | ||
source.rollover() | ||
val sourceEventFiles = source.readEventsContent() as List<String> | ||
if (sourceEventFiles.isEmpty()) { | ||
source.cleanupMetadata() | ||
return | ||
} | ||
|
||
for (sourceEventFilePath in sourceEventFiles) { | ||
val events = source.getEventsString(sourceEventFilePath) | ||
var count = 0 | ||
val baseEvents = JSONArray(events).toEvents() | ||
for (event in baseEvents) { | ||
try { | ||
count++ | ||
destination.writeEvent(event) | ||
} catch (e: Exception) { | ||
logger.error("can't move event ($event) from file $sourceEventFilePath: ${e.message}") | ||
} | ||
} | ||
logger.debug("Migrated $count/${baseEvents.size} events from $sourceEventFilePath") | ||
source.removeFile(sourceEventFilePath) | ||
} | ||
source.cleanupMetadata() | ||
destination.rollover() | ||
} catch (e: Exception) { | ||
logger.error("can't move event files: ${e.message}") | ||
} | ||
} | ||
|
||
private suspend fun moveSimpleValues() { | ||
moveSimpleValue(Storage.Constants.PREVIOUS_SESSION_ID) | ||
moveSimpleValue(Storage.Constants.LAST_EVENT_TIME) | ||
moveSimpleValue(Storage.Constants.LAST_EVENT_ID) | ||
|
||
moveSimpleValue(Storage.Constants.OPT_OUT) | ||
moveSimpleValue(Storage.Constants.Events) | ||
moveSimpleValue(Storage.Constants.APP_VERSION) | ||
moveSimpleValue(Storage.Constants.APP_BUILD) | ||
} | ||
|
||
private suspend fun moveSimpleValue(key: Storage.Constants) { | ||
try { | ||
val sourceValue = source.read(key) ?: return | ||
val destinationValue = destination.read(key) | ||
if (destinationValue == null) { | ||
try { | ||
logger.debug("Migrating $key with value $sourceValue") | ||
destination.write(key, sourceValue) | ||
} catch (e: Exception) { | ||
logger.error("can't write destination $key: ${e.message}") | ||
return | ||
} | ||
} | ||
source.remove(key) | ||
} catch (e: Exception) { | ||
logger.error("can't move $key: ${e.message}") | ||
} | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
android/src/main/java/com/amplitude/android/migration/ApiKeyStorageMigration.kt
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
android/src/main/java/com/amplitude/android/migration/IdentityStorageMigration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.amplitude.android.migration | ||
|
||
import com.amplitude.common.Logger | ||
import com.amplitude.id.IdentityStorage | ||
|
||
class IdentityStorageMigration( | ||
private val source: IdentityStorage, | ||
private val destination: IdentityStorage, | ||
private val logger: Logger | ||
) { | ||
fun execute() { | ||
try { | ||
val identity = source.load() | ||
logger.debug("Loaded old identity: $identity") | ||
if (identity.userId != null) { | ||
destination.saveUserId(identity.userId) | ||
} | ||
if (identity.deviceId != null) { | ||
destination.saveDeviceId(identity.deviceId) | ||
} | ||
source.delete() | ||
} catch (e: Exception) { | ||
logger.error("Unable to migrate file identity storage: ${e.message}") | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
android/src/main/java/com/amplitude/android/migration/MigrationManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.amplitude.android.migration | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import com.amplitude.android.Amplitude | ||
import com.amplitude.android.Configuration | ||
import com.amplitude.android.storage.AndroidStorageContextV1 | ||
import com.amplitude.android.storage.AndroidStorageContextV2 | ||
import com.amplitude.android.storage.LegacySdkStorageContext | ||
import com.amplitude.android.storage.StorageVersion | ||
import com.amplitude.common.Logger | ||
|
||
internal class MigrationManager(private val amplitude: Amplitude) { | ||
private val sharedPreferences: SharedPreferences | ||
private val config: Configuration = amplitude.configuration as Configuration | ||
private val logger: Logger = amplitude.logger | ||
private val currentStorageVersion: Int | ||
|
||
init { | ||
sharedPreferences = config.context.getSharedPreferences( | ||
"amplitude-android-${config.instanceName}", | ||
Context.MODE_PRIVATE | ||
) | ||
currentStorageVersion = sharedPreferences.getInt("storage_version", 0) | ||
} | ||
|
||
suspend fun migrateOldStorage() { | ||
if (currentStorageVersion < StorageVersion.V3.rawValue) { | ||
logger.debug("Migrating storage to version ${StorageVersion.V3.rawValue}") | ||
safePerformMigration() | ||
} else { | ||
amplitude.logger.debug("Storage already at version ${StorageVersion.V3.rawValue}") | ||
} | ||
} | ||
|
||
internal suspend fun safePerformMigration() { | ||
try { | ||
val config = amplitude.configuration as Configuration | ||
if (config.migrateLegacyData) { | ||
val legacySdkStorageContext = LegacySdkStorageContext(amplitude) | ||
legacySdkStorageContext.migrateToLatestVersion() | ||
} | ||
|
||
val storageContextV1 = AndroidStorageContextV1(amplitude, config) | ||
storageContextV1.migrateToLatestVersion() | ||
|
||
val storageContextV2 = AndroidStorageContextV2(amplitude, config) | ||
storageContextV2.migrateToLatestVersion() | ||
|
||
sharedPreferences.edit().putInt("storage_version", StorageVersion.V3.rawValue).apply() | ||
} catch (ex: Throwable) { | ||
logger.error("Failed to migrate storage: ${ex.message}") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.