From 92fe596fd82dc053b45b68928aa23d65584af98b Mon Sep 17 00:00:00 2001 From: fluo10 Date: Tue, 2 Sep 2025 08:50:09 +0900 Subject: [PATCH] Update global database connection --- core/src/global/database_connection.rs | 105 ++++++------------------- core/src/migration.rs | 7 -- 2 files changed, 25 insertions(+), 87 deletions(-) delete mode 100644 core/src/migration.rs diff --git a/core/src/global/database_connection.rs b/core/src/global/database_connection.rs index 4d45b1a..d1a7e05 100644 --- a/core/src/global/database_connection.rs +++ b/core/src/global/database_connection.rs @@ -1,106 +1,51 @@ -use std::path::{Path, PathBuf}; +use std::{path::{Path, PathBuf}, sync::OnceLock}; use dirs::cache_dir; -use sea_orm::{ConnectOptions, Database, DbErr, DatabaseConnection}; -use sea_orm_migration::MigratorTrait; -use crate::{cache::migration::CacheMigrator, config::StorageConfig, error::Error}; +use rusqlite::Connection; +use crate::{cache::migration::CacheMigrator, config::StorageConfig, data::local::migration::migrate, error::Error}; use tokio::sync::OnceCell; -pub static DATABASE_CONNECTIONS: GlobalDatabaseConnections = GlobalDatabaseConnections::const_new(); +pub static LOCAL_DATABASE_CONNECTION: GlobalDatabaseConnection = GlobalDatabaseConnection::const_new(); -pub struct DatabaseConnections<'a> { - pub data: &'a DatabaseConnection, - pub cache: &'a DatabaseConnection +pub struct GlobalDatabaseConnection { + inner: OnceLock } -pub struct GlobalDatabaseConnections { - data: OnceCell, - cache: OnceCell, -} - -impl GlobalDatabaseConnections { +impl GlobalDatabaseConnection { pub const fn const_new() -> Self { Self { - data: OnceCell::const_new(), - cache: OnceCell::const_new() + inner: OnceLock::new() } } - pub fn get_data(&'static self) -> Option<&'static DatabaseConnection> { - self.data.get() + pub fn get(&'static self) -> Option<&'static Connection> { + self.inner.get() } - pub fn get_data_unchecked(&'static self) -> &'static DatabaseConnection { - self.get_data().expect("Global data database connection should initialized before access!") + pub fn get_unchecked(&'static self) -> &'static Connection { + self.get().expect("local data database connection should initialized before access!") } - pub fn get_cache(&'static self) -> Option<&'static DatabaseConnection> { - self.cache.get() - } - - pub fn get_cache_unchecked(&'static self) -> &'static DatabaseConnection { - self.get_cache().expect("Global cache database connection should initialized before access!") - } - - fn get_data_file_path(config: &T) -> PathBuf + fn get_file_path(config: &T) -> PathBuf where T: AsRef { - config.as_ref().data_directory.join("data.sqlite") - } - - fn get_cache_file_path(config: &T) -> PathBuf - where - T: AsRef - { - config.as_ref().cache_directory.join("cache.sqlite") + config.as_ref().data_directory.join("local.sqlite") } - fn get_url_unchecked(path: T) -> String - where - T: AsRef - { - "sqlite://".to_string() + path.as_ref().as_os_str().to_str().expect("Failed to convert path to string!") + "?mode=rwc" - } - - async fn get_or_init_database_connection_unchecked(cell: &OnceCell, options: T, _: U ) -> &DatabaseConnection - where - T: Into, - U: MigratorTrait - { - cell.get_or_init(|| async { - let db = Database::connect(options.into()).await.unwrap(); - U::up(&db, None).await.unwrap(); - db - }).await - } - - - pub async fn get_or_init_unchecked(&'static self, config: T, _migrator: U) -> DatabaseConnections + pub async fn get_or_init_unchecked(&'static self, config: T) -> Connection where T: AsRef, - U: MigratorTrait, { - let data_path = Self::get_data_file_path(&config); - if let Some(x) = data_path.parent() { - std::fs::create_dir_all(x).expect("Failed to create directory for data database"); - } - let cache_path = Self::get_cache_file_path(&config); - if let Some(x) = cache_path.parent() { - std::fs::create_dir_all(x).expect("Failed to create directory for cache database"); - } - DatabaseConnections{ - data: Self::get_or_init_database_connection_unchecked( - &self.data, - Self::get_url_unchecked(data_path), - _migrator - ).await, - cache: Self::get_or_init_database_connection_unchecked( - &self.cache, - Self::get_url_unchecked(cache_path), - CacheMigrator - ).await, + let path = Self::get_file_path(&config); + if let Some(x) = path.parent() { + std::fs::create_dir_all(x).expect("Failed to create directory for local database"); } + self.inner.get_or_init(|| { + let db = Connection::open(path)?; + migrate(&db).unwrap(); + db + }) } } @@ -112,10 +57,10 @@ pub use tests::*; #[cfg(test)] mod tests { use super::*; - use crate::{cache::migration::CacheMigrator, data::migration::DataMigrator, global::CONFIG, tests::{TEST_CONFIG}}; + use crate::{cache::migration::CacheMigrator, global::CONFIG, tests::{TEST_CONFIG}}; #[tokio::test] pub async fn get_or_init_database() { - DATABASE_CONNECTIONS.get_or_init_unchecked(&*TEST_CONFIG, DataMigrator).await; + LOCAL_DATABASE_CONNECTION.get_or_init_unchecked(&*TEST_CONFIG).await; } } diff --git a/core/src/migration.rs b/core/src/migration.rs deleted file mode 100644 index 63c43d5..0000000 --- a/core/src/migration.rs +++ /dev/null @@ -1,7 +0,0 @@ -use sea_orm_migration::{prelude::*, schema::*}; - -#[async_trait::async_trait] -pub trait TableMigration { - async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> ; - async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr>; -} \ No newline at end of file