Fetching and then instantly reloading library loses data #256

Merged
wojtek merged 6 commits from 254---fetching-and-then-instantly-reloading-library-loses-data into main 2025-01-05 11:06:05 +01:00
4 changed files with 28 additions and 22 deletions
Showing only changes of commit ce3571e393 - Show all commits

View File

@ -13,8 +13,8 @@ pub trait IDatabase {
/// Load collection from the database.
fn load(&self) -> Result<Collection, LoadError>;
/// Save collection to the database.
fn save(&mut self, collection: &Collection) -> Result<(), SaveError>;
/// Save collection to the database. Return the written data.
fn save(&mut self, collection: &Collection) -> Result<Collection, SaveError>;
}
/// Null database implementation of [`IDatabase`].
@ -25,8 +25,8 @@ impl IDatabase for NullDatabase {
Ok(vec![])
}
fn save(&mut self, _collection: &Collection) -> Result<(), SaveError> {
Ok(())
fn save(&mut self, _collection: &Collection) -> Result<Collection, SaveError> {
Ok(vec![])
}
}

View File

@ -374,10 +374,15 @@ impl<Library> IMusicHoardDatabasePrivate for MusicHoard<NoDatabase, Library> {
impl<Database: IDatabase, Library> IMusicHoardDatabasePrivate for MusicHoard<Database, Library> {
fn commit(&mut self) -> Result<(), Error> {
if self.collection != self.pre_commit {
if let Err(err) = self.database.save(&self.pre_commit) {
match self.database.save(&self.pre_commit) {
Ok(collection) => {
self.database_cache = collection;
}
Err(err) => {
self.pre_commit = self.collection.clone();
return Err(err.into());
}
}
self.collection = self.pre_commit.clone();
self.filtered = self.filter_collection();
}
@ -496,13 +501,13 @@ mod tests {
.times(1)
.in_sequence(&mut seq)
.with(predicate::eq(with_artist.clone()))
.returning(|_| Ok(()));
.returning(|_| Ok(vec![]));
database
.expect_save()
.times(1)
.in_sequence(&mut seq)
.with(predicate::eq(collection.clone()))
.returning(|_| Ok(()));
.returning(|_| Ok(vec![]));
let mut music_hoard = MusicHoard::database(database).unwrap();
assert_eq!(music_hoard.collection, collection);
@ -524,7 +529,7 @@ mod tests {
fn artist_sort_set_clear() {
let mut database = MockIDatabase::new();
database.expect_load().times(1).returning(|| Ok(vec![]));
database.expect_save().times(4).returning(|_| Ok(()));
database.expect_save().times(4).returning(|_| Ok(vec![]));
type MH = MusicHoard<MockIDatabase, NoLibrary>;
let mut music_hoard: MH = MusicHoard::database(database).unwrap();
@ -592,7 +597,7 @@ mod tests {
fn set_clear_artist_mb_ref() {
let mut database = MockIDatabase::new();
database.expect_load().times(1).returning(|| Ok(vec![]));
database.expect_save().times(3).returning(|_| Ok(()));
database.expect_save().times(3).returning(|_| Ok(vec![]));
let mut artist_id = ArtistId::new("an artist");
let artist_id_2 = ArtistId::new("another artist");
@ -638,7 +643,7 @@ mod tests {
fn set_clear_artist_info() {
let mut database = MockIDatabase::new();
database.expect_load().times(1).returning(|| Ok(vec![]));
database.expect_save().times(3).returning(|_| Ok(()));
database.expect_save().times(3).returning(|_| Ok(vec![]));
let artist_id = ArtistId::new("an artist");
let artist_id_2 = ArtistId::new("another artist");
@ -682,7 +687,7 @@ mod tests {
fn add_to_remove_from_property() {
let mut database = MockIDatabase::new();
database.expect_load().times(1).returning(|| Ok(vec![]));
database.expect_save().times(3).returning(|_| Ok(()));
database.expect_save().times(3).returning(|_| Ok(vec![]));
let artist_id = ArtistId::new("an artist");
let artist_id_2 = ArtistId::new("another artist");
@ -731,7 +736,7 @@ mod tests {
fn set_clear_property() {
let mut database = MockIDatabase::new();
database.expect_load().times(1).returning(|| Ok(vec![]));
database.expect_save().times(3).returning(|_| Ok(()));
database.expect_save().times(3).returning(|_| Ok(vec![]));
let artist_id = ArtistId::new("an artist");
let artist_id_2 = ArtistId::new("another artist");
@ -797,13 +802,13 @@ mod tests {
.times(1)
.in_sequence(&mut seq)
.with(predicate::eq(with_album.clone()))
.returning(|_| Ok(()));
.returning(|_| Ok(vec![]));
database
.expect_save()
.times(1)
.in_sequence(&mut seq)
.with(predicate::eq(collection.clone()))
.returning(|_| Ok(()));
.returning(|_| Ok(vec![]));
let mut music_hoard = MusicHoard::database(database).unwrap();
assert_eq!(music_hoard.collection, collection);
@ -842,7 +847,7 @@ mod tests {
.expect_load()
.times(1)
.return_once(|| Ok(database_result));
database.expect_save().times(2).returning(|_| Ok(()));
database.expect_save().times(2).returning(|_| Ok(vec![]));
let mut music_hoard = MusicHoard::database(database).unwrap();
let album = &music_hoard.collection[0].albums[0];
@ -900,7 +905,7 @@ mod tests {
.expect_load()
.times(1)
.return_once(|| Ok(database_result));
database.expect_save().times(2).returning(|_| Ok(()));
database.expect_save().times(2).returning(|_| Ok(vec![]));
let mut music_hoard = MusicHoard::database(database).unwrap();
assert_eq!(music_hoard.collection[0].albums[0].meta.seq, AlbumSeq(0));
@ -940,7 +945,7 @@ mod tests {
.expect_load()
.times(1)
.return_once(|| Ok(database_result));
database.expect_save().times(2).returning(|_| Ok(()));
database.expect_save().times(2).returning(|_| Ok(vec![]));
let mut music_hoard = MusicHoard::database(database).unwrap();
let meta = &music_hoard.collection[0].albums[0].meta;

View File

@ -157,7 +157,7 @@ mod tests {
.expect_save()
.with(predicate::eq(&*LIBRARY_COLLECTION))
.times(1)
.return_once(|_| Ok(()));
.return_once(|_| Ok(vec![]));
let mut music_hoard = MusicHoard::new(database, library).unwrap();

View File

@ -55,11 +55,12 @@ impl<JDB: IJsonDatabaseBackend> IDatabase for JsonDatabase<JDB> {
Ok(database.into())
}
fn save(&mut self, collection: &Collection) -> Result<(), SaveError> {
fn save(&mut self, collection: &Collection) -> Result<Collection, SaveError> {
let database: SerializeDatabase = collection.into();
let serialized = serde_json::to_string(&database)?;
let deserialized: DeserializeDatabase = serde_json::from_str(&serialized)?;
self.backend.write(&serialized)?;
Ok(())
Ok(deserialized.into())
}
}