From 0d4d160407f623acfa39ededea8bf68a842ab23a Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Tue, 11 Oct 2016 00:04:34 +1100 Subject: [PATCH] Fix migration for DB version 50. The migration resulted in a query being run which was broken. The query was broken because it was dynamically generated by Java code. This Java code resulted in a valid migration when until very recently when the query was refactored to deal with a new DB structure. Now the query is no longer suitable to be run against a DB_VERSION 49 database. To resolve this, the migration now hard codes the query to a string which is executable when the DB_VERSION is 49. --- .../org/fdroid/fdroid/data/AppProvider.java | 34 ++++--------------- .../java/org/fdroid/fdroid/data/DBHelper.java | 27 ++++++++++++++- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/data/AppProvider.java b/app/src/main/java/org/fdroid/fdroid/data/AppProvider.java index ff9cafa66..8866d1af0 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/AppProvider.java +++ b/app/src/main/java/org/fdroid/fdroid/data/AppProvider.java @@ -5,7 +5,6 @@ import android.content.ContentValues; import android.content.Context; import android.content.UriMatcher; import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.text.TextUtils; import android.util.Log; @@ -191,22 +190,6 @@ public class AppProvider extends FDroidProvider { } } - /** - * Class that only exists to call private methods in the {@link AppProvider} without having - * to go via a Context/ContentResolver. The reason is that if the {@link DBHelper} class - * was to try and use its getContext().getContentResolver() in order to access the app - * provider, then the AppProvider will end up creating a new instance of a writeable - * SQLiteDatabase. This causes problems because the {@link DBHelper} still has its reference - * open and locks certain tables. - */ - static final class UpgradeHelper { - - public static void updateIconUrls(Context context, SQLiteDatabase db) { - AppProvider.updateIconUrls(context, db, AppMetadataTable.NAME, ApkTable.NAME); - } - - } - /** * A QuerySelection which is aware of the option/need to join onto the * installed apps table. Not that the base classes @@ -948,7 +931,7 @@ public class AppProvider extends FDroidProvider { updateCompatibleFlags(); updateSuggestedFromUpstream(); updateSuggestedFromLatest(); - updateIconUrls(getContext(), db(), getTableName(), getApkTableName()); + updateIconUrls(); } private void updatePreferredMetadata() { @@ -1047,14 +1030,11 @@ public class AppProvider extends FDroidProvider { db().execSQL(updateSql); } - /** - * Made static so that the {@link org.fdroid.fdroid.data.AppProvider.UpgradeHelper} can access - * it without instantiating an {@link AppProvider}. This is also the reason it needs to accept - * the context and database as arguments. - */ - private static void updateIconUrls(Context context, SQLiteDatabase db, String appTable, String apkTable) { - final String iconsDir = Utils.getIconsDir(context, 1.0); - final String iconsDirLarge = Utils.getIconsDir(context, 1.5); + private void updateIconUrls() { + final String appTable = getTableName(); + final String apkTable = getApkTableName(); + final String iconsDir = Utils.getIconsDir(getContext(), 1.0); + final String iconsDirLarge = Utils.getIconsDir(getContext(), 1.5); String repoVersion = Integer.toString(Repo.VERSION_DENSITY_SPECIFIC_ICONS); Utils.debugLog(TAG, "Updating icon paths for apps belonging to repos with version >= " + repoVersion); Utils.debugLog(TAG, "Using icons dir '" + iconsDir + "'"); @@ -1064,7 +1044,7 @@ public class AppProvider extends FDroidProvider { repoVersion, iconsDir, Utils.FALLBACK_ICONS_DIR, repoVersion, iconsDirLarge, Utils.FALLBACK_ICONS_DIR, }; - db.execSQL(query, params); + db().execSQL(query, params); } /** 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 5cfe6cd4a..831d12d54 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java +++ b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java @@ -765,7 +765,32 @@ class DBHelper extends SQLiteOpenHelper { return; } Utils.debugLog(TAG, "Recalculating app icon URLs so that the newly added large icons will get updated."); - AppProvider.UpgradeHelper.updateIconUrls(context, db); + + String query = "UPDATE fdroid_app " + + "SET iconUrl = (" + + " SELECT (fdroid_repo.address || CASE WHEN fdroid_repo.version >= ? THEN ? ELSE ? END || fdroid_app.icon) " + + " FROM fdroid_apk " + + " JOIN fdroid_repo ON (fdroid_repo._id = fdroid_apk.repo) " + + " WHERE fdroid_app.id = fdroid_apk.id AND fdroid_apk.vercode = fdroid_app.suggestedVercode " + + "), iconUrlLarge = (" + + " SELECT (fdroid_repo.address || CASE WHEN fdroid_repo.version >= ? THEN ? ELSE ? END || fdroid_app.icon) " + + " FROM fdroid_apk " + + " JOIN fdroid_repo ON (fdroid_repo._id = fdroid_apk.repo) " + + " WHERE fdroid_app.id = fdroid_apk.id AND fdroid_apk.vercode = fdroid_app.suggestedVercode" + + ")"; + + String iconsDir = Utils.getIconsDir(context, 1.0); + String iconsDirLarge = Utils.getIconsDir(context, 1.5); + String repoVersion = Integer.toString(Repo.VERSION_DENSITY_SPECIFIC_ICONS); + Utils.debugLog(TAG, "Using icons dir '" + iconsDir + "'"); + Utils.debugLog(TAG, "Using large icons dir '" + iconsDirLarge + "'"); + String[] args = { + repoVersion, iconsDir, Utils.FALLBACK_ICONS_DIR, + repoVersion, iconsDirLarge, Utils.FALLBACK_ICONS_DIR, + }; + + db.rawQuery(query, args); + clearRepoEtags(db); }