fix: node search by keeping denormalized name columns up to date and backfilling existing nodes (#3839)

This commit is contained in:
Mac DeCourcy 2025-11-28 09:38:30 -08:00 committed by GitHub
parent ef9c0dc844
commit 1c0dc486e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 44 additions and 1 deletions

View file

@ -30,6 +30,7 @@ import org.meshtastic.core.database.entity.MetadataEntity
import org.meshtastic.core.database.entity.MyNodeEntity
import org.meshtastic.core.database.entity.NodeEntity
import org.meshtastic.core.database.entity.NodeWithRelations
import org.meshtastic.proto.MeshProtos
@Suppress("TooManyFunctions")
@Dao
@ -49,6 +50,13 @@ interface NodeInfoDao {
// and to support lazy migration.
incomingNode.publicKey = incomingNode.user.publicKey
// Populate denormalized name columns from the User protobuf for search functionality
// Only populate if the user is not a placeholder (hwModel != UNSET)
if (incomingNode.user.hwModel != MeshProtos.HardwareModel.UNSET) {
incomingNode.longName = incomingNode.user.longName
incomingNode.shortName = incomingNode.user.shortName
}
val existingNodeEntity = getNodeByNum(incomingNode.num)?.node
return if (existingNodeEntity == null) {
@ -240,4 +248,27 @@ interface NodeInfoDao {
setMyNodeInfo(mi)
putAll(nodes.map { getVerifiedNodeForUpsert(it) })
}
/**
* Backfills longName and shortName columns from the user protobuf for nodes where these columns are NULL. This
* ensures search functionality works for all nodes. Skips placeholder/default users (hwModel == UNSET).
*/
@Transaction
fun backfillDenormalizedNames() {
val nodes = getAllNodesSnapshot()
val nodesToUpdate =
nodes
.filter { node ->
// Only backfill if columns are NULL AND the user is not a placeholder (hwModel != UNSET)
(node.longName == null || node.shortName == null) &&
node.user.hwModel != MeshProtos.HardwareModel.UNSET
}
.map { node -> node.copy(longName = node.user.longName, shortName = node.user.shortName) }
if (nodesToUpdate.isNotEmpty()) {
putAll(nodesToUpdate)
}
}
@Query("SELECT * FROM nodes")
fun getAllNodesSnapshot(): List<NodeEntity>
}