[db] allow setting a preferred repo per app

This commit is contained in:
Torsten Grote
2023-10-16 18:16:48 -03:00
parent f4a1d92933
commit 704234c9df
11 changed files with 1327 additions and 59 deletions

View File

@@ -305,7 +305,9 @@ internal interface AppDaoInt : AppDao {
@Transaction
@Query("""SELECT ${AppMetadata.TABLE}.* FROM ${AppMetadata.TABLE}
JOIN RepositoryPreferences AS pref USING (repoId)
WHERE packageName = :packageName AND pref.enabled = 1
LEFT JOIN AppPrefs USING (packageName)
WHERE packageName = :packageName AND pref.enabled = 1 AND
COALESCE(preferredRepoId, repoId) = repoId
ORDER BY pref.weight DESC LIMIT 1""")
override fun getApp(packageName: String): LiveData<App?>
@@ -341,7 +343,8 @@ internal interface AppDaoInt : AppDao {
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
LEFT JOIN ${HighestVersion.TABLE} AS version USING (repoId, packageName)
LEFT JOIN ${LocalizedIcon.TABLE} AS icon USING (repoId, packageName)
WHERE pref.enabled = 1
LEFT JOIN AppPrefs USING (packageName)
WHERE pref.enabled = 1 AND COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)
ORDER BY localizedName IS NULL ASC, icon.packageName IS NULL ASC,
localizedSummary IS NULL ASC, app.lastUpdated DESC
@@ -355,7 +358,9 @@ internal interface AppDaoInt : AppDao {
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
LEFT JOIN ${HighestVersion.TABLE} AS version USING (repoId, packageName)
LEFT JOIN ${LocalizedIcon.TABLE} AS icon USING (repoId, packageName)
WHERE pref.enabled = 1 AND categories LIKE '%,' || :category || ',%'
LEFT JOIN AppPrefs USING (packageName)
WHERE pref.enabled = 1 AND categories LIKE '%,' || :category || ',%' AND
COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)
ORDER BY localizedName IS NULL ASC, icon.packageName IS NULL ASC,
localizedSummary IS NULL ASC, app.lastUpdated DESC
@@ -440,8 +445,10 @@ internal interface AppDaoInt : AppDao {
FROM ${AppMetadata.TABLE} AS app
JOIN ${AppMetadataFts.TABLE} USING (repoId, packageName)
LEFT JOIN ${HighestVersion.TABLE} AS version USING (repoId, packageName)
LEFT JOIN AppPrefs USING (packageName)
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
WHERE pref.enabled = 1 AND ${AppMetadataFts.TABLE} MATCH :searchQuery
WHERE pref.enabled = 1 AND ${AppMetadataFts.TABLE} MATCH :searchQuery AND
COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)""")
fun getAppListItems(searchQuery: String): LiveData<List<AppListItem>>
@@ -455,9 +462,11 @@ internal interface AppDaoInt : AppDao {
FROM ${AppMetadata.TABLE} AS app
JOIN ${AppMetadataFts.TABLE} USING (repoId, packageName)
LEFT JOIN ${HighestVersion.TABLE} AS version USING (repoId, packageName)
LEFT JOIN AppPrefs USING (packageName)
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
WHERE pref.enabled = 1 AND categories LIKE '%,' || :category || ',%' AND
${AppMetadataFts.TABLE} MATCH :searchQuery
${AppMetadataFts.TABLE} MATCH :searchQuery AND
COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)""")
fun getAppListItems(category: String, searchQuery: String): LiveData<List<AppListItem>>
@@ -485,8 +494,9 @@ internal interface AppDaoInt : AppDao {
version.antiFeatures, app.isCompatible, app.preferredSigner
FROM ${AppMetadata.TABLE} AS app
LEFT JOIN ${HighestVersion.TABLE} AS version USING (repoId, packageName)
LEFT JOIN AppPrefs USING (packageName)
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
WHERE pref.enabled = 1
WHERE pref.enabled = 1 AND COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)
ORDER BY localizedName COLLATE NOCASE ASC""")
fun getAppListItemsByName(): LiveData<List<AppListItem>>
@@ -498,7 +508,8 @@ internal interface AppDaoInt : AppDao {
FROM ${AppMetadata.TABLE} AS app
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
LEFT JOIN ${HighestVersion.TABLE} AS version USING (repoId, packageName)
WHERE pref.enabled = 1
LEFT JOIN AppPrefs USING (packageName)
WHERE pref.enabled = 1 AND COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)
ORDER BY app.lastUpdated DESC""")
fun getAppListItemsByLastUpdated(): LiveData<List<AppListItem>>
@@ -510,7 +521,9 @@ internal interface AppDaoInt : AppDao {
FROM ${AppMetadata.TABLE} AS app
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
LEFT JOIN ${HighestVersion.TABLE} AS version USING (repoId, packageName)
WHERE pref.enabled = 1 AND categories LIKE '%,' || :category || ',%'
LEFT JOIN AppPrefs USING (packageName)
WHERE pref.enabled = 1 AND categories LIKE '%,' || :category || ',%' AND
COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)
ORDER BY app.lastUpdated DESC""")
fun getAppListItemsByLastUpdated(category: String): LiveData<List<AppListItem>>
@@ -522,7 +535,9 @@ internal interface AppDaoInt : AppDao {
FROM ${AppMetadata.TABLE} AS app
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
LEFT JOIN ${HighestVersion.TABLE} AS version USING (repoId, packageName)
WHERE pref.enabled = 1 AND categories LIKE '%,' || :category || ',%'
LEFT JOIN AppPrefs USING (packageName)
WHERE pref.enabled = 1 AND categories LIKE '%,' || :category || ',%' AND
COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)
ORDER BY localizedName COLLATE NOCASE ASC""")
fun getAppListItemsByName(category: String): LiveData<List<AppListItem>>
@@ -556,7 +571,9 @@ internal interface AppDaoInt : AppDao {
app.isCompatible, app.preferredSigner
FROM ${AppMetadata.TABLE} AS app
JOIN ${RepositoryPreferences.TABLE} AS pref USING (repoId)
WHERE pref.enabled = 1 AND packageName IN (:packageNames)
LEFT JOIN AppPrefs USING (packageName)
WHERE pref.enabled = 1 AND packageName IN (:packageNames) AND
COALESCE(preferredRepoId, repoId) = repoId
GROUP BY packageName HAVING MAX(pref.weight)
ORDER BY localizedName COLLATE NOCASE ASC""")
fun getAppListItems(packageNames: List<String>): LiveData<List<AppListItem>>

View File

@@ -13,6 +13,7 @@ public data class AppPrefs(
@PrimaryKey
val packageName: String,
override val ignoreVersionCodeUpdate: Long = 0,
val preferredRepoId: Long? = null,
// This is named like this, because it hit a Room bug when joining with Version table
// which had exactly the same field.
internal val appPrefReleaseChannels: List<String>? = null,

View File

@@ -3,6 +3,7 @@ package org.fdroid.database
import android.content.res.Resources
import androidx.core.os.ConfigurationCompat.getLocales
import androidx.core.os.LocaleListCompat
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
@@ -14,7 +15,7 @@ import java.util.concurrent.Callable
// When bumping this version, please make sure to add one (or more) migration(s) below!
// Consider also providing tests for that migration.
// Don't forget to commit the new schema to the git repo as well.
version = 1,
version = 2,
entities = [
// repo
CoreRepository::class,
@@ -41,6 +42,7 @@ import java.util.concurrent.Callable
exportSchema = true,
autoMigrations = [
// add future migrations here (if they are easy enough to be done automatically)
AutoMigration(1, 2),
],
)
@TypeConverters(Converters::class)

View File

@@ -35,6 +35,12 @@ public interface VersionDao {
* Returns a list of versions for the given [packageName] sorting by highest version code first.
*/
public fun getAppVersions(packageName: String): LiveData<List<AppVersion>>
/**
* Returns a list of versions from the repo identified by the given [repoId]
* for the given [packageName] sorting by highest version code first.
*/
public fun getAppVersions(repoId: Long, packageName: String): LiveData<List<AppVersion>>
}
/**
@@ -161,13 +167,12 @@ internal interface VersionDaoInt : VersionDao {
ORDER BY manifest_versionCode DESC, pref.weight DESC""")
override fun getAppVersions(packageName: String): LiveData<List<AppVersion>>
/**
* Only use for testing, not sorted, does take disabled repos into account.
*/
@Transaction
@RewriteQueriesToDropUnusedColumns
@Query("""SELECT * FROM ${Version.TABLE}
WHERE repoId = :repoId AND packageName = :packageName""")
fun getAppVersions(repoId: Long, packageName: String): List<AppVersion>
WHERE repoId = :repoId AND packageName = :packageName
ORDER BY manifest_versionCode DESC""")
override fun getAppVersions(repoId: Long, packageName: String): LiveData<List<AppVersion>>
@Query("""SELECT * FROM ${Version.TABLE}
WHERE repoId = :repoId AND packageName = :packageName AND versionId = :versionId""")