Skip to content

Commit 49ed920

Browse files
committed
update outcomes table to include session_time column
* Update database version to v9 and make migration. * I default this new `session_time` to a value of `1` instead of `0` intentionally to work around an issue from the v5.0.0-beta's for migration's sake and get these outcomes migrated, sent, and done with. * When outcome requests fail, they are cached for retrying. The v5.0.0-beta's likely encountered failure responses and cached these. If there are cached `os__session_duration` outcomes and we send them off with zero session_time, the server will keep sending us a failure response. So, let's just send a time of 1 second.
1 parent 43fa3cc commit 49ed920

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/database/impl/OSDatabase.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.onesignal.core.internal.database.IDatabase
1616
import com.onesignal.debug.internal.logging.Logging
1717
import com.onesignal.session.internal.outcomes.impl.OutcomeTableProvider
1818
import com.onesignal.session.internal.outcomes.impl.OutcomesDbContract.SQL_CREATE_OUTCOME_ENTRIES_V1
19-
import com.onesignal.session.internal.outcomes.impl.OutcomesDbContract.SQL_CREATE_OUTCOME_ENTRIES_V3
19+
import com.onesignal.session.internal.outcomes.impl.OutcomesDbContract.SQL_CREATE_OUTCOME_ENTRIES_V4
2020
import com.onesignal.session.internal.outcomes.impl.OutcomesDbContract.SQL_CREATE_UNIQUE_OUTCOME_ENTRIES_V1
2121
import com.onesignal.session.internal.outcomes.impl.OutcomesDbContract.SQL_CREATE_UNIQUE_OUTCOME_ENTRIES_V2
2222

@@ -246,7 +246,7 @@ internal open class OSDatabase(
246246

247247
override fun onCreate(db: SQLiteDatabase) {
248248
db.execSQL(SQL_CREATE_ENTRIES)
249-
db.execSQL(SQL_CREATE_OUTCOME_ENTRIES_V3)
249+
db.execSQL(SQL_CREATE_OUTCOME_ENTRIES_V4)
250250
db.execSQL(SQL_CREATE_UNIQUE_OUTCOME_ENTRIES_V2)
251251
db.execSQL(SQL_CREATE_IN_APP_MESSAGE_ENTRIES)
252252
for (ind in SQL_INDEX_ENTRIES) {
@@ -277,6 +277,7 @@ internal open class OSDatabase(
277277
if (oldVersion == 5 && newVersion >= 6) upgradeFromV5ToV6(db)
278278
if (oldVersion < 7 && newVersion >= 7) upgradeToV7(db)
279279
if (oldVersion < 8 && newVersion >= 8) upgradeToV8(db)
280+
if (oldVersion < 9 && newVersion >= 9) upgradeToV9(db)
280281
}
281282

282283
// Add collapse_id field and index
@@ -342,6 +343,10 @@ internal open class OSDatabase(
342343
_outcomeTableProvider.upgradeCacheOutcomeTableRevision1To2(db)
343344
}
344345

346+
private fun upgradeToV9(db: SQLiteDatabase) {
347+
_outcomeTableProvider.upgradeOutcomeTableRevision3To4(db)
348+
}
349+
345350
override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
346351
Logging.warn("SDK version rolled back! Clearing $DATABASE_NAME as it could be in an unexpected state.")
347352

@@ -357,7 +362,7 @@ internal open class OSDatabase(
357362
}
358363

359364
companion object {
360-
private const val dbVersion = 8
365+
private const val dbVersion = 9
361366
private val LOCK = Any()
362367
private const val DATABASE_NAME = "OneSignal.db"
363368
private const val INTEGER_PRIMARY_KEY_TYPE = " INTEGER PRIMARY KEY"

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/impl/OutcomeTableProvider.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ internal class OutcomeTableProvider {
7878
}
7979
}
8080

81+
/**
82+
* On the outcome table this adds the new session_time column.
83+
*
84+
* @param db
85+
*/
86+
fun upgradeOutcomeTableRevision3To4(db: SQLiteDatabase) {
87+
try {
88+
db.execSQL("BEGIN TRANSACTION;")
89+
db.execSQL("ALTER TABLE " + OutcomeEventsTable.TABLE_NAME + " ADD COLUMN " + OutcomeEventsTable.COLUMN_NAME_SESSION_TIME + " INTEGER DEFAULT 1;")
90+
// We intentionally choose to default session_time to 1 to address a bug on cached outcomes from v5.0.0-beta's
91+
// os__session_duration requests expect a session_time and these will keep failing and caching, so let's just send them with a time of 1 for migrations
92+
} catch (e: SQLiteException) {
93+
e.printStackTrace()
94+
} finally {
95+
db.execSQL("COMMIT;")
96+
}
97+
}
98+
8199
/**
82100
* On the cache unique outcome table rename table, rename column notification id to influence id
83101
* Add column channel type

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/impl/OutcomesDbContract.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ internal object OutcomeEventsTable {
2020
const val COLUMN_NAME_WEIGHT = "weight" // Added on DB v5 SDK v3.12.1, migration added on DB v6 SDK v3.12.2
2121
const val COLUMN_NAME_TIMESTAMP = "timestamp" // Added on DB v4 SDK v3.12.0
2222
const val COLUMN_NAME_PARAMS = "params" // Added on DB v4 SDK v3.12.0 replaced with weight on DB v5 SDK v3.12.1, migration added on DB v6 SDK v3.12.2
23+
24+
// Session time
25+
const val COLUMN_NAME_SESSION_TIME = "session_time" // Added on DB v9 SDK v5.0.0 (note that 5.0.0-beta's were still on v8)
2326
}
2427

2528
internal object CachedUniqueOutcomeTable {
@@ -72,6 +75,22 @@ internal object OutcomesDbContract {
7275
OutcomeEventsTable.COLUMN_NAME_TIMESTAMP + TIMESTAMP_TYPE + "," + // "params TEXT" Added in v4, removed in v5.
7376
OutcomeEventsTable.COLUMN_NAME_WEIGHT + FLOAT_TYPE + // New in v5, missing migration added in v6
7477
");"
78+
79+
/**
80+
* Adds a new column called session_time
81+
*/
82+
const val SQL_CREATE_OUTCOME_ENTRIES_V4 = "CREATE TABLE " + OutcomeEventsTable.TABLE_NAME + " (" +
83+
OutcomeEventsTable.ID + INTEGER_PRIMARY_KEY_TYPE + "," +
84+
OutcomeEventsTable.COLUMN_NAME_NOTIFICATION_INFLUENCE_TYPE + TEXT_TYPE + "," +
85+
OutcomeEventsTable.COLUMN_NAME_IAM_INFLUENCE_TYPE + TEXT_TYPE + "," +
86+
OutcomeEventsTable.COLUMN_NAME_NOTIFICATION_IDS + TEXT_TYPE + "," +
87+
OutcomeEventsTable.COLUMN_NAME_IAM_IDS + TEXT_TYPE + "," +
88+
OutcomeEventsTable.COLUMN_NAME_NAME + TEXT_TYPE + "," +
89+
OutcomeEventsTable.COLUMN_NAME_TIMESTAMP + TIMESTAMP_TYPE + "," +
90+
OutcomeEventsTable.COLUMN_NAME_WEIGHT + FLOAT_TYPE + "," +
91+
OutcomeEventsTable.COLUMN_NAME_SESSION_TIME + INT_TYPE +
92+
");"
93+
7594
const val SQL_CREATE_UNIQUE_OUTCOME_ENTRIES_V1 = "CREATE TABLE " + CachedUniqueOutcomeTable.TABLE_NAME_V1 + " (" +
7695
CachedUniqueOutcomeTable.ID + INTEGER_PRIMARY_KEY_TYPE + "," +
7796
CachedUniqueOutcomeTable.COLUMN_NAME_NOTIFICATION_ID + TEXT_TYPE + "," +

0 commit comments

Comments
 (0)