Skip to content

Commit

Permalink
Prevent adding duplicated extensions to the db table (#808)
Browse files Browse the repository at this point in the history
* Prevent adding duplicated extensions to the db table

There is a possibility that multiple extension repos have been added which contain the same extensions.
In case these extensions have not been added to the db table yet, they would all get added to the db, which would create duplicated extensions

* Use the extension with the latest version from all repos

In case multiple repos have the same extension, use the extension with the latest version
  • Loading branch information
schroda authored Jan 10, 2024
1 parent 3a1e0c5 commit c852592
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,21 @@ object ExtensionsList {

private fun updateExtensionDatabase(foundExtensions: List<OnlineExtension>) {
transaction {
val uniqueExtensions =
foundExtensions.groupBy { it.pkgName }.mapValues {
(_, extension) ->
extension.maxBy { it.versionCode }
}.values
val installedExtensions =
ExtensionTable.selectAll().toList()
.associateBy { it[ExtensionTable.pkgName] }
val extensionsToUpdate = mutableListOf<Pair<OnlineExtension, ResultRow>>()
val extensionsToInsert = mutableListOf<OnlineExtension>()
val extensionsToDelete =
installedExtensions.filter { it.value[ExtensionTable.repo] != null }.mapNotNull { (pkgName, extension) ->
extension.takeUnless { foundExtensions.any { it.pkgName == pkgName } }
extension.takeUnless { uniqueExtensions.any { it.pkgName == pkgName } }
}
foundExtensions.forEach {
uniqueExtensions.forEach {
val extension = installedExtensions[it.pkgName]
if (extension != null) {
extensionsToUpdate.add(it to extension)
Expand Down

0 comments on commit c852592

Please sign in to comment.