File tree Expand file tree Collapse file tree 5 files changed +116
-0
lines changed
OneSignalSDK/onesignal/core/src
main/java/com/onesignal/user
test/java/com/onesignal/user/internal/migrations Expand file tree Collapse file tree 5 files changed +116
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import com.onesignal.user.internal.backend.impl.UserBackendService
17
17
import com.onesignal.user.internal.builduser.IRebuildUserService
18
18
import com.onesignal.user.internal.builduser.impl.RebuildUserService
19
19
import com.onesignal.user.internal.identity.IdentityModelStore
20
+ import com.onesignal.user.internal.migrations.RecoverConfigPushSubscription
20
21
import com.onesignal.user.internal.migrations.RecoverFromDroppedLoginBug
21
22
import com.onesignal.user.internal.operations.impl.executors.IdentityOperationExecutor
22
23
import com.onesignal.user.internal.operations.impl.executors.LoginUserFromSubscriptionOperationExecutor
@@ -74,6 +75,7 @@ internal class UserModule : IModule {
74
75
builder.register<UserRefreshService >().provides<IStartableService >()
75
76
76
77
builder.register<RecoverFromDroppedLoginBug >().provides<IStartableService >()
78
+ builder.register<RecoverConfigPushSubscription >().provides<IStartableService >()
77
79
78
80
// Shared state between Executors
79
81
builder.register<NewRecordsState >().provides<NewRecordsState >()
Original file line number Diff line number Diff line change
1
+ package com.onesignal.user.internal.migrations
2
+
3
+ import com.onesignal.core.internal.startup.IStartableService
4
+
5
+ /* *
6
+ * Purpose: allow to identify and take corrective action for some specific bad states during migration
7
+ *
8
+ * Implement these properties to:
9
+ * - isInBadState(): return true the condition for the bad state has met
10
+ * - recover(): take a recovery action if the bad state has been identified
11
+ * - recoveryMessage: log a message after the bad state is found and the corrective action is taken
12
+ */
13
+ interface IMigrationRecovery : IStartableService {
14
+ fun isInBadState (): Boolean
15
+
16
+ fun recover ()
17
+
18
+ fun recoveryMessage (): String
19
+ }
Original file line number Diff line number Diff line change
1
+ package com.onesignal.user.internal.migrations
2
+
3
+ import com.onesignal.debug.internal.logging.Logging
4
+
5
+ open class MigrationRecovery : IMigrationRecovery {
6
+ override fun isInBadState (): Boolean {
7
+ return false
8
+ }
9
+
10
+ override fun recover () {
11
+ // left blank intentionally
12
+ }
13
+
14
+ override fun recoveryMessage (): String {
15
+ return " "
16
+ }
17
+
18
+ override fun start () {
19
+ // log a message and take the corrective action if it is determined that app is in a bad state
20
+ if (isInBadState()) {
21
+ Logging .warn(recoveryMessage())
22
+ recover()
23
+ }
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package com.onesignal.user.internal.migrations
2
+
3
+ import com.onesignal.core.internal.config.ConfigModelStore
4
+ import com.onesignal.user.internal.subscriptions.SubscriptionModelStore
5
+ import com.onesignal.user.internal.subscriptions.SubscriptionType
6
+
7
+ /* *
8
+ * Purpose: Automatically recovers a missing push sub ID in the config model store due
9
+ * to a bug migrating from the SDK 5.2.0-beta
10
+ */
11
+ class RecoverConfigPushSubscription (
12
+ private val _configModelStore : ConfigModelStore ,
13
+ private val _subscriptionModelStore : SubscriptionModelStore ,
14
+ ) : MigrationRecovery() {
15
+ val activePushSubscription by lazy { _subscriptionModelStore .list().firstOrNull { it.type == SubscriptionType .PUSH } }
16
+
17
+ override fun isInBadState (): Boolean {
18
+ val isPushSubIdMissing = _configModelStore .model.pushSubscriptionId == null
19
+ return isPushSubIdMissing && activePushSubscription != null
20
+ }
21
+
22
+ override fun recover () {
23
+ _configModelStore .model.pushSubscriptionId = activePushSubscription?.id
24
+ }
25
+
26
+ override fun recoveryMessage (): String {
27
+ return " Recovering missing push subscription ID in the config model store."
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ package com.onesignal.user.internal.migrations
2
+
3
+ import com.onesignal.debug.LogLevel
4
+ import com.onesignal.debug.internal.logging.Logging
5
+ import com.onesignal.mocks.MockHelper
6
+ import com.onesignal.user.internal.subscriptions.SubscriptionModel
7
+ import com.onesignal.user.internal.subscriptions.SubscriptionModelStore
8
+ import com.onesignal.user.internal.subscriptions.SubscriptionType
9
+ import io.kotest.core.spec.style.FunSpec
10
+ import io.mockk.every
11
+ import io.mockk.mockk
12
+ import io.mockk.verify
13
+
14
+ class MigrationRecoveryTests : FunSpec ({
15
+ beforeAny {
16
+ Logging .logLevel = LogLevel .NONE
17
+ }
18
+
19
+ test("ensure RecoverConfigPushSubscription adds the missing push sub ID ") {
20
+ // Given
21
+ val configModelStore = MockHelper .configModelStore()
22
+ val mockSubscriptionModelStore = mockk<SubscriptionModelStore >()
23
+ val recovery = RecoverConfigPushSubscription (configModelStore, mockSubscriptionModelStore)
24
+
25
+ configModelStore.model.let { it.pushSubscriptionId = null }
26
+
27
+ // add a push subscription `
28
+ val pushSubscription = SubscriptionModel ()
29
+ pushSubscription.type = SubscriptionType .PUSH
30
+ pushSubscription.id = " 999"
31
+ every { mockSubscriptionModelStore.list() } returns listOf(pushSubscription)
32
+
33
+ // When
34
+ recovery.start()
35
+
36
+ // Then
37
+ verify {
38
+ configModelStore.model.pushSubscriptionId = pushSubscription.id
39
+ }
40
+ }
41
+ })
You can’t perform that action at this time.
0 commit comments