Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2,749 changes: 2,749 additions & 0 deletions app/schemas/com.theveloper.pixelplay.data.database.PixelPlayDatabase/43.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,159 @@ class MusicDaoTest {
return ArtistEntity(id = id, name = name, trackCount = 10, imageUrl = null)
}

// --- Artist de-duplication (case / whitespace variants) ---

@Test
@Throws(Exception::class)
fun mergeDuplicateArtistsCollapsesCaseVariantsAndKeepsSongs() = runTest {
// Three rows for one human, which is exactly what the old raw-name
// lookup in SyncWorker produced over successive scans.
musicDao.insertArtists(
listOf(
createArtistEntity(1L, "Beyoncé"),
createArtistEntity(2L, "beyonce"),
createArtistEntity(3L, " BEYONCE ")
)
)
musicDao.insertSongs(
listOf(
createSongEntity(11L, "Halo", "Beyoncé", "I Am", "/music/a/halo.mp3"),
createSongEntity(12L, "Irreplaceable", "beyonce", "I Am", "/music/a/irreplaceable.mp3")
)
)
musicDao.insertSongArtistCrossRefs(
listOf(
SongArtistCrossRef(11L, 1L, isPrimary = true),
SongArtistCrossRef(12L, 2L, isPrimary = true)
)
)

db.mergeDuplicateArtists()

val artists = db.musicDao().getAllArtistsListRaw()
assertEquals(1, artists.size)
// Lowest id survives.
assertEquals(1L, artists.first().id)

// Both songs survive and now point at the surviving artist.
val songs = musicDao.getSongsForArtistList(1L)
assertEquals(setOf(11L, 12L), songs.map { it.id }.toSet())
assertEquals(1L, songs.first().artistId)
}

@Test
@Throws(Exception::class)
fun mergeDuplicateArtistsPreservesArtworkFromTheDuplicate() = runTest {
// The survivor has no image but a duplicate does; the merge must not cost
// the user their artist artwork.
musicDao.insertArtists(
listOf(
createArtistEntity(1L, "Radiohead"),
ArtistEntity(id = 2L, name = "radiohead", trackCount = 4, imageUrl = "https://img/radiohead.jpg")
)
)

db.mergeDuplicateArtists()

val survivor = musicDao.getArtistsByIds(listOf(1L)).single()
assertEquals("https://img/radiohead.jpg", survivor.imageUrl)
}

@Test
@Throws(Exception::class)
fun mergeDuplicateArtistsIsIdempotent() = runTest {
musicDao.insertArtists(
listOf(
createArtistEntity(1L, "Aphex Twin"),
createArtistEntity(2L, "aphex twin")
)
)
musicDao.insertSongs(
listOf(createSongEntity(21L, "Xtal", "Aphex Twin", "SAW", "/music/a/xtal.mp3"))
)
musicDao.insertSongArtistCrossRefs(listOf(SongArtistCrossRef(21L, 2L, isPrimary = true)))

db.mergeDuplicateArtists()
val afterFirst = musicDao.getAllArtistsListRaw()
val refsAfterFirst = musicDao.getCrossRefsForSong(21L)

db.mergeDuplicateArtists()
val afterSecond = musicDao.getAllArtistsListRaw()
val refsAfterSecond = musicDao.getCrossRefsForSong(21L)

assertEquals(afterFirst.map { it.id }, afterSecond.map { it.id })
assertEquals(refsAfterFirst, refsAfterSecond)
assertEquals(1, afterSecond.size)
}

@Test
@Throws(Exception::class)
fun mergeDuplicateArtistsLeavesDistinctNamesAlone() = runTest {
// "The Beatles" and "Beatles" are different artists and must not merge.
musicDao.insertArtists(
listOf(
createArtistEntity(1L, "The Beatles"),
createArtistEntity(2L, "Beatles")
)
)

db.mergeDuplicateArtists()

assertEquals(2, musicDao.getAllArtistsListRaw().size)
}

@Test
@Throws(Exception::class)
fun mergeDuplicateArtistsDropsRedundantCrossRefWhenSongIsCreditedToBothDuplicates() = runTest {
// (song_id, artist_id) is a composite primary key, so re-pointing the
// loser's ref must not create a duplicate for the survivor.
musicDao.insertArtists(
listOf(
createArtistEntity(1L, "Portishead"),
createArtistEntity(2L, "PORTISHEAD")
)
)
musicDao.insertSongs(
listOf(createSongEntity(31L, "Roads", "Portishead", "Dummy", "/music/a/roads.mp3"))
)
musicDao.insertSongArtistCrossRefs(
listOf(
SongArtistCrossRef(31L, 1L, isPrimary = true),
SongArtistCrossRef(31L, 2L, isPrimary = false)
)
)

db.mergeDuplicateArtists()

val refs = musicDao.getCrossRefsForSong(31L)
assertEquals(1, refs.size)
assertEquals(1L, refs.first().artistId)
}

@Test
@Throws(Exception::class)
fun recomputeAllArtistTrackCountsReflectsCrossRefs() = runTest {
musicDao.insertArtists(listOf(createArtistEntity(1L, "Massive Attack")))
musicDao.insertSongs(
listOf(
createSongEntity(41L, "Teardrop", "Massive Attack", "Mezzanine", "/music/a/teardrop.mp3"),
createSongEntity(42L, "Angel", "Massive Attack", "Mezzanine", "/music/a/angel.mp3")
)
)
musicDao.insertSongArtistCrossRefs(
listOf(
SongArtistCrossRef(41L, 1L, isPrimary = true),
SongArtistCrossRef(42L, 1L, isPrimary = true)
)
)
// The helper seeds a deliberately wrong count.
musicDao.insertArtists(listOf(createArtistEntity(1L, "Massive Attack").copy(trackCount = 99)))

musicDao.recomputeAllArtistTrackCounts()

assertEquals(2, musicDao.getArtistsByIds(listOf(1L)).single().trackCount)
}

@Test
@Throws(Exception::class)
fun insertAndGetSongs() = runTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AiPlaylistGenerator @Inject constructor(
val genre = song.genre?.replace("\"", "'")?.take(15) ?: "?"
if (index > 0) append(",\n")
if (useExtendedFields) {
val album = song.album?.replace("\"", "'")?.take(25) ?: "?"
val album = song.album.replace("\"", "'").take(25)
val dur = song.duration
val fav = if (song.isFavorite) "1" else "0"
append("""{"id":"${song.id}","t":"$title","a":"$artist","g":"$genre","al":"$album","d":$dur,"f":$fav,"s":$score}""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,30 @@ interface MusicDao {
@Query("DELETE FROM artists WHERE NOT EXISTS (SELECT 1 FROM song_artist_cross_ref WHERE song_artist_cross_ref.artist_id = artists.id)")
suspend fun deleteOrphanedArtists()

/**
* Recomputes the denormalised `artists.track_count` from the cross-reference
* table for every artist.
*
* `track_count` is a cache of `COUNT(DISTINCT song_id)` per artist. Sync
* recomputes it only for artists touched by the current batch, so artists
* outside that batch keep whatever value they had — which goes stale after
* songs are deleted or after [PixelPlayDatabase.mergeDuplicateArtists]
* collapses duplicate rows. The artist list queries compute the count live,
* so this column is not load-bearing; it is kept correct so diagnostics and
* any future query that reads it directly are not misled.
*/
@Query(
"""
UPDATE artists
SET track_count = (
SELECT COUNT(DISTINCT song_artist_cross_ref.song_id)
FROM song_artist_cross_ref
WHERE song_artist_cross_ref.artist_id = artists.id
)
"""
)
suspend fun recomputeAllArtistTrackCounts()

// --- Favorite Operations ---
@Query("UPDATE songs SET is_favorite = :isFavorite WHERE id = :songId")
suspend fun setFavoriteStatus(songId: Long, isFavorite: Boolean)
Expand Down
Loading