|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use sqlx::{ |
| 4 | + migrate::Migrator, |
| 5 | + sqlite::{SqliteConnectOptions, SqlitePoolOptions}, |
| 6 | + ConnectOptions, SqlitePool, |
| 7 | +}; |
| 8 | + |
| 9 | +// db 存储方案: SqlClient |
| 10 | +pub struct SqliteClient { |
| 11 | + pub cli: SqlitePool, |
| 12 | +} |
| 13 | + |
| 14 | +impl Clone for SqliteClient { |
| 15 | + fn clone(&self) -> Self { |
| 16 | + Self { cli: self.cli.clone() } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl SqliteClient { |
| 21 | + pub async fn default() -> Self { |
| 22 | + Self::new(None, None).await |
| 23 | + } |
| 24 | + |
| 25 | + pub async fn new(db_name: Option<&str>, migrations: Option<&str>) -> Self { |
| 26 | + // let mut _root_dir = tauri::api::path::document_dir(); |
| 27 | + |
| 28 | + // sqlite:tmp/app.db |
| 29 | + // let url = env::var("").unwrap_or(util::app_cfg::get_local_sqlite_url(Some(db_name))); |
| 30 | + let db_url = util::fmt_local_sqlite_url(db_name); |
| 31 | + |
| 32 | + // todo x: 仅用于创建 db 文件 |
| 33 | + let _ = SqliteConnectOptions::from_str(&db_url) |
| 34 | + .unwrap() |
| 35 | + .create_if_missing(true) |
| 36 | + .connect() |
| 37 | + .await |
| 38 | + .expect("connect to sqlite failed"); |
| 39 | + |
| 40 | + // connect pool |
| 41 | + let cli = SqlitePoolOptions::new().min_connections(2).connect(&db_url).await; |
| 42 | + |
| 43 | + match cli { |
| 44 | + Ok(c) => { |
| 45 | + let dir = migrations.unwrap_or("./migrations"); |
| 46 | + |
| 47 | + // if dir exist, do migrate |
| 48 | + if std::path::Path::new(dir).exists() { |
| 49 | + // todo x: 自动执行 db migrations |
| 50 | + let m = |
| 51 | + Migrator::new(std::path::Path::new(dir)).await.expect("migrations failed"); |
| 52 | + |
| 53 | + match m.run(&c).await { |
| 54 | + Ok(_) => { |
| 55 | + println!("migrations run success"); |
| 56 | + }, |
| 57 | + Err(e) => { |
| 58 | + panic!("migrations run error: {:?}", e); |
| 59 | + }, |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // // todo x: 初始化表结构 |
| 64 | + // sqlx::migrate!(migrations.unwrap_or("./migrations")) |
| 65 | + // .run(&conn) |
| 66 | + // .await |
| 67 | + // .expect("migrate failed"); |
| 68 | + |
| 69 | + Self { cli: c } |
| 70 | + }, |
| 71 | + Err(e) => { |
| 72 | + panic!("connect to sqlite db failed: {}", e); |
| 73 | + }, |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +pub mod util { |
| 79 | + use std::{env, path::PathBuf}; |
| 80 | + |
| 81 | + pub fn local_tmp_dir() -> PathBuf { |
| 82 | + let tmp_local = "tmp"; |
| 83 | + |
| 84 | + let mut current_dir = env::current_dir().expect("get current directory failed"); |
| 85 | + let tmp_dir = current_dir.join(tmp_local); |
| 86 | + if !tmp_dir.exists() { |
| 87 | + std::fs::create_dir(&tmp_dir).expect("create tmp dir failed"); |
| 88 | + } |
| 89 | + tmp_dir |
| 90 | + } |
| 91 | + |
| 92 | + pub fn fmt_local_sqlite_url(db_name: Option<&str>) -> String { |
| 93 | + let fp = db_name.unwrap_or("app.db"); |
| 94 | + let prefix = "sqlite:"; |
| 95 | + format!("{}{}", prefix, local_tmp_dir().join(fp).to_str().unwrap()) |
| 96 | + } |
| 97 | + |
| 98 | + #[cfg(test)] |
| 99 | + mod test { |
| 100 | + use super::*; |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_get_local_sqlite_url() { |
| 104 | + let cases = [None, Some("test.db")]; |
| 105 | + |
| 106 | + // iter |
| 107 | + for case in cases.iter() { |
| 108 | + let url = fmt_local_sqlite_url(*case); |
| 109 | + println!("url: {}", url); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#[cfg(test)] |
| 116 | +mod test { |
| 117 | + use tokio; |
| 118 | + |
| 119 | + use super::*; |
| 120 | + |
| 121 | + #[tokio::test] |
| 122 | + async fn test_new_sqlite_client() { |
| 123 | + let db = SqliteClient::default().await; |
| 124 | + } |
| 125 | +} |
0 commit comments