From a317877120a1b7b3ea2afcf5d048c3d77fd62fe2 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Tue, 18 Oct 2016 17:53:26 +1100 Subject: [PATCH] Be a little more concise about what to do when running migration for v64. Before it is not apparant that the migration introduced for v64 is associated with that particular version. This is because the guard condition used to bail out from the upgrade is more closely related to a previous migration. This is due to a flaw with the desigh of `resetTransient()`, whereby it always resets the database to the schema _of the current F-Droid version being run_, not of the tables as they stood at the time of the particular migration being introduced. This clarifies the guard condition for v64 by instead explicitly asking if the columns of interest exist yet in this particular invocation of `onUpgrade()`. --- .../java/org/fdroid/fdroid/data/DBHelper.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java index 0a2125425..0cd82c8b2 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java +++ b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java @@ -360,27 +360,33 @@ class DBHelper extends SQLiteOpenHelper { addObbFiles(db, oldVersion); } - /** - * For upgrades from earlier than 63, this is created in - * {@link #resetTransient(SQLiteDatabase)} in - * {@link #migrateToPackageTable(SQLiteDatabase, int)}, so it only needs - * to run when the database is at version 63. - */ private void addObbFiles(SQLiteDatabase db, int oldVersion) { - if (oldVersion != 63) { + if (oldVersion >= 64) { return; } - Utils.debugLog(TAG, "Adding " + ApkTable.Cols.OBB_MAIN_FILE - + ", " + ApkTable.Cols.OBB_PATCH_FILE - + ", and hash columns to " + ApkTable.NAME); - db.execSQL("alter table " + ApkTable.NAME + " add column " - + ApkTable.Cols.OBB_MAIN_FILE + " string"); - db.execSQL("alter table " + ApkTable.NAME + " add column " - + ApkTable.Cols.OBB_MAIN_FILE_SHA256 + " string"); - db.execSQL("alter table " + ApkTable.NAME + " add column " - + ApkTable.Cols.OBB_PATCH_FILE + " string"); - db.execSQL("alter table " + ApkTable.NAME + " add column " - + ApkTable.Cols.OBB_PATCH_FILE_SHA256 + " string"); + + Utils.debugLog(TAG, "Ensuring " + ApkTable.Cols.OBB_MAIN_FILE + ", " + + ApkTable.Cols.OBB_PATCH_FILE + ", and hash columns exist on " + ApkTable.NAME); + + if (!columnExists(db, ApkTable.NAME, ApkTable.Cols.OBB_MAIN_FILE)) { + db.execSQL("alter table " + ApkTable.NAME + " add column " + + ApkTable.Cols.OBB_MAIN_FILE + " string"); + } + + if (!columnExists(db, ApkTable.NAME, ApkTable.Cols.OBB_MAIN_FILE_SHA256)) { + db.execSQL("alter table " + ApkTable.NAME + " add column " + + ApkTable.Cols.OBB_MAIN_FILE_SHA256 + " string"); + } + + if (!columnExists(db, ApkTable.NAME, ApkTable.Cols.OBB_PATCH_FILE)) { + db.execSQL("alter table " + ApkTable.NAME + " add column " + + ApkTable.Cols.OBB_PATCH_FILE + " string"); + } + + if (!columnExists(db, ApkTable.NAME, ApkTable.Cols.OBB_PATCH_FILE_SHA256)) { + db.execSQL("alter table " + ApkTable.NAME + " add column " + + ApkTable.Cols.OBB_PATCH_FILE_SHA256 + " string"); + } } private void migrateToPackageTable(SQLiteDatabase db, int oldVersion) {