Skip to content

Commit a021525

Browse files
[CM-2448] Crash : Table already exist | Android SDK (#628)
* added a check to skip existing table * refactored the tableExist code
1 parent 8504cfe commit a021525

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import kotlin.Throws
1111
object DatabaseMigrationHelper {
1212
private const val TEMP_ENCRYPTED_DB_NAME = "temp_encrypted.db"
1313

14+
// Check if table exists in destination DB
15+
private fun tableExists(db: SQLiteDatabase, tableName: String): Boolean {
16+
return db.rawQuery(
17+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name=? LIMIT 1",
18+
arrayOf(tableName)
19+
).use { cursor -> cursor.moveToFirst() }
20+
}
21+
1422
@JvmStatic
1523
@Throws(Exception::class)
1624
fun migrateDatabase(context: Context, dbName: String) {
@@ -83,6 +91,12 @@ object DatabaseMigrationHelper {
8391
continue
8492
}
8593

94+
// Verifies if table exist
95+
if (tableExists(destinationDb, tableName)) {
96+
println("Table $tableName already exists in destination DB, skipping creation.")
97+
continue
98+
}
99+
86100
// Copy table schema
87101
val createTableSql = getTableCreateSql(sourceDb, tableName)
88102
destinationDb.execSQL(createTableSql)

0 commit comments

Comments
 (0)