-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix crash during database migration_5_6 on Android 10 and below
- Loading branch information
1 parent
970a92e
commit 0b23eee
Showing
4 changed files
with
60 additions
and
3 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...src/main/java/com/mrsep/musicrecognizer/data/database/migration/DatabaseMigrationUtils.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,43 @@ | ||
package com.mrsep.musicrecognizer.data.database.migration | ||
|
||
import androidx.core.database.getStringOrNull | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
|
||
internal object DatabaseMigrationUtils { | ||
|
||
internal fun SupportSQLiteDatabase.isSQLiteVersionAtLeast(version: String): Boolean? { | ||
val thisVersion = querySQLiteVersion() ?: return null | ||
return runCatching { (compareSQLiteVersions(thisVersion, version) != -1) }.getOrNull() | ||
} | ||
|
||
private fun SupportSQLiteDatabase.querySQLiteVersion(): String? { | ||
return query("SELECT sqlite_version()").use { | ||
if (!it.moveToNext()) return null | ||
it.getStringOrNull(0) | ||
} | ||
} | ||
|
||
private fun compareSQLiteVersions(version1: String, version2: String): Int { | ||
val pattern = Regex("^\\d+(\\.\\d+)*\$") | ||
if (!pattern.matches(version1)) { | ||
throw IllegalArgumentException("Incorrect version format:$version1") | ||
} | ||
if (!pattern.matches(version2)) { | ||
throw IllegalArgumentException("Incorrect version format:$version2") | ||
} | ||
val partsThis = version1.split('.') | ||
val partsOther = version2.split('.') | ||
val maxLength = maxOf(partsThis.size, partsOther.size) | ||
for (i in 0 until maxLength) { | ||
val partThis = partsThis.getOrNull(i)?.toIntOrNull() ?: 0 | ||
val partOther = partsOther.getOrNull(i)?.toIntOrNull() ?: 0 | ||
|
||
when { | ||
partThis > partOther -> return 1 | ||
partThis < partOther -> return -1 | ||
} | ||
} | ||
return 0 | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Fixed crash during database migration on app startup on Android 10 and below |
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 @@ | ||
* Исправлен вылет во время миграции базы данных при запуске приложения на Android 10 и ниже |