fixed diff db

This commit is contained in:
Iaphetes 2025-05-13 07:15:18 +02:00
parent 3951f91901
commit b38d0cc149
2 changed files with 23 additions and 25 deletions

View file

@ -6,8 +6,9 @@ use std::{
};
use crate::{config::_get_client_id, get_basepath};
use futures::FutureExt;
use serde::{Deserialize, Serialize};
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
use sqlx::{migrate::MigrateDatabase, sqlite::SqliteConnectOptions, Sqlite, SqlitePool};
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
struct DBFileEntry {
@ -76,17 +77,18 @@ pub async fn initialize_database(app_handle: tauri::AppHandle) -> Result<(), DBE
client_id TEXT NOT NULL,
diff_text TEXT NOT NULL,
current_diff BOOLEAN,
FOREIGN KEY (id)
REFERENCES file_diffs (parent_diff_id)
FOREIGN KEY (id)
REFERENCES files (file_id)
parent_diff_id INTEGER,
file_id INTEGER,
FOREIGN KEY (parent_diff_id) REFERENCES file_diffs(id),
FOREIGN KEY (file_id) REFERENCES files(id),
PRIMARY KEY (id, client_id)
)
;",
)
.execute(&db)
.await
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
.unwrap();
// .map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
db.close().await;
Ok(())
}
@ -195,6 +197,7 @@ pub async fn store_diff(
// FOREIGN KEY (id)
// REFERENCES files (file_id)
// PRIMARY KEY (id, client_id)
let file_id = get_file_id(app_handle, relative_file_path, file_name).await?;
let _ = sqlx::query(&format!(
"
UPDATE file_diffs
@ -210,23 +213,9 @@ pub async fn store_diff(
let _ = sqlx::query(&format!(
"
INSERT INTO
file_diffs (client_id, diff_text, current_diff, file_diffs, file_id)
file_diffs (client_id, diff_text, current_diff, file_id)
VALUES
('{client_id}', '{diff}', 1, (
SELECT id
FROM file_diffs
WHERE relative_path IS '{relative_file_path}'
AND file_name IS '{file_name}'
AND current_diff is 1
LIMIT 1
),
(
SELECT id
FROM files
WHERE relative_path IS '{relative_file_path}'
AND file_name IS '{file_name}'
LIMIT 1
));"
('{client_id}', '{diff}', 1, );"
))
.execute(&db)
.await

View file

@ -4,8 +4,8 @@ use crate::{
get_basepath,
};
use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput};
use std::{fs::read_to_string, path::PathBuf};
use std::path::Path;
use std::{fs::read_to_string, path::PathBuf};
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
fn compare_content(old_content: &str, new_content: &str) -> Result<String, Error> {
@ -22,10 +22,19 @@ fn compare_content(old_content: &str, new_content: &str) -> Result<String, Error
}
#[tauri::command]
pub async fn save_file(app_handle: tauri::AppHandle, content: String) -> Result<(), String> {
pub async fn save_file(app_handle: tauri::AppHandle, content: String) -> Result<(), String> {
if let Some(path) = get_open_file_path(app_handle.clone()) {
if let Ok(file_content) = load_file(app_handle.clone(), &path).await {
store_diff(&app_handle, &path.clone(), &PathBuf::from(path).file_name().unwrap().to_string_lossy(), compare_content(&file_content, &content).map_err(|e| format!("{e:?}"))?);
println!(
"{:?}",
store_diff(
&app_handle,
&path.clone(),
&PathBuf::from(path).file_name().unwrap().to_string_lossy(),
compare_content(&file_content, &content).map_err(|e| format!("{e:?}"))?,
)
.await
);
println!("{:?}", compare_content(&file_content, &content));
}
}