Really fix database upgrade

This commit is contained in:
Matéo Duparc 2023-09-19 11:41:01 +02:00
parent 891a581329
commit 571a79cc1d
Signed by: hardcoresushi
GPG Key ID: AFE384344A45E13A
3 changed files with 20 additions and 14 deletions

View File

@ -37,8 +37,8 @@ android {
applicationId "sushi.hardcore.droidfs"
minSdkVersion 21
targetSdkVersion 32
versionCode 34
versionName "2.1.1"
versionCode 35
versionName "2.1.2"
ndk {
abiFilters "x86", "x86_64", "armeabi-v7a", "arm64-v8a"

View File

@ -9,7 +9,6 @@ import android.util.Log
import sushi.hardcore.droidfs.filesystems.EncryptedVolume
import sushi.hardcore.droidfs.util.PathUtils
import java.io.File
import java.util.UUID
class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Constants.VOLUME_DATABASE_NAME, null, 6) {
companion object {
@ -102,17 +101,23 @@ class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Co
volumeNames[i++] = cursor.getString(0)
}
cursor.close()
db.execSQL("ALTER TABLE $TABLE_NAME RENAME TO OLD;")
createTable(db)
val uuidsValues = volumeNames.indices.joinToString(", ") { "('${VolumeData.newUuid()}', ?)" }
// add uuids to old data
db.execSQL(
"INSERT INTO $TABLE_NAME " +
"WITH uuids($COLUMN_UUID, $COLUMN_NAME) AS (VALUES $uuidsValues) " +
"SELECT * FROM OLD NATURAL JOIN uuids;",
volumeNames
)
db.execSQL("DROP TABLE OLD;")
if (volumeNames.isEmpty()) {
db.execSQL("DROP TABLE $TABLE_NAME;")
createTable(db)
} else {
db.execSQL("ALTER TABLE $TABLE_NAME RENAME TO OLD;")
createTable(db)
val uuidsValues = volumeNames.indices.joinToString(", ") { "('${VolumeData.newUuid()}', ?)" }
// add uuids to old data
db.execSQL(
"INSERT INTO $TABLE_NAME " +
"WITH uuids($COLUMN_UUID, $COLUMN_NAME) AS (VALUES $uuidsValues) " +
"SELECT $COLUMN_UUID, OLD.$COLUMN_NAME, $COLUMN_HIDDEN, $COLUMN_TYPE, $COLUMN_HASH, $COLUMN_IV " +
"FROM OLD JOIN uuids ON OLD.name = uuids.name;",
volumeNames
)
db.execSQL("DROP TABLE OLD;")
}
}
}

View File

@ -0,0 +1 @@
- Really fix database upgrade crash