From f7f83e88c2b6f691693550552bf0e602fec0fe9c Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Sat, 16 Sep 2023 12:17:33 -0700 Subject: [PATCH 1/2] Dont crash the app if search index is not built --- pgml-dashboard/src/utils/markdown.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pgml-dashboard/src/utils/markdown.rs b/pgml-dashboard/src/utils/markdown.rs index b13636c6a..14d8a93d3 100644 --- a/pgml-dashboard/src/utils/markdown.rs +++ b/pgml-dashboard/src/utils/markdown.rs @@ -1193,7 +1193,24 @@ impl SearchIndex { } pub fn open() -> tantivy::Result { - let index = tantivy::Index::open_in_dir(&Self::path())?; + let path = Self::path(); + + if !path.exists() { + std::fs::create_dir(Self::path()) + .expect("failed to create search_index directory, is the filesystem writable?"); + } + + let index = match tantivy::Index::open_in_dir(&Self::path()) { + Ok(index) => index, + Err(err) => { + warn!( + "Failed to open Tantivy index in '{}', creating an empty one, error: {}", + path.display(), + err + ); + Index::create_in_dir(&Self::path(), Self::schema())? + } + }; let reader = index.reader_builder().try_into()?; From b07fd0712e8aab46ac26d37155c995b7e0e60f0b Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Sat, 16 Sep 2023 12:21:49 -0700 Subject: [PATCH 2/2] code clarity --- pgml-dashboard/src/utils/markdown.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pgml-dashboard/src/utils/markdown.rs b/pgml-dashboard/src/utils/markdown.rs index 14d8a93d3..f9b345508 100644 --- a/pgml-dashboard/src/utils/markdown.rs +++ b/pgml-dashboard/src/utils/markdown.rs @@ -1196,11 +1196,11 @@ impl SearchIndex { let path = Self::path(); if !path.exists() { - std::fs::create_dir(Self::path()) + std::fs::create_dir(&path) .expect("failed to create search_index directory, is the filesystem writable?"); } - let index = match tantivy::Index::open_in_dir(&Self::path()) { + let index = match tantivy::Index::open_in_dir(&path) { Ok(index) => index, Err(err) => { warn!( @@ -1208,7 +1208,7 @@ impl SearchIndex { path.display(), err ); - Index::create_in_dir(&Self::path(), Self::schema())? + Index::create_in_dir(&path, Self::schema())? } };