From f7d8a0213851870c3a52b2d60493ee80b4a9b3ed Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 15 Jun 2023 10:22:28 +0800 Subject: [PATCH] :art: Auto rebuild history database index when indexing failed https://github.com/siyuan-note/siyuan/issues/8543 --- kernel/model/history.go | 11 +++++++++++ kernel/sql/queue_history.go | 2 +- kernel/util/runtime.go | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/kernel/model/history.go b/kernel/model/history.go index 5a537b27b..c4e991b30 100644 --- a/kernel/model/history.go +++ b/kernel/model/history.go @@ -33,6 +33,7 @@ import ( "github.com/88250/lute/ast" "github.com/88250/lute/parse" "github.com/88250/lute/render" + "github.com/siyuan-note/eventbus" "github.com/siyuan-note/filelock" "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/conf" @@ -691,3 +692,13 @@ func fromSQLHistories(sqlHistories []*sql.History) (ret []*HistoryItem) { } return } + +func init() { + subscribeSQLHistoryEvents() +} + +func subscribeSQLHistoryEvents() { + eventbus.Subscribe(util.EvtSQLHistoryRebuild, func() { + ReindexHistory() + }) +} diff --git a/kernel/sql/queue_history.go b/kernel/sql/queue_history.go index 3ee8a42fb..a48e820ef 100644 --- a/kernel/sql/queue_history.go +++ b/kernel/sql/queue_history.go @@ -83,7 +83,7 @@ func FlushHistoryQueue() { if err = execHistoryOp(op, tx, context); nil != err { tx.Rollback() logging.LogErrorf("queue operation failed: %s", err) - util.PushMsg("History database index failed, please try to manually rebuild it in [Data history - File history]", 5000) + eventbus.Publish(util.EvtSQLHistoryRebuild) return } diff --git a/kernel/util/runtime.go b/kernel/util/runtime.go index dbf223513..f3c8a8bf4 100644 --- a/kernel/util/runtime.go +++ b/kernel/util/runtime.go @@ -404,4 +404,6 @@ func existAvailabilityStatus(workspaceAbsPath string) bool { const ( EvtConfPandocInitialized = "conf.pandoc.initialized" + + EvtSQLHistoryRebuild = "sql.history.rebuild" )