work on database structure and added function stubs for file entry

This commit is contained in:
Iaphetes 2025-03-13 21:25:21 +01:00
parent 615e09db16
commit 5b31b76e69

View file

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