From 571a79cc1d3db36d2e41b90c4c6548e59de45220 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 19 Sep 2023 11:41:01 +0200 Subject: [PATCH] Really fix database upgrade --- app/build.gradle | 4 +-- .../sushi/hardcore/droidfs/VolumeDatabase.kt | 29 +++++++++++-------- .../metadata/android/en-US/changelogs/35.txt | 1 + 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 fastlane/metadata/android/en-US/changelogs/35.txt diff --git a/app/build.gradle b/app/build.gradle index cdfb006..51ac9b2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" diff --git a/app/src/main/java/sushi/hardcore/droidfs/VolumeDatabase.kt b/app/src/main/java/sushi/hardcore/droidfs/VolumeDatabase.kt index 087c2b8..99843ac 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/VolumeDatabase.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/VolumeDatabase.kt @@ -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;") + } } } diff --git a/fastlane/metadata/android/en-US/changelogs/35.txt b/fastlane/metadata/android/en-US/changelogs/35.txt new file mode 100644 index 0000000..31b018c --- /dev/null +++ b/fastlane/metadata/android/en-US/changelogs/35.txt @@ -0,0 +1 @@ +- Really fix database upgrade crash