Skip to content

Commit 093b219

Browse files
Add exception handling for SQLite Cipher Database migration. (#624)
* Added Exception handling in database migration. * Added Database migration retries.
1 parent dd246e1 commit 093b219

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

kommunicate/src/main/java/io/kommunicate/commons/AppSpecificSettings.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ public class AppSpecificSettings {
2121
private static final String DATABASE_NAME = "DATABASE_NAME";
2222
private static final String ENABLE_TEXT_LOGGING = "ENABLE_TEXT_LOGGING";
2323
private static final String TEXT_LOG_FILE_NAME = "TEXT_LOG_FILE_NAME";
24-
private static final String AL_BASE_URL = "AL_BASE_URL";
24+
private static final String BASE_URL = "AL_BASE_URL";
2525
private static final String KM_BASE_URL = "KM_BASE_URL";
26-
private static final String AL_SUPPORT_EMAIL_ID = "AL_SUPPORT_EMAIL_ID";
26+
private static final String SUPPORT_EMAIL_ID = "AL_SUPPORT_EMAIL_ID";
2727
private static final String ENABLE_LOGGING_IN_RELEASE_BUILD = "ENABLE_LOGGING_IN_RELEASE_BUILD";
28-
private static final String AL_NOTIFICATION_AFTER_TIME = "AL_NOTIFICATION_AFTER_TIME";
28+
private static final String NOTIFICATION_AFTER_TIME = "AL_NOTIFICATION_AFTER_TIME";
29+
private static final String DATABASE_MIGRATION_RETRY_COUNT = "DATABASE_MIGRATION_RETRY_COUNT";
2930

3031
private AppSpecificSettings(Context context) {
3132
this.sharedPreferences = AppContextService.getContext(context).getSharedPreferences(MY_PREFERENCE, Context.MODE_PRIVATE);
@@ -66,11 +67,11 @@ public String getTextLogFileName() {
6667
}
6768

6869
public String getAlBaseUrl() {
69-
return sharedPreferences.getString(AL_BASE_URL, null);
70+
return sharedPreferences.getString(BASE_URL, null);
7071
}
7172

7273
public AppSpecificSettings setAlBaseUrl(String url) {
73-
sharedPreferences.edit().putString(AL_BASE_URL, url).commit();
74+
sharedPreferences.edit().putString(BASE_URL, url).commit();
7475
return this;
7576
}
7677

@@ -84,11 +85,11 @@ public AppSpecificSettings setKmBaseUrl(String url) {
8485
}
8586

8687
public String getSupportEmailId() {
87-
return sharedPreferences.getString(AL_SUPPORT_EMAIL_ID, APPLOZIC_SUPPORT);
88+
return sharedPreferences.getString(SUPPORT_EMAIL_ID, APPLOZIC_SUPPORT);
8889
}
8990

9091
public AppSpecificSettings setSupportEmailId(String emailId) {
91-
sharedPreferences.edit().putString(AL_SUPPORT_EMAIL_ID, emailId).commit();
92+
sharedPreferences.edit().putString(SUPPORT_EMAIL_ID, emailId).commit();
9293
return this;
9394
}
9495

@@ -102,16 +103,25 @@ public boolean isLoggingEnabledForReleaseBuild() {
102103
}
103104

104105
public AppSpecificSettings setNotificationAfterTime(long notificationAfterTime) {
105-
sharedPreferences.edit().putLong(AL_NOTIFICATION_AFTER_TIME, notificationAfterTime).commit();
106+
sharedPreferences.edit().putLong(NOTIFICATION_AFTER_TIME, notificationAfterTime).commit();
106107
return this;
107108
}
108109

109110
public boolean isAllNotificationMuted() {
110-
long notificationAfterTime = sharedPreferences.getLong(AL_NOTIFICATION_AFTER_TIME, 0);
111+
long notificationAfterTime = sharedPreferences.getLong(NOTIFICATION_AFTER_TIME, 0);
111112
Date date = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
112113
return (notificationAfterTime - date.getTime() > 0);
113114
}
114115

116+
public int getCurrentDatabaseMigrationRetryCount() {
117+
return sharedPreferences.getInt(DATABASE_MIGRATION_RETRY_COUNT, 0);
118+
}
119+
120+
public AppSpecificSettings setCurrentDatabaseMigrationRetryCount(int databaseMigrationRetryCount) {
121+
sharedPreferences.edit().putInt(DATABASE_MIGRATION_RETRY_COUNT, databaseMigrationRetryCount).apply();
122+
return this;
123+
}
124+
115125
public boolean clearAll() {
116126
if (sharedPreferences != null) {
117127
return sharedPreferences.edit().clear().commit();

kommunicate/src/main/java/io/kommunicate/database/DatabaseMigrationHelper.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import io.kommunicate.devkit.api.MobiComKitClientService
66
import io.kommunicate.commons.AppContextService
77
import net.sqlcipher.database.SQLiteDatabase
88
import net.sqlcipher.database.SQLiteException
9+
import kotlin.Throws
910

1011
object DatabaseMigrationHelper {
1112
private const val TEMP_ENCRYPTED_DB_NAME = "temp_encrypted.db"
1213

1314
@JvmStatic
15+
@Throws(Exception::class)
1416
fun migrateDatabase(context: Context, dbName: String) {
1517
val databaseName = if(context.getDatabasePath(dbName).exists()) {
1618
dbName
@@ -70,6 +72,7 @@ object DatabaseMigrationHelper {
7072
}
7173

7274
// Copy tables and data from one database to another
75+
@Throws(Exception::class)
7376
private fun copyDataBetweenDatabases(sourceDb: SQLiteDatabase, destinationDb: SQLiteDatabase) {
7477
val cursor: Cursor =
7578
sourceDb.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null)

kommunicate/src/main/java/io/kommunicate/devkit/database/MobiComDatabaseHelper.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import io.kommunicate.commons.commons.core.utils.Utils;
1515

1616
import io.kommunicate.database.DatabaseMigrationHelper;
17+
import io.sentry.Hint;
18+
import io.sentry.Sentry;
1719

1820
public class MobiComDatabaseHelper extends SQLiteOpenHelper {
1921

@@ -247,6 +249,7 @@ public class MobiComDatabaseHelper extends SQLiteOpenHelper {
247249
private static final String TAG = "MobiComDatabaseHelper";
248250
private static MobiComDatabaseHelper sInstance;
249251
private Context context;
252+
private static final int MAX_DATABASE_MIGRATION_RETRY_COUNT = 3;
250253

251254
private MobiComDatabaseHelper(Context context) {
252255
this(context, !TextUtils.isEmpty(AppSpecificSettings.getInstance(AppContextService.getContext(context)).getDatabaseName()) ? AppSpecificSettings.getInstance(AppContextService.getContext(context)).getDatabaseName() : "MCK_" + MobiComKitClientService.getApplicationKey(AppContextService.getContext(context)), null, DB_VERSION);
@@ -256,8 +259,17 @@ private MobiComDatabaseHelper(Context context) {
256259
public MobiComDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
257260
super(context, name, factory, version);
258261
SQLiteDatabase.loadLibs(context);
259-
if (!DBUtils.isDatabaseEncrypted(context, name)) {
260-
DatabaseMigrationHelper.migrateDatabase(context, name);
262+
AppSpecificSettings appSpecificSettings = AppSpecificSettings.getInstance(context);
263+
int currentRetryCount = appSpecificSettings.getCurrentDatabaseMigrationRetryCount();
264+
if (!DBUtils.isDatabaseEncrypted(context, name) && currentRetryCount < MAX_DATABASE_MIGRATION_RETRY_COUNT) {
265+
try {
266+
DatabaseMigrationHelper.migrateDatabase(context, name);
267+
} catch (Exception e) {
268+
// Database Exception handled.
269+
Sentry.captureException(e);
270+
}
271+
currentRetryCount+=1;
272+
appSpecificSettings.setCurrentDatabaseMigrationRetryCount(currentRetryCount);
261273
}
262274
}
263275

0 commit comments

Comments
 (0)