feat: Implement repository removal during database upgrades

This commit introduces a mechanism to remove specific repositories from the user's database during an `onUpgrade` event.

Key changes:
- Added a `toRemove` list in `Repository.kt` to specify addresses of repositories to be removed.
- Implemented `SQLiteDatabase.removeRepositories()` in `DatabaseHelper.kt`. This function:
    - Queries the database for existing repositories.
    - Compares them against the `toRemove` list.
    - Marks matching repositories as deleted in the database by setting the `ROW_DELETED` flag to 1.
- The `onUpgrade` method in `DatabaseHelper.kt` now calls `db.removeRepositories()` before adding newly added repositories.
This commit is contained in:
LooKeR 2025-06-23 20:16:40 +05:30
parent 6a00b6bc0c
commit 4223cea6d7
No known key found for this signature in database
GPG Key ID: 6B59369FDB608FB9
2 changed files with 55 additions and 1 deletions

View File

@ -36,7 +36,7 @@ class DatabaseHelper(context: Context) :
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
// Add newly added repositories when database version is upgraded
db.removeRepositories()
db.addNewlyAddedRepositories()
this.updated = true
}
@ -104,6 +104,55 @@ class DatabaseHelper(context: Context) :
}
}
private fun SQLiteDatabase.removeRepositories() {
// Remove repositories that are in the toRemove list
val reposToRemove = Repository.toRemove
if (reposToRemove.isEmpty()) return
// Get all repositories with their IDs and addresses
val existingRepos = query(
Schema.Repository.name,
columns = arrayOf(Schema.Repository.ROW_ID, Schema.Repository.ROW_DATA),
selection = null,
signal = null,
).use { cursor ->
cursor.asSequence().mapNotNull {
val idIndex = it.getColumnIndexOrThrow(Schema.Repository.ROW_ID)
val dataIndex = it.getColumnIndexOrThrow(Schema.Repository.ROW_DATA)
val id = it.getLong(idIndex)
val data = it.getBlob(dataIndex)
try {
val repo = data.jsonParse { json -> json.repository() }
id to repo.address
} catch (_: Exception) {
null
}
}.toMap()
}
// Find repositories to remove
val reposToRemoveIds = existingRepos.filter { (_, address) ->
address in reposToRemove
}.keys
if (reposToRemoveIds.isNotEmpty()) {
transaction {
reposToRemoveIds.forEach { repoId ->
// Directly update the database to mark repository as deleted
update(
Schema.Repository.name,
android.content.ContentValues().apply {
put(Schema.Repository.ROW_DELETED, 1)
},
"${Schema.Repository.ROW_ID} = ?",
arrayOf(repoId.toString())
)
}
}
}
}
private fun handleTables(db: SQLiteDatabase, vararg tables: Table): Boolean {
val shouldRecreate = tables.any { table ->
val sql = db.query(

View File

@ -413,5 +413,10 @@ data class Repository(
fingerprint = "49F37E74DEE483DCA2B991334FB5A0200787430D0B5F9A783DD5F13695E9517B"
)
)
val toRemove: List<String> = listOf(
// Add repository addresses that should be removed during database upgrades and remove them from the lists above
// Example: "https://example.com/fdroid/repo"
)
}
}