fix: clear local DB after reset (#1746)

This commit is contained in:
James Rich 2025-04-21 13:52:41 -05:00 committed by GitHub
parent a28b03fde7
commit 5d5a4938a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 77 additions and 30 deletions

View file

@ -103,7 +103,7 @@ class NodeRepository @Inject constructor(
).mapLatest { list -> list.map { it.toModel() } }.flowOn(dispatchers.io).conflate()
suspend fun upsert(node: NodeEntity) = withContext(dispatchers.io) {
nodeInfoDao.upsertCheckKeyMatch(node)
nodeInfoDao.upsert(node)
}
suspend fun installNodeDB(mi: MyNodeEntity, nodes: List<NodeEntity>) = withContext(dispatchers.io) {
@ -116,6 +116,10 @@ class NodeRepository @Inject constructor(
nodeInfoDao.putAll(nodes)
}
suspend fun clearNodeDB() = withContext(dispatchers.io) {
nodeInfoDao.clearNodeInfo()
}
suspend fun deleteNode(num: Int) = withContext(dispatchers.io) {
nodeInfoDao.deleteNode(num)
nodeInfoDao.deleteMetadata(num)

View file

@ -17,7 +17,6 @@
package com.geeksville.mesh.database.dao
import android.util.Log
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.MapColumn
@ -25,13 +24,14 @@ import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Upsert
import com.geeksville.mesh.android.BuildUtils.warn
import com.geeksville.mesh.copy
import com.geeksville.mesh.database.entity.MetadataEntity
import com.geeksville.mesh.database.entity.MyNodeEntity
import com.geeksville.mesh.database.entity.NodeEntity
import com.geeksville.mesh.database.entity.NodeWithRelations
import kotlinx.coroutines.flow.Flow
private const val TAG = "NodeInfoDao"
@Suppress("TooManyFunctions")
@Dao
interface NodeInfoDao {
@ -108,21 +108,39 @@ interface NodeInfoDao {
): Flow<List<NodeWithRelations>>
@Upsert
fun upsert(node: NodeEntity)
fun upsertCheckKeyMatch(node: NodeEntity) {
val existingNode = getNodeByNum(node.num)
if (existingNode != null && existingNode.user.publicKey != node.user.publicKey) {
Log.w(TAG, "Node ${node.num} has changed its public key")
val user =
node.user.toBuilder().setPublicKey(NodeEntity.ERROR_BYTE_STRING).build()
node.user = user
fun upsert(node: NodeEntity) {
val found = getNodeByNum(node.num)?.node
found?.let {
val keyMatch = !it.hasPKC || it.user.publicKey == node.user.publicKey
it.user = if (keyMatch) {
node.user
} else {
node.user.copy {
warn("Public key mismatch from $longName ($shortName)")
publicKey = NodeEntity.ERROR_BYTE_STRING
}
}
}
upsert(node)
doUpsert(node)
}
@Transaction
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun putAll(nodes: List<NodeEntity>) {
nodes.forEach { upsertCheckKeyMatch(it) }
nodes.forEach { node ->
val found = getNodeByNum(node.num)?.node
found?.let {
val keyMatch = !it.hasPKC || it.user.publicKey == node.user.publicKey
it.user = if (keyMatch) {
node.user
} else {
node.user.copy {
warn("Public key mismatch from $longName ($shortName)")
publicKey = NodeEntity.ERROR_BYTE_STRING
}
}
}
}
doPutAll(nodes)
}
@Query("DELETE FROM nodes")
@ -137,6 +155,13 @@ interface NodeInfoDao {
@Query("DELETE FROM metadata WHERE num=:num")
fun deleteMetadata(num: Int)
@Query("SELECT * FROM nodes WHERE num = :num")
fun getNodeByNum(num: Int): NodeEntity?
@Query("SELECT * FROM nodes WHERE num=:num")
@Transaction
fun getNodeByNum(num: Int): NodeWithRelations?
@Upsert
fun doUpsert(node: NodeEntity)
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun doPutAll(nodes: List<NodeEntity>)
}