remove path_cache

This commit is contained in:
2025-10-17 17:42:20 +02:00
parent d003f0783d
commit 757536c277
4 changed files with 1 additions and 53 deletions
-2
View File
@@ -30,7 +30,6 @@ pub struct Folder {
pub id: Uuid,
pub name: String,
pub parent_id: Option<Uuid>,
pub path_cache: Option<String>,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
@@ -41,7 +40,6 @@ pub struct NewFolder {
pub id: Uuid,
pub name: String,
pub parent_id: Option<Uuid>,
pub path_cache: Option<String>,
}
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
-46
View File
@@ -55,7 +55,6 @@ pub struct FolderInfo {
pub id: Uuid,
pub name: String,
pub parent_id: Option<Uuid>,
pub path_cache: Option<String>,
pub created_at: String,
pub updated_at: String,
}
@@ -97,12 +96,10 @@ pub async fn ensure_folder_path(
let folder = if let Some(folder) = existing {
folder
} else {
let path_cache = build_path_cache(conn, current_parent, name)?;
let new_folder = NewFolder {
id: Uuid::new_v4(),
name: name.to_string(),
parent_id: current_parent,
path_cache,
};
diesel::insert_into(folders::table)
@@ -133,13 +130,11 @@ pub async fn create_folder(
}
let mut conn = state.db()?;
let path_cache = build_path_cache(&mut conn, payload.parent_id, &payload.name)?;
let new_folder = NewFolder {
id: Uuid::new_v4(),
name: payload.name.trim().to_string(),
parent_id: payload.parent_id,
path_cache,
};
diesel::insert_into(folders::table)
@@ -346,49 +341,24 @@ pub async fn update_folder(
));
}
let new_path = build_path_cache(conn, next_parent, &new_name)?;
diesel::update(folders::table.find(folder_id))
.set((
folders::parent_id.eq(next_parent),
folders::name.eq(&new_name),
folders::path_cache.eq(new_path),
))
.execute(conn)?;
refresh_descendant_paths(conn, folder_id)?;
Ok(())
})?;
Ok(StatusCode::NO_CONTENT)
}
fn build_path_cache(
conn: &mut PgConnection,
parent_id: Option<Uuid>,
name: &str,
) -> AppResult<Option<String>> {
let path = if let Some(parent_id) = parent_id {
let parent: Folder = folders::table.find(parent_id).first(conn)?;
let base = parent
.path_cache
.unwrap_or_else(|| "/".to_string())
.trim_end_matches('/')
.to_string();
format!("{}/{}", base, name)
} else {
format!("/{}", name)
};
Ok(Some(path))
}
fn folder_to_info(folder: Folder) -> FolderInfo {
FolderInfo {
id: folder.id,
name: folder.name,
parent_id: folder.parent_id,
path_cache: folder.path_cache,
created_at: to_iso(folder.created_at),
updated_at: to_iso(folder.updated_at),
}
@@ -412,19 +382,3 @@ pub(super) fn gather_descendant_folder_ids(
Ok(ids)
}
fn refresh_descendant_paths(conn: &mut PgConnection, parent_id: Uuid) -> AppResult<()> {
let children: Vec<Folder> = folders::table
.filter(folders::parent_id.eq(Some(parent_id)))
.load(conn)?;
for child in children {
let path_cache = build_path_cache(conn, Some(parent_id), &child.name)?;
diesel::update(folders::table.find(child.id))
.set(folders::path_cache.eq(path_cache))
.execute(conn)?;
refresh_descendant_paths(conn, child.id)?;
}
Ok(())
}
-2
View File
@@ -86,8 +86,6 @@ diesel::table! {
#[max_length = 255]
name -> Varchar,
parent_id -> Nullable<Uuid>,
#[max_length = 1000]
path_cache -> Nullable<Varchar>,
created_at -> Timestamptz,
updated_at -> Timestamptz,
}
+1 -3
View File
@@ -16,7 +16,6 @@ struct FolderResponse {
struct FolderInfo {
id: Uuid,
name: String,
path_cache: Option<String>,
}
#[derive(Deserialize)]
@@ -287,7 +286,6 @@ async fn folder_rename_updates_name_and_child_paths() -> Result<()> {
.find(|f| f.id == parent.folder.id)
.expect("renamed folder present");
assert_eq!(renamed.name, "Archive");
assert_eq!(renamed.path_cache.as_deref(), Some("/Archive"));
let child_contents = app
.get(
@@ -299,7 +297,7 @@ async fn folder_rename_updates_name_and_child_paths() -> Result<()> {
let child_contents_body = body_to_vec(child_contents.into_body()).await?;
let child_details: FolderContents = serde_json::from_slice(&child_contents_body)?;
let child_folder = child_details.folder.expect("child folder info");
assert_eq!(child_folder.path_cache.as_deref(), Some("/Archive/Q1"));
assert_eq!(child_folder.name, "Q1");
app.cleanup().await?;
Ok(())