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" applicationId "sushi.hardcore.droidfs"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 32 targetSdkVersion 32
versionCode 34 versionCode 35
versionName "2.1.1" versionName "2.1.2"
ndk { ndk {
abiFilters "x86", "x86_64", "armeabi-v7a", "arm64-v8a" 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.filesystems.EncryptedVolume
import sushi.hardcore.droidfs.util.PathUtils import sushi.hardcore.droidfs.util.PathUtils
import java.io.File import java.io.File
import java.util.UUID
class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Constants.VOLUME_DATABASE_NAME, null, 6) { class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Constants.VOLUME_DATABASE_NAME, null, 6) {
companion object { companion object {
@ -102,17 +101,23 @@ class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Co
volumeNames[i++] = cursor.getString(0) volumeNames[i++] = cursor.getString(0)
} }
cursor.close() cursor.close()
db.execSQL("ALTER TABLE $TABLE_NAME RENAME TO OLD;") if (volumeNames.isEmpty()) {
createTable(db) db.execSQL("DROP TABLE $TABLE_NAME;")
val uuidsValues = volumeNames.indices.joinToString(", ") { "('${VolumeData.newUuid()}', ?)" } createTable(db)
// add uuids to old data } else {
db.execSQL( db.execSQL("ALTER TABLE $TABLE_NAME RENAME TO OLD;")
"INSERT INTO $TABLE_NAME " + createTable(db)
"WITH uuids($COLUMN_UUID, $COLUMN_NAME) AS (VALUES $uuidsValues) " + val uuidsValues = volumeNames.indices.joinToString(", ") { "('${VolumeData.newUuid()}', ?)" }
"SELECT * FROM OLD NATURAL JOIN uuids;", // add uuids to old data
volumeNames db.execSQL(
) "INSERT INTO $TABLE_NAME " +
db.execSQL("DROP TABLE OLD;") "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