diff --git a/src-tauri/src/database.rs b/src-tauri/src/database.rs index 4071975..4618e89 100644 --- a/src-tauri/src/database.rs +++ b/src-tauri/src/database.rs @@ -4,9 +4,9 @@ use std::{ path::Path, }; +use crate::get_basepath; use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool}; use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url}; -use crate::get_basepath; async fn populate_file_db(db: &SqlitePool) {} #[tauri::command] @@ -15,60 +15,61 @@ pub async fn initialize_database( basepath: String, pathtemplate: String, ) { - if let Some(basepath) = get_basepath(app_handle.clone()) { - let db_path = Path::new("Documents") - .join(Path::new("Knowledgebase")) - .join("db.sqlite"); + let db_path = Path::new("Documents") + .join(Path::new("Knowledgebase")) + .join("db.sqlite"); - let resolved_db_path = match app_handle - .path() - .resolve(db_path, tauri::path::BaseDirectory::Home) - { - Ok(resolved_knowledgebase_path) => resolved_knowledgebase_path, + let resolved_db_path = match app_handle + .path() + .resolve(db_path, tauri::path::BaseDirectory::Home) + { + Ok(resolved_knowledgebase_path) => resolved_knowledgebase_path, - Err(_) => return, - }; - let unicode_db_path = &resolved_db_path.to_string_lossy().to_owned(); - if !Sqlite::database_exists(&unicode_db_path) - .await - .unwrap_or(false) - { - match Sqlite::create_database(&unicode_db_path).await { - Ok(_) => println!("Create db success"), - Err(error) => panic!("error: {}", error), + Err(_) => return, + }; + let unicode_db_path = &resolved_db_path.to_string_lossy().to_owned(); + if !Sqlite::database_exists(&unicode_db_path) + .await + .unwrap_or(false) + { + match Sqlite::create_database(&unicode_db_path).await { + Ok(_) => println!("Create db success"), + Err(error) => panic!("error: {}", error), + } } - } - let db = SqlitePool::connect(unicode_db_path).await.unwrap(); - let result = sqlx::query( - "CREATE TABLE IF NOT EXISTS files + let db = SqlitePool::connect(unicode_db_path).await.unwrap(); + let result = sqlx::query( + "CREATE TABLE IF NOT EXISTS files ( id INTEGER PRIMARY KEY NOT NULL, relative_path TEXT NOT NULL, file_name TEXT NOT NULL );", - ) - .execute(&db) - .await - .unwrap(); + ) + .execute(&db) + .await + .unwrap(); - let result = sqlx::query( - "CREATE TABLE IF NOT EXISTS file_diffs + let result = sqlx::query( + "CREATE TABLE IF NOT EXISTS file_diffs ( - id INTEGER PRIMARY KEY NOT NULL, + id INTEGER NOT NULL, + client_id INTEGER NOT NULL, diff_text TEXT NOT NULL, FOREIGN KEY (id) REFERENCES files (file_id) + PRIMARY KEY (id, client_id) ) ;", - ) - .execute(&db) - .await - .unwrap(); + ) + .execute(&db) + .await + .unwrap(); } } #[tauri::command] -async fn store_diff(diff: String) { +async fn store_diff(diff: String) {} -} +async fn add_file(file_path: String, file_name: String) {}