Now resolve the preferred track artworks during scan steps

This commit is contained in:
emeric
2025-06-21 18:13:20 +02:00
parent a1533c4c8d
commit dd2aa49bb0
16 changed files with 704 additions and 176 deletions
+1
View File
@@ -20,6 +20,7 @@
#include "database/Cluster.hpp"
#include "database/Artist.hpp"
#include "database/Artwork.hpp"
#include "database/Directory.hpp"
#include "database/MediaLibrary.hpp"
#include "database/Release.hpp"
+90 -3
View File
@@ -1231,7 +1231,6 @@ FROM tracklist)");
constraint "fk_artwork_image" foreign key ("image_id") references "image" ("id") on delete cascade deferrable initially deferred))");
// Replaced image by artwork for release
// Create the new table, copy the data, drop the old table, rename the new one
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "release_backup" (
"id" integer primary key autoincrement,
"version" integer not null,
@@ -1267,8 +1266,96 @@ FROM release)");
utils::executeCommand(*session.getDboSession(), "DROP TABLE release");
utils::executeCommand(*session.getDboSession(), "ALTER TABLE release_backup RENAME TO release");
// Replaced image by artwork for track
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "track_backup" (
"id" integer primary key autoincrement,
"version" integer not null,
"scan_version" integer not null,
"track_number" integer,
"disc_number" integer,
"total_track" integer,
"disc_subtitle" text not null,
"name" text not null,
"duration" integer,
"bitrate" integer not null,
"bits_per_sample" integer not null,
"channel_count" integer not null,
"sample_rate" integer not null,
"date" text,
"original_date" text,
"absolute_file_path" text not null,
"relative_file_path" text not null,
"file_stem" text not null,
"file_name" text not null,
"file_size" bigint not null,
"file_last_write" text,
"file_added" text,
"mbid" text not null,
"recording_mbid" text not null,
"copyright" text not null,
"copyright_url" text not null,
"advisory" integer not null,
"track_replay_gain" real,
"release_replay_gain" real,
"artist_display_name" text not null,
"comment" text not null,
"release_id" bigint,
"media_library_id" bigint,
"directory_id" bigint,
"preferred_artwork_id" bigint,
"preferred_media_artwork_id" bigint,
constraint "fk_track_release" foreign key ("release_id") references "release" ("id") on delete cascade deferrable initially deferred,
constraint "fk_track_media_library" foreign key ("media_library_id") references "media_library" ("id") on delete set null deferrable initially deferred,
constraint "fk_track_directory" foreign key ("directory_id") references "directory" ("id") on delete cascade deferrable initially deferred,
constraint "fk_track_preferred_artwork" foreign key ("preferred_artwork_id") references "artwork" ("id") on delete set null deferrable initially deferred,
constraint "fk_track_preferred_media_artwork" foreign key ("preferred_media_artwork_id") references "artwork" ("id") on delete set null deferrable initially deferred
))");
// Migrate data, with the new preferred_artwork_id and preferred_media_artwork_id fields set to null
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO track_backup
SELECT
id,
version,
scan_version,
track_number,
disc_number,
total_track,
disc_subtitle,
name,
duration,
bitrate,
bits_per_sample,
channel_count,
sample_rate,
date,
original_date,
absolute_file_path,
relative_file_path,
file_stem,
file_name,
file_size,
file_last_write,
file_added,
mbid,
recording_mbid,
copyright,
copyright_url,
advisory,
track_replay_gain,
release_replay_gain,
artist_display_name,
comment,
release_id,
media_library_id,
directory_id,
NULL as preferred_artwork_id,
NULL as preferred_media_artwork_id
FROM track)");
utils::executeCommand(*session.getDboSession(), "DROP TABLE track");
utils::executeCommand(*session.getDboSession(), "ALTER TABLE track_backup RENAME TO track");
// Just increment the scan version of the settings to make the next scan rescan everything
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET artist_info_scan_version = artist_info_scan_version + 1");
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET audio_scan_version = audio_scan_version + 1");
}
bool doDbMigration(Session& session)
@@ -1339,7 +1426,7 @@ FROM release)");
{ 89, migrateFromV89 },
{ 90, migrateFromV90 },
{ 91, migrateFromV91 },
{ 92, migrateFromV92 }
{ 92, migrateFromV92 },
};
bool migrationPerformed{};
+1
View File
@@ -21,6 +21,7 @@
#include <Wt/Dbo/SqlTraits.h>
#include "database/Artwork.hpp"
#include "database/Directory.hpp"
#include "database/MediaLibrary.hpp"
#include "database/Release.hpp"
+5
View File
@@ -706,6 +706,11 @@ namespace lms::db
return ObjectPtr<Artwork>{ _preferredArtwork };
}
ArtworkId Release::getPreferredArtworkId() const
{
return _preferredArtwork.id();
}
void Release::clearLabels()
{
_labels.clear();
+55
View File
@@ -23,6 +23,7 @@
#include "core/ILogger.hpp"
#include "database/Artist.hpp"
#include "database/Artwork.hpp"
#include "database/Cluster.hpp"
#include "database/Directory.hpp"
#include "database/MediaLibrary.hpp"
@@ -324,6 +325,20 @@ namespace lms::db
return utils::execRangeQuery<TrackId>(query, range);
}
void Track::updatePreferredArtwork(Session& session, TrackId trackId, ArtworkId artworkId)
{
session.checkWriteTransaction();
utils::executeCommand(*session.getDboSession(), "UPDATE track SET preferred_artwork_id = ? WHERE id = ?", artworkId, trackId);
}
void Track::updatePreferredMediaArtwork(Session& session, TrackId trackId, ArtworkId artworkId)
{
session.checkWriteTransaction();
utils::executeCommand(*session.getDboSession(), "UPDATE track SET preferred_media_artwork_id = ? WHERE id = ?", artworkId, trackId);
}
std::vector<Cluster::pointer> Track::getClusters() const
{
return utils::fetchQueryResults<Cluster::pointer>(_clusters.find());
@@ -338,6 +353,26 @@ namespace lms::db
return utils::fetchQueryResults(query);
}
ObjectPtr<MediaLibrary> Track::getMediaLibrary() const
{
return _mediaLibrary;
}
ObjectPtr<Directory> Track::getDirectory() const
{
return _directory;
}
ObjectPtr<Artwork> Track::getPreferredArtwork() const
{
return _preferredArtwork;
}
ObjectPtr<Artwork> Track::getPreferredMediaArtwork() const
{
return _preferredMediaArtwork;
}
RangeResults<TrackId> Track::findIds(Session& session, const FindParameters& parameters)
{
session.checkReadTransaction();
@@ -484,6 +519,26 @@ namespace lms::db
_embeddedImageLinks.insert(getDboPtr(image));
}
void Track::setMediaLibrary(ObjectPtr<MediaLibrary> mediaLibrary)
{
_mediaLibrary = getDboPtr(mediaLibrary);
}
void Track::setDirectory(ObjectPtr<Directory> directory)
{
_directory = getDboPtr(directory);
}
void Track::setPreferredArtwork(ObjectPtr<Artwork> artwork)
{
_preferredArtwork = getDboPtr(artwork);
}
void Track::setPreferredMediaArtwork(ObjectPtr<Artwork> artwork)
{
_preferredMediaArtwork = getDboPtr(artwork);
}
std::optional<int> Track::getYear() const
{
return _date.getYear();
+19 -5
View File
@@ -35,6 +35,7 @@
#include "core/PartialDateTime.hpp"
#include "core/UUID.hpp"
#include "database/ArtistId.hpp"
#include "database/ArtworkId.hpp"
#include "database/ClusterId.hpp"
#include "database/DirectoryId.hpp"
#include "database/Filters.hpp"
@@ -50,6 +51,7 @@
namespace lms::db
{
class Artist;
class Artwork;
class Cluster;
class ClusterType;
class Directory;
@@ -215,6 +217,10 @@ namespace lms::db
static RangeResults<TrackId> findIdsTrackMBIDDuplicates(Session& session, std::optional<Range> range = std::nullopt);
static RangeResults<TrackId> findIdsWithRecordingMBIDAndMissingFeatures(Session& session, std::optional<Range> range = std::nullopt);
// Update utility functions
static void updatePreferredArtwork(Session& session, TrackId trackId, ArtworkId artworkId);
static void updatePreferredMediaArtwork(Session& session, TrackId trackId, ArtworkId artworkId);
// Accessors
void setScanVersion(std::size_t version) { _scanVersion = version; }
void setTrackNumber(std::optional<int> num) { _trackNumber = num; }
@@ -252,8 +258,10 @@ namespace lms::db
void addLyrics(const ObjectPtr<TrackLyrics>& lyrics);
void clearEmbeddedImageLinks();
void addEmbeddedImageLink(const ObjectPtr<TrackEmbeddedImageLink>& link);
void setMediaLibrary(ObjectPtr<MediaLibrary> mediaLibrary) { _mediaLibrary = getDboPtr(mediaLibrary); }
void setDirectory(ObjectPtr<Directory> directory) { _directory = getDboPtr(directory); }
void setMediaLibrary(ObjectPtr<MediaLibrary> mediaLibrary);
void setDirectory(ObjectPtr<Directory> directory);
void setPreferredArtwork(ObjectPtr<Artwork> artwork);
void setPreferredMediaArtwork(ObjectPtr<Artwork> artwork);
std::size_t getScanVersion() const { return _scanVersion; }
std::optional<std::size_t> getTrackNumber() const { return _trackNumber; }
@@ -295,8 +303,10 @@ namespace lms::db
ObjectPtr<Release> getRelease() const { return _release; }
std::vector<ObjectPtr<Cluster>> getClusters() const;
std::vector<ClusterId> getClusterIds() const;
ObjectPtr<MediaLibrary> getMediaLibrary() const { return _mediaLibrary; }
ObjectPtr<Directory> getDirectory() const { return _directory; }
ObjectPtr<MediaLibrary> getMediaLibrary() const;
ObjectPtr<Directory> getDirectory() const;
ObjectPtr<Artwork> getPreferredArtwork() const;
ObjectPtr<Artwork> getPreferredMediaArtwork() const;
std::vector<std::vector<ObjectPtr<Cluster>>> getClusterGroups(const std::vector<ClusterTypeId>& clusterTypes, std::size_t size) const;
@@ -331,11 +341,13 @@ namespace lms::db
Wt::Dbo::field(a, _trackReplayGain, "track_replay_gain");
Wt::Dbo::field(a, _releaseReplayGain, "release_replay_gain"); // here in Track since Release does not have concept of "disc" (yet?)
Wt::Dbo::field(a, _artistDisplayName, "artist_display_name");
Wt::Dbo::field(a, _comment, "comment");
Wt::Dbo::field(a, _comment, "comment"); // TODO: move in a dedicated table
Wt::Dbo::belongsTo(a, _release, "release", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _mediaLibrary, "media_library", Wt::Dbo::OnDeleteSetNull); // don't delete track on media library removal, we want to wait for the next scan to have a chance to migrate files
Wt::Dbo::belongsTo(a, _directory, "directory", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _preferredArtwork, "preferred_artwork", Wt::Dbo::OnDeleteSetNull);
Wt::Dbo::belongsTo(a, _preferredMediaArtwork, "preferred_media_artwork", Wt::Dbo::OnDeleteSetNull);
Wt::Dbo::hasMany(a, _trackArtistLinks, Wt::Dbo::ManyToOne, "track");
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToMany, "track_cluster", "", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _trackLyrics, Wt::Dbo::ManyToOne, "track");
@@ -382,6 +394,8 @@ namespace lms::db
Wt::Dbo::ptr<Release> _release;
Wt::Dbo::ptr<MediaLibrary> _mediaLibrary;
Wt::Dbo::ptr<Directory> _directory;
Wt::Dbo::ptr<Artwork> _preferredArtwork;
Wt::Dbo::ptr<Artwork> _preferredMediaArtwork;
Wt::Dbo::collection<Wt::Dbo::ptr<TrackArtistLink>> _trackArtistLinks;
Wt::Dbo::collection<Wt::Dbo::ptr<Cluster>> _clusters;
Wt::Dbo::collection<Wt::Dbo::ptr<TrackLyrics>> _trackLyrics;
+12
View File
@@ -1220,6 +1220,18 @@ namespace lms::db::tests
ASSERT_TRUE(releaseArtwork);
EXPECT_EQ(releaseArtwork->getId(), artwork.getId());
}
// Check cascade delete
{
auto transaction{ session.createWriteTransaction() };
image.lockAndGet().remove();
}
{
auto transaction{ session.createReadTransaction() };
auto releaseArtwork(release.get()->getPreferredArtwork());
ASSERT_FALSE(releaseArtwork);
}
}
TEST_F(DatabaseFixture, Release_sortDateAdded)