🎨 Supports cleaning up unreferenced databases https://github.com/siyuan-note/siyuan/issues/11569

Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
Daniel 2026-01-28 21:04:59 +08:00
parent 6c70144d7c
commit 5c4660f520
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 51 additions and 0 deletions

View file

@ -28,6 +28,22 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func removeUnusedAttributeView(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
p := arg["path"].(string)
asset := model.RemoveUnusedAttributeView(p)
ret.Data = map[string]interface{}{
"path": asset,
}
}
func removeUnusedAttributeViews(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

View file

@ -490,6 +490,8 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/av/getAttributeViewBoundBlockIDsByItemIDs", model.CheckAuth, getAttributeViewBoundBlockIDsByItemIDs)
ginServer.Handle("POST", "/api/av/getAttributeViewItemIDsByBoundIDs", model.CheckAuth, getAttributeViewItemIDsByBoundIDs)
ginServer.Handle("POST", "/api/av/getUnusedAttributeViews", model.CheckAuth, getUnusedAttributeViews)
ginServer.Handle("POST", "/api/av/removeUnusedAttributeViews", model.CheckAuth, removeUnusedAttributeViews)
ginServer.Handle("POST", "/api/av/removeUnusedAttributeView", model.CheckAuth, removeUnusedAttributeView)
ginServer.Handle("POST", "/api/ai/chatGPT", model.CheckAuth, model.CheckAdminRole, chatGPT)
ginServer.Handle("POST", "/api/ai/chatGPTWithAction", model.CheckAuth, model.CheckAdminRole, chatGPTWithAction)

View file

@ -46,6 +46,39 @@ import (
"github.com/xrash/smetrics"
)
func RemoveUnusedAttributeView(p string) (ret string) {
absPath := filepath.Join(util.DataDir, "storage", "av", p+".json")
if !filelock.IsExist(absPath) {
return absPath
}
historyDir, err := GetHistoryDir(HistoryOpClean)
if err != nil {
logging.LogErrorf("get history dir failed: %s", err)
return
}
newP := strings.TrimPrefix(absPath, util.DataDir)
historyPath := filepath.Join(historyDir, newP)
if filelock.IsExist(absPath) {
if err = filelock.Copy(absPath, historyPath); err != nil {
return
}
}
if err = filelock.RemoveWithoutFatal(absPath); err != nil {
logging.LogErrorf("remove unused asset [%s] failed: %s", absPath, err)
util.PushErrMsg(fmt.Sprintf("%s", err), 7000)
return
}
ret = absPath
IncSync()
indexHistoryDir(filepath.Base(historyDir), util.NewLute())
return
}
func RemoveUnusedAttributeViews() (ret []string) {
ret = []string{}
var size int64