WIP: Refactor the IDatabase calls to write directly to the database #271

Draft
wojtek wants to merge 12 commits from 268---refactor-the-idatabase-calls-to-write-directly-to-the-database into main
5 changed files with 35 additions and 14 deletions
Showing only changes of commit 8a9030bf61 - Show all commits

View File

@ -8,7 +8,7 @@
This feature requires the `sqlite` library. This feature requires the `sqlite` library.
Either install system libraries: with Either install system libraries with:
On Fedora: On Fedora:
``` sh ``` sh
@ -17,6 +17,13 @@ sudo dnf install sqlite-devel
Or use a bundled version by enabling the `database-sqlite-bundled` feature. Or use a bundled version by enabling the `database-sqlite-bundled` feature.
To run the tests you will also need `sqldiff`.
On Fedora:
``` sh
sudo dnf install sqlite-tools
```
#### musicbrainz-api #### musicbrainz-api
This feature requires the `openssl` system library. This feature requires the `openssl` system library.

View File

@ -1,7 +1,3 @@
use std::{fs, path::PathBuf};
use once_cell::sync::Lazy;
use musichoard::{ use musichoard::{
collection::{artist::Artist, Collection}, collection::{artist::Artist, Collection},
external::database::sql::{backend::SqlDatabaseSqliteBackend, SqlDatabase}, external::database::sql::{backend::SqlDatabaseSqliteBackend, SqlDatabase},
@ -9,10 +5,7 @@ use musichoard::{
}; };
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use crate::{copy_file_into_temp, testlib::COLLECTION}; use crate::{copy_file_into_temp, testlib::COLLECTION, COMPLETE_DB_TEST_FILE};
pub static DATABASE_TEST_FILE: Lazy<PathBuf> =
Lazy::new(|| fs::canonicalize("./tests/files/database/database.db").unwrap());
fn expected() -> Collection { fn expected() -> Collection {
let mut expected = COLLECTION.to_owned(); let mut expected = COLLECTION.to_owned();
@ -47,7 +40,7 @@ fn save() {
#[test] #[test]
fn load() { fn load() {
let backend = SqlDatabaseSqliteBackend::new(&*DATABASE_TEST_FILE).unwrap(); let backend = SqlDatabaseSqliteBackend::new(&*COMPLETE_DB_TEST_FILE).unwrap();
let mut database = SqlDatabase::new(backend).unwrap(); let mut database = SqlDatabase::new(backend).unwrap();
let read_data: Vec<Artist> = database.load().unwrap(); let read_data: Vec<Artist> = database.load().unwrap();
@ -74,7 +67,7 @@ fn reverse() {
#[test] #[test]
fn reverse_with_reset() { fn reverse_with_reset() {
let file = copy_file_into_temp(&*DATABASE_TEST_FILE); let file = copy_file_into_temp(&*COMPLETE_DB_TEST_FILE);
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap(); let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
let mut database = SqlDatabase::new(backend).unwrap(); let mut database = SqlDatabase::new(backend).unwrap();

Binary file not shown.

View File

@ -6,7 +6,7 @@ mod library;
mod testlib; mod testlib;
use std::{fs, path::PathBuf}; use std::{ffi::OsStr, fs, path::PathBuf, process::Command};
use musichoard::{ use musichoard::{
external::{ external::{
@ -15,16 +15,33 @@ use musichoard::{
}, },
IMusicHoardBase, IMusicHoardDatabase, IMusicHoardLibrary, MusicHoard, IMusicHoardBase, IMusicHoardDatabase, IMusicHoardLibrary, MusicHoard,
}; };
use once_cell::sync::Lazy;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use crate::testlib::COLLECTION; use crate::testlib::COLLECTION;
pub static PARTIAL_DB_TEST_FILE: Lazy<PathBuf> =
Lazy::new(|| fs::canonicalize("./tests/files/database/partial.db").unwrap());
pub static COMPLETE_DB_TEST_FILE: Lazy<PathBuf> =
Lazy::new(|| fs::canonicalize("./tests/files/database/complete.db").unwrap());
pub fn copy_file_into_temp<P: Into<PathBuf>>(path: P) -> NamedTempFile { pub fn copy_file_into_temp<P: Into<PathBuf>>(path: P) -> NamedTempFile {
let temp = NamedTempFile::new().unwrap(); let temp = NamedTempFile::new().unwrap();
fs::copy(path.into(), temp.path()).unwrap(); fs::copy(path.into(), temp.path()).unwrap();
temp temp
} }
pub fn sqldiff(left: &OsStr, right: &OsStr) {
let output = Command::new("sqldiff")
.arg(left)
.arg(right)
.output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout, "");
}
#[test] #[test]
fn merge_library_then_database() { fn merge_library_then_database() {
// Acquired the lock on the beets config file. We need to own the underlying object so later we // Acquired the lock on the beets config file. We need to own the underlying object so later we
@ -37,7 +54,7 @@ fn merge_library_then_database() {
.config(Some(&*library::beets::BEETS_TEST_CONFIG_PATH)); .config(Some(&*library::beets::BEETS_TEST_CONFIG_PATH));
let library = BeetsLibrary::new(executor); let library = BeetsLibrary::new(executor);
let file = copy_file_into_temp(&*database::sql::DATABASE_TEST_FILE); let file = copy_file_into_temp(&*PARTIAL_DB_TEST_FILE);
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap(); let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
let database = SqlDatabase::new(backend).unwrap(); let database = SqlDatabase::new(backend).unwrap();
@ -47,6 +64,8 @@ fn merge_library_then_database() {
music_hoard.reload_database().unwrap(); music_hoard.reload_database().unwrap();
assert_eq!(music_hoard.get_collection(), &*COLLECTION); assert_eq!(music_hoard.get_collection(), &*COLLECTION);
sqldiff(&*COMPLETE_DB_TEST_FILE.as_os_str(), file.path().as_os_str());
} }
#[test] #[test]
@ -61,7 +80,7 @@ fn merge_database_then_library() {
.config(Some(&*library::beets::BEETS_TEST_CONFIG_PATH)); .config(Some(&*library::beets::BEETS_TEST_CONFIG_PATH));
let library = BeetsLibrary::new(executor); let library = BeetsLibrary::new(executor);
let file = copy_file_into_temp(&*database::sql::DATABASE_TEST_FILE); let file = copy_file_into_temp(&*PARTIAL_DB_TEST_FILE);
let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap(); let backend = SqlDatabaseSqliteBackend::new(file.path()).unwrap();
let database = SqlDatabase::new(backend).unwrap(); let database = SqlDatabase::new(backend).unwrap();
@ -71,4 +90,6 @@ fn merge_database_then_library() {
music_hoard.rescan_library().unwrap(); music_hoard.rescan_library().unwrap();
assert_eq!(music_hoard.get_collection(), &*COLLECTION); assert_eq!(music_hoard.get_collection(), &*COLLECTION);
sqldiff(&*COMPLETE_DB_TEST_FILE.as_os_str(), file.path().as_os_str());
} }