Commit f0e8254f authored by Matthieu Gallien's avatar Matthieu Gallien 🎵
Browse files

when inserting tracks again with a database file, ensure they are updated

fix case of rating not being updated when elisa is started again
parent 97c876e2
......@@ -227,6 +227,85 @@ private Q_SLOTS:
}
}
void restoreModifiedTracksWidthDatabaseFile()
{
QTemporaryFile myTempDatabase;
myTempDatabase.open();
{
DatabaseInterface musicDb;
musicDb.init(QStringLiteral("testDb1"), myTempDatabase.fileName());
auto allNewTracks = mNewTracks;
allNewTracks.push_back({true, QStringLiteral("$19"), QStringLiteral("0"), QStringLiteral("track6"),
QStringLiteral("artist2"), QStringLiteral("album3"), QStringLiteral("artist2"), 6, 1, QTime::fromMSecsSinceStartOfDay(19),
{QUrl::fromLocalFile(QStringLiteral("/$19"))}, {QUrl::fromLocalFile(QStringLiteral("file://image$19"))}, 5});
musicDb.insertTracksList(allNewTracks, mNewCovers, QStringLiteral("autoTest"));
QCOMPARE(musicDb.allAlbums().count(), 3);
auto firstAlbum = musicDb.albumFromTitle(QStringLiteral("album1"));
QCOMPARE(firstAlbum.isValid(), true);
QCOMPARE(firstAlbum.title(), QStringLiteral("album1"));
auto firstAlbumInvalid = musicDb.albumFromTitle(QStringLiteral("album1Invalid"));
QCOMPARE(firstAlbumInvalid.isValid(), false);
auto fourthAlbum = musicDb.albumFromTitle(QStringLiteral("album3"));
QCOMPARE(fourthAlbum.isValid(), true);
QCOMPARE(fourthAlbum.title(), QStringLiteral("album3"));
QCOMPARE(fourthAlbum.tracksCount(), 4);
auto oneTrack = fourthAlbum.trackFromIndex(3);
QCOMPARE(oneTrack.isValid(), true);
QCOMPARE(oneTrack.rating(), 5);
}
{
DatabaseInterface musicDb;
musicDb.init(QStringLiteral("testDb2"), myTempDatabase.fileName());
auto allNewTracks = mNewTracks;
allNewTracks.push_back({true, QStringLiteral("$19"), QStringLiteral("0"), QStringLiteral("track6"),
QStringLiteral("artist2"), QStringLiteral("album3"), QStringLiteral("artist2"), 6, 1, QTime::fromMSecsSinceStartOfDay(19),
{QUrl::fromLocalFile(QStringLiteral("/$19"))}, {QUrl::fromLocalFile(QStringLiteral("file://image$19"))}, 3});
musicDb.insertTracksList(allNewTracks, mNewCovers, QStringLiteral("autoTest"));
QCOMPARE(musicDb.allAlbums().count(), 3);
auto firstAlbum = musicDb.albumFromTitle(QStringLiteral("album1"));
QCOMPARE(firstAlbum.isValid(), true);
QCOMPARE(firstAlbum.title(), QStringLiteral("album1"));
auto firstAlbumInvalid = musicDb.albumFromTitle(QStringLiteral("album1Invalid"));
QCOMPARE(firstAlbumInvalid.isValid(), false);
auto fourthAlbum = musicDb.albumFromTitle(QStringLiteral("album3"));
QCOMPARE(fourthAlbum.isValid(), true);
QCOMPARE(fourthAlbum.title(), QStringLiteral("album3"));
QCOMPARE(fourthAlbum.tracksCount(), 4);
auto oneTrack = fourthAlbum.trackFromIndex(3);
QCOMPARE(oneTrack.isValid(), true);
QCOMPARE(oneTrack.rating(), 3);
}
}
void simpleAccessor()
{
auto configDirectory = QDir(QStandardPaths::writableLocation(QStandardPaths::QStandardPaths::AppDataLocation));
......
......@@ -630,10 +630,41 @@ void DatabaseInterface::insertTracksList(QList<MusicAudioTrack> tracks, QHash<QS
return;
}
if (d->mSelectTrackIdFromTitleAlbumIdArtistQuery.next()) {
d->mTrackId = std::max(d->mTrackId, d->mSelectTrackIdFromTitleAlbumIdArtistQuery.record().value(0).toULongLong() + 1);
bool isUpdatedTrack = d->mSelectTrackIdFromTitleAlbumIdArtistQuery.next();
insertTrackOrigin(d->mSelectTrackIdFromTitleAlbumIdArtistQuery.record().value(0).toULongLong(), track.resourceURI(), insertMusicSource(musicSource));
if (isUpdatedTrack) {
auto originTrackId = d->mSelectTrackIdFromTitleAlbumIdArtistQuery.record().value(0).toULongLong();
removeTrackInDatabase(originTrackId);
d->mInsertTrackQuery.bindValue(QStringLiteral(":trackId"), originTrackId);
d->mInsertTrackQuery.bindValue(QStringLiteral(":title"), track.title());
d->mInsertTrackQuery.bindValue(QStringLiteral(":album"), albumId);
d->mInsertTrackQuery.bindValue(QStringLiteral(":artistId"), insertArtist(artistName));
d->mInsertTrackQuery.bindValue(QStringLiteral(":trackNumber"), track.trackNumber());
d->mInsertTrackQuery.bindValue(QStringLiteral(":discNumber"), track.discNumber());
d->mInsertTrackQuery.bindValue(QStringLiteral(":trackDuration"), QVariant::fromValue<qlonglong>(track.duration().msecsSinceStartOfDay()));
d->mInsertTrackQuery.bindValue(QStringLiteral(":trackRating"), track.rating());
result = d->mInsertTrackQuery.exec();
if (result && d->mInsertTrackQuery.isActive()) {
d->mInsertTrackQuery.finish();
insertTrackOrigin(d->mSelectTrackIdFromTitleAlbumIdArtistQuery.record().value(0).toULongLong(), track.resourceURI(), insertMusicSource(musicSource));
Q_EMIT trackModified(internalTrackFromDatabaseId(originTrackId));
d->mTrackId = std::max(d->mTrackId, originTrackId + 1);
} else {
d->mInsertTrackQuery.finish();
qDebug() << "DatabaseInterface::modifyTracksList" << d->mInsertTrackQuery.lastQuery();
qDebug() << "DatabaseInterface::modifyTracksList" << d->mInsertTrackQuery.boundValues();
qDebug() << "DatabaseInterface::modifyTracksList" << d->mInsertTrackQuery.lastError();
continue;
}
d->mSelectTrackIdFromTitleAlbumIdArtistQuery.finish();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment