From 1d143aaab7e47af5a9361e95be0624345a8cb3cb Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Fri, 5 Jul 2024 21:12:47 -0400 Subject: [PATCH] Upgrade native_db to 0.7 --- Cargo.lock | 10 +++---- crates/resolute/Cargo.toml | 2 +- crates/resolute/src/db.rs | 54 +++++++++++++++++++++--------------- crates/resolute/src/mods.rs | 2 +- crates/tauri-app/Cargo.toml | 2 -- crates/tauri-app/src/main.rs | 11 +------- 6 files changed, 39 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5f1b4fc..2b70e98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2734,9 +2734,9 @@ dependencies = [ [[package]] name = "native_db" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab2628b11ef6a479db783b2fabefbd4d2f3ffbe825cc2154cfe3d3c9c779734" +checksum = "ed54dc7c04d201260247cbd1de99d459297fe80f0a79daa4598ef9c6ce4d7782" dependencies = [ "native_db_macro", "native_model", @@ -2749,9 +2749,9 @@ dependencies = [ [[package]] name = "native_db_macro" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e80d1609e8eb0404ea5de614883cdf159b6d875c370c0c1bfbc21d80772399e" +checksum = "163533e9cf510dfdc1f417462beb3651fccdcb9dd027533d85f780081ee212e8" dependencies = [ "proc-macro2", "quote", @@ -3941,8 +3941,6 @@ dependencies = [ "clap", "itertools", "log", - "native_db", - "once_cell", "opener", "path-clean", "reqwest", diff --git a/crates/resolute/Cargo.toml b/crates/resolute/Cargo.toml index e1c5979..13abe92 100644 --- a/crates/resolute/Cargo.toml +++ b/crates/resolute/Cargo.toml @@ -21,7 +21,7 @@ futures-util = "0.3" path-clean = "1.0" sha2 = "0.10" steamlocate = "2.0.0-beta.2" -native_db = { version = "0.6", optional = true } +native_db = { version = "0.7", optional = true } native_model = { version = "0.4", optional = true } redb = { version = "2.0", optional = true } diff --git a/crates/resolute/src/db.rs b/crates/resolute/src/db.rs index 45e41a9..7b0df39 100644 --- a/crates/resolute/src/db.rs +++ b/crates/resolute/src/db.rs @@ -1,10 +1,11 @@ use std::{io::ErrorKind, path::Path}; use log::{info, warn}; -use native_db::{db_type::Error as NativeDbError, Database, DatabaseBuilder}; +use native_db::{db_type::Error as NativeDbError, Builder, Database, Models}; +use once_cell::sync::Lazy; use redb::{DatabaseError, StorageError}; -use crate::{mods::ResoluteMod, Error, Result}; +use crate::{mods::ResoluteMod, Error, Result as ResoluteResult}; /// Wrapper for interacting with a Resolute database #[allow(missing_debug_implementations)] @@ -12,21 +13,22 @@ pub struct ResoluteDatabase<'a> { db: Database<'a>, } -impl<'a> ResoluteDatabase<'a> { +impl ResoluteDatabase<'_> { /// Opens a database using a provided builder. /// If the database doesn't already exist at the given path, it will be created. - pub fn open(builder: &'a DatabaseBuilder, db_path: impl AsRef) -> Result { + pub fn open(db_path: impl AsRef) -> ResoluteResult { info!("Opening database at {}", db_path.as_ref().display()); // Try to open an already-existing database - let db = match builder.open(&db_path) { + let builder = Builder::new(); + let db = match builder.open(&MODELS, &db_path) { // If it fails because it doesn't exist, then go ahead and create one instead Err( NativeDbError::Io(err) | NativeDbError::RedbDatabaseError(DatabaseError::Storage(StorageError::Io(err))), ) if err.kind() == ErrorKind::NotFound => { warn!("Database doesn't exist; creating"); - builder.create(&db_path)? + builder.create(&MODELS, &db_path)? } Ok(db) => db, @@ -37,40 +39,36 @@ impl<'a> ResoluteDatabase<'a> { Ok(Self { db }) } - /// Defines the database models on a builder - pub fn define_models(builder: &mut DatabaseBuilder) -> Result<()> { - builder.define::()?; - Ok(()) - } - /// Retrieves all mods stored in the database - pub fn get_mods(&self) -> Result> { + pub fn get_mods(&self) -> ResoluteResult> { let read = self.db.r_transaction()?; - let mods = read.scan().primary()?.all().collect(); + let mods = read.scan().primary()?.all().collect::>()?; Ok(mods) } /// Retrieves all mods from the database that have an installed version - pub fn get_installed_mods(&self) -> Result> { + pub fn get_installed_mods(&self) -> ResoluteResult> { let read = self.db.r_transaction()?; let mods = read .scan() .primary()? .all() + .collect::, _>>()? + .into_iter() .filter(|rmod: &ResoluteMod| rmod.installed_version.is_some()) .collect(); Ok(mods) } /// Retrieves a single mod from the database by its ID - pub fn get_mod(&self, id: impl AsRef) -> Result> { + pub fn get_mod(&self, id: impl AsRef) -> ResoluteResult> { let read = self.db.r_transaction()?; - let rmod = read.get().primary(id.as_ref().to_owned())?; + let rmod = read.get().primary(id.as_ref())?; Ok(rmod) } /// Stores a mod in the database (overwrites any existing entry for the same mod) - pub fn store_mod(&self, rmod: ResoluteMod) -> Result<()> { + pub fn store_mod(&self, rmod: ResoluteMod) -> ResoluteResult<()> { let mod_name = rmod.to_string(); let rw = self.db.rw_transaction()?; @@ -82,7 +80,7 @@ impl<'a> ResoluteDatabase<'a> { } /// Removes a mod from the database - pub fn remove_mod(&self, rmod: ResoluteMod) -> Result<()> { + pub fn remove_mod(&self, rmod: ResoluteMod) -> ResoluteResult<()> { let mod_name = rmod.to_string(); // Remove the mod @@ -95,13 +93,25 @@ impl<'a> ResoluteDatabase<'a> { } /// Removes a mod from the database by its ID - pub fn remove_mod_by_id(&self, id: impl AsRef) -> Result<()> { + pub fn remove_mod_by_id(&self, id: impl AsRef) -> ResoluteResult<()> { // Find the item in the database - let id = id.as_ref().to_owned(); + let id = id.as_ref(); let read = self.db.r_transaction()?; - let rmod: ResoluteMod = read.get().primary(id.clone())?.ok_or_else(|| Error::ItemNotFound(id))?; + let rmod: ResoluteMod = read + .get() + .primary(id)? + .ok_or_else(|| Error::ItemNotFound(id.to_owned()))?; // Remove it self.remove_mod(rmod) } } + +/// Models that a [`ResoluteDatabase`] interacts with +pub static MODELS: Lazy = Lazy::new(|| { + let mut models = Models::new(); + models + .define::() + .expect("Unable to define ResoluteMod model"); + models +}); diff --git a/crates/resolute/src/mods.rs b/crates/resolute/src/mods.rs index afb57f8..123bea3 100644 --- a/crates/resolute/src/mods.rs +++ b/crates/resolute/src/mods.rs @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize}; use url::Url; #[cfg(feature = "db")] -use native_db::{native_db, InnerKeyValue}; +use native_db::{native_db, ToKey}; #[cfg(feature = "db")] use native_model::{native_model, Model}; diff --git a/crates/tauri-app/Cargo.toml b/crates/tauri-app/Cargo.toml index e331867..6fb95fb 100644 --- a/crates/tauri-app/Cargo.toml +++ b/crates/tauri-app/Cargo.toml @@ -22,8 +22,6 @@ clap = { version = "4.5", features = ["derive"] } url = "2.5" log = "0.4" sha2 = "0.10" -native_db = "0.6" -once_cell = "1.19" itertools = "0.13" path-clean = "1.0" opener = "0.7" diff --git a/crates/tauri-app/src/main.rs b/crates/tauri-app/src/main.rs index 7543810..4616ee9 100644 --- a/crates/tauri-app/src/main.rs +++ b/crates/tauri-app/src/main.rs @@ -77,8 +77,6 @@ use std::{env, error::Error, io, thread, time::Duration}; use anyhow::{bail, Context}; use clap::Parser; use log::{debug, error, info, warn, LevelFilter}; -use native_db::DatabaseBuilder; -use once_cell::sync::Lazy; use resolute::{db::ResoluteDatabase, discover, manager::ModManager, manifest}; use tauri::{async_runtime, App, AppHandle, Manager, WebviewUrl, WebviewWindow, WebviewWindowBuilder, WindowEvent}; use tauri_plugin_deep_link::DeepLinkExt; @@ -90,13 +88,6 @@ use url::Url; mod commands; mod settings; -/// Lazily-initialized database builder for the Resolute DB -static DB_BUILDER: Lazy = Lazy::new(|| { - let mut builder = DatabaseBuilder::new(); - ResoluteDatabase::define_models(&mut builder).expect("unable to define models on database builder"); - builder -}); - #[derive(Debug, Parser)] #[command(version)] struct Cli { @@ -268,7 +259,7 @@ async fn init(app: &AppHandle) -> Result<(), anyhow::Error> { .app_data_dir() .context("Unable to get data dir")? .join("resolute.db"); - let db = ResoluteDatabase::open(&DB_BUILDER, db_path).context("Unable to open database")?; + let db = ResoluteDatabase::open(db_path).context("Unable to open database")?; // Get the Resonite path setting info!("Retrieving Resonite path from settings store");