From 424876ee1c84e6ee1c5b41caf1780ebdf7b4cfce Mon Sep 17 00:00:00 2001 From: emeric Date: Tue, 10 Jun 2025 08:52:05 +0200 Subject: [PATCH] Simplified a check in unit tests --- src/libs/database/impl/Session.cpp | 10 ++++++ .../database/include/database/Session.hpp | 2 ++ src/libs/database/test/Common.cpp | 32 ++----------------- .../services/artwork/IArtworkService.hpp | 2 +- 4 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/libs/database/impl/Session.cpp b/src/libs/database/impl/Session.cpp index 02176093..b9e2f3ca 100644 --- a/src/libs/database/impl/Session.cpp +++ b/src/libs/database/impl/Session.cpp @@ -397,6 +397,16 @@ namespace lms::db LMS_LOG(DB, INFO, "Analyze complete!"); } + bool Session::areAllTablesEmpty() + { + const std::vector entryList{ utils::fetchQueryResults(_session.query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")) }; + + return std::all_of(entryList.cbegin(), entryList.cend(), [this](const std::string& entry) { + const auto count{ utils::fetchQuerySingleResult(_session.query("SELECT COUNT(*) FROM " + entry)) }; + return count == 0; + }); + } + void Session::retrieveEntriesToAnalyze(std::vector& entryList) { auto transaction{ createReadTransaction() }; diff --git a/src/libs/database/include/database/Session.hpp b/src/libs/database/include/database/Session.hpp index af85f48b..e1b4cd66 100644 --- a/src/libs/database/include/database/Session.hpp +++ b/src/libs/database/include/database/Session.hpp @@ -96,6 +96,8 @@ namespace lms::db void retrieveEntriesToAnalyze(std::vector& entryList); void analyzeEntry(const std::string& entry); + bool areAllTablesEmpty(); // need to acquire a read transaction + void prepareTablesIfNeeded(); // need to run only once at startup bool migrateSchemaIfNeeded(); // returns true if migration was performed void createIndexesIfNeeded(); diff --git a/src/libs/database/test/Common.cpp b/src/libs/database/test/Common.cpp index 67b62159..2a59b829 100644 --- a/src/libs/database/test/Common.cpp +++ b/src/libs/database/test/Common.cpp @@ -19,23 +19,10 @@ #include "Common.hpp" -#include "database/Artist.hpp" -#include "database/Cluster.hpp" #include "database/Db.hpp" -#include "database/Image.hpp" -#include "database/Listen.hpp" -#include "database/MediaLibrary.hpp" #include "database/Release.hpp" #include "database/Session.hpp" -#include "database/StarredArtist.hpp" -#include "database/StarredRelease.hpp" -#include "database/StarredTrack.hpp" -#include "database/Track.hpp" -#include "database/TrackArtistLink.hpp" -#include "database/TrackBookmark.hpp" -#include "database/TrackList.hpp" #include "database/Types.hpp" -#include "database/User.hpp" namespace lms::db::tests { @@ -75,24 +62,9 @@ namespace lms::db::tests { using namespace db; - auto transaction{ session.createWriteTransaction() }; + auto transaction{ session.createReadTransaction() }; - EXPECT_EQ(Artist::getCount(session), 0); - EXPECT_EQ(Cluster::getCount(session), 0); - EXPECT_EQ(ClusterType::getCount(session), 0); - EXPECT_EQ(Label::getCount(session), 0); - EXPECT_EQ(Listen::getCount(session), 0); - EXPECT_EQ(Image::getCount(session), 0); - EXPECT_EQ(MediaLibrary::getCount(session), 0); - EXPECT_EQ(Release::getCount(session), 0); - EXPECT_EQ(ReleaseType::getCount(session), 0); - EXPECT_EQ(StarredArtist::getCount(session), 0); - EXPECT_EQ(StarredRelease::getCount(session), 0); - EXPECT_EQ(StarredTrack::getCount(session), 0); - EXPECT_EQ(Track::getCount(session), 0); - EXPECT_EQ(TrackBookmark::getCount(session), 0); - EXPECT_EQ(TrackList::getCount(session), 0); - EXPECT_EQ(User::getCount(session), 0); + EXPECT_TRUE(session.areAllTablesEmpty()); } TEST_F(DatabaseFixture, vacuum) diff --git a/src/libs/services/artwork/include/services/artwork/IArtworkService.hpp b/src/libs/services/artwork/include/services/artwork/IArtworkService.hpp index 3a8bb41d..ff4933e0 100644 --- a/src/libs/services/artwork/include/services/artwork/IArtworkService.hpp +++ b/src/libs/services/artwork/include/services/artwork/IArtworkService.hpp @@ -62,7 +62,7 @@ namespace lms::artwork virtual std::shared_ptr getImage(db::ImageId imageId, std::optional width) = 0; virtual std::shared_ptr getTrackEmbeddedImage(db::TrackEmbeddedImageId trackEmbeddedImageId, std::optional width) = 0; - // Svg images dont have image "size" + // Svg images don't have image "size" virtual std::shared_ptr getDefaultReleaseCover() = 0; virtual std::shared_ptr getDefaultArtistImage() = 0;