library: fix sort-name resolution and migration rebuild

Collect every credited artist id when resolving album and song sort keys so
the singular id and plural refs can both be looked up, and add regression
tests for the compilation and stale-sort-name cases.

Drop the FTS tables before the backfill so the old triggers do not index rows
that are about to be discarded, and rely on createSearchIndex's rebuild
instead of rebuilding a second time. Treat a whitespace-only artist sortName
as absent, and use the sort keys in the downloads tree too.
This commit is contained in:
austinried
2026-10-18 05:26:59 +00:00
parent 7d8a2dae9f
commit 0c2e85134b
8 changed files with 118 additions and 29 deletions
@@ -166,7 +166,10 @@ interface DownloadDao {
@Query(
"SELECT d.sourceId AS sourceId, d.songId AS songId, s.title AS title, s.albumId AS albumId, al.name AS albumName, " +
"al.artistId AS artistId, COALESCE(ar.name, al.albumArtist) AS artistName, d.status AS status, " +
"d.bytes AS bytes, d.total AS total " +
"d.bytes AS bytes, d.total AS total, " +
"COALESCE(NULLIF(ar.sortName, ''), NULLIF(al.albumArtistSort, ''), ar.name, al.albumArtist, '') AS artistSort, " +
"COALESCE(NULLIF(al.sortName, ''), al.name, '') AS albumSort, " +
"COALESCE(NULLIF(s.sortName, ''), s.title, '') AS songSort " +
"FROM song_downloads d " +
"JOIN songs s ON s.sourceId = d.sourceId AND s.id = d.songId " +
"LEFT JOIN albums al ON al.sourceId = s.sourceId AND al.id = s.albumId " +
@@ -461,6 +461,9 @@ val MIGRATION_30_31 =
connection.execSQL("ALTER TABLE `albums` ADD COLUMN `albumArtistSort` TEXT NOT NULL DEFAULT '' COLLATE NOCASE")
connection.execSQL("ALTER TABLE `songs` ADD COLUMN `sortName` TEXT NOT NULL DEFAULT '' COLLATE NOCASE")
connection.execSQL("ALTER TABLE `songs` ADD COLUMN `artistSort` TEXT NOT NULL DEFAULT '' COLLATE NOCASE")
dropSearchIndex(connection, "artist_search")
dropSearchIndex(connection, "album_search")
dropSearchIndex(connection, "song_search")
connection.execSQL("UPDATE `artists` SET `sortName` = `name`")
connection.execSQL("UPDATE `albums` SET `sortName` = `name`")
connection.execSQL("UPDATE `songs` SET `sortName` = `title`")
@@ -488,15 +491,9 @@ val MIGRATION_30_31 =
ALBUM_INDEXES.forEach { connection.execSQL("DROP INDEX IF EXISTS `$it`") }
ARTIST_INDEX_SQL.forEach { connection.execSQL(it) }
ALBUM_INDEX_SQL.forEach { connection.execSQL(it) }
dropSearchIndex(connection, "artist_search")
dropSearchIndex(connection, "album_search")
dropSearchIndex(connection, "song_search")
createSearchIndex(connection, "artist_search", "artists", listOf("name", "sortName"))
createSearchIndex(connection, "album_search", "albums", listOf("name", "albumArtist", "sortName", "albumArtistSort"))
createSearchIndex(connection, "song_search", "songs", listOf("title", "artist", "sortName", "artistSort"))
rebuildSearchIndex(connection, "artist_search")
rebuildSearchIndex(connection, "album_search")
rebuildSearchIndex(connection, "song_search")
}
}
@@ -554,13 +551,6 @@ private suspend fun dropSearchIndex(
connection.execSQL("DROP TABLE IF EXISTS `$table`")
}
private suspend fun rebuildSearchIndex(
connection: SQLiteConnection,
table: String,
) {
connection.execSQL("INSERT INTO `$table`(`$table`) VALUES('rebuild')")
}
// Mirrors the FTS5 table and content-sync triggers Room generates for the @Fts5 entities.
private suspend fun createSearchIndex(
connection: SQLiteConnection,
@@ -94,6 +94,9 @@ data class DownloadedSong(
val size: Long = 0,
val bytes: Long = 0,
val total: Long = 0,
val artistSort: String = artistName.orEmpty(),
val albumSort: String = albumName.orEmpty(),
val songSort: String = title,
) {
val progress: Float?
get() = total.takeIf { it > 0 }?.let { (bytes.toFloat() / it).coerceIn(0f, 1f) }
@@ -32,7 +32,7 @@ object SubsonicXml {
albumCount = attrs.longAttr("albumCount") ?: 0L,
starred = attrs.dateAttr("starred"),
coverArt = attrs.attr("coverArt").ifEmpty { null },
sortName = attrs.attr("sortName").ifEmpty { attrs.attr("name") },
sortName = attrs.attr("sortName").takeIf { it.isNotBlank() } ?: attrs.attr("name"),
)
internal fun albumDraft(
@@ -190,7 +190,10 @@ class SyncService(
val sortNames =
artistSortNames(
batch.first().sourceId,
batch.mapNotNull { it.artistRefs.firstOrNull()?.id }.toSet(),
batch
.flatMap { listOfNotNull(it.artistId) + it.artistRefs.map { ref -> ref.id } }
.filter { it.isNotBlank() }
.toSet(),
)
return batch.map { song ->
val primary = song.artistId ?: song.artists.firstOrNull()?.id
@@ -207,7 +210,10 @@ class SyncService(
val sortNames =
artistSortNames(
batch.first().sourceId,
batch.mapNotNull { it.artistId ?: it.artists.firstOrNull()?.id }.toSet(),
batch
.flatMap { listOfNotNull(it.artistId) + it.artistRefs.map { ref -> ref.id } }
.filter { it.isNotBlank() }
.toSet(),
)
return batch.map { album ->
val primary = album.artistId ?: album.artists.firstOrNull()?.id
@@ -31,6 +31,7 @@ data class DownloadArtistNode(
val name: String,
val bytes: Long,
val albums: List<DownloadAlbumNode>,
val sortName: String = name,
)
data class DownloadAlbumNode(
@@ -38,6 +39,7 @@ data class DownloadAlbumNode(
val name: String,
val bytes: Long,
val songs: List<DownloadedSong>,
val sortName: String = name,
)
@OptIn(ExperimentalCoroutinesApi::class)
@@ -105,6 +107,7 @@ internal fun buildTree(
id = artistKey.first,
name = artistKey.second.ifBlank { unknownArtist },
bytes = artistSongs.sumOf { it.size },
sortName = artistSongs.first().artistSort.ifBlank { artistKey.second },
albums =
artistSongs
.groupBy { it.albumId.orEmpty() to it.albumName.orEmpty() }
@@ -113,9 +116,10 @@ internal fun buildTree(
id = albumKey.first,
name = albumKey.second.ifBlank { unknownAlbum },
bytes = albumSongs.sumOf { it.size },
songs = albumSongs.sortedBy { it.title.lowercase() },
sortName = albumSongs.first().albumSort.ifBlank { albumKey.second },
songs = albumSongs.sortedBy { it.songSort.lowercase() },
)
}.sortedBy { it.name.lowercase() },
}.sortedBy { it.sortName.lowercase() },
)
}.sortedBy { it.name.lowercase() },
}.sortedBy { it.sortName.lowercase() },
)