Skip to content

Commit

Permalink
feat: dev validate db
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Dec 30, 2024
1 parent 1760161 commit deb14ed
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 31 deletions.
22 changes: 13 additions & 9 deletions src/main/frontend/handler/common/developer.cljs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
(ns frontend.handler.common.developer
"Common fns for developer related functionality"
(:require [frontend.db :as db]
[cljs.pprint :as pprint]
[frontend.state :as state]
(:require [cljs.pprint :as pprint]
[datascript.impl.entity :as de]
[frontend.config :as config]
[frontend.db :as db]
[frontend.format.mldoc :as mldoc]
[frontend.handler.notification :as notification]
[frontend.persist-db :as persist-db]
[frontend.state :as state]
[frontend.ui :as ui]
[frontend.util.page :as page-util]
[frontend.format.mldoc :as mldoc]
[frontend.config :as config]
[frontend.persist-db :as persist-db]
[promesa.core :as p]
[datascript.impl.entity :as de]
[logseq.db.frontend.property :as db-property]))
[logseq.db.frontend.property :as db-property]
[promesa.core :as p]))

;; Fns used between menus and commands
(defn show-entity-data
Expand Down Expand Up @@ -88,6 +88,10 @@
(show-content-ast (get-in page-data [:block/file :file/content]) (:block/format page-data))
(notification/show! "No page found" :warning)))))

(defn ^:export validate-db []
(when-let [^Object worker @state/*db-worker]
(.validate-db worker (state/get-current-repo))))

(defn import-chosen-graph
[repo]
(p/let [_ (persist-db/<unsafe-delete repo)]
Expand Down
8 changes: 6 additions & 2 deletions src/main/frontend/handler/worker.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
[frontend.handler.file :as file-handler]
[frontend.handler.notification :as notification]
[frontend.state :as state]
[promesa.core :as p]
[logseq.db :as ldb]))
[lambdaisland.glogi :as log]
[logseq.db :as ldb]
[promesa.core :as p]))

(defmulti handle identity)

Expand All @@ -25,6 +26,9 @@
(defmethod handle :notification [_ _worker data]
(apply notification/show! data))

(defmethod handle :log [_ _worker [name level data]]
(log/log name level data))

(defmethod handle :add-repo [_ _worker data]
(state/add-repo! {:url (:repo data)})
(state/pub-event! [:graph/switch (:repo data) {:rtc-download? true}]))
Expand Down
38 changes: 22 additions & 16 deletions src/main/frontend/modules/shortcut/config.cljs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
(ns frontend.modules.shortcut.config
(:require [clojure.string :as str]
(:require [clojure.data :as data]
[clojure.string :as str]
[electron.ipc :as ipc]
[frontend.commands :as commands]
[frontend.components.commit :as commit]
[frontend.extensions.srs.handler :as srs]
[frontend.config :as config]
[frontend.dicts :as dicts]
[frontend.extensions.pdf.utils :as pdf-utils]
[frontend.extensions.srs.handler :as srs]
[frontend.handler.config :as config-handler]
[frontend.handler.editor :as editor-handler]
[frontend.handler.paste :as paste-handler]
[frontend.handler.export :as export-handler]
[frontend.handler.history :as history]
[frontend.handler.journal :as journal-handler]
[frontend.handler.jump :as jump-handler]
[frontend.handler.notification :as notification]
[frontend.handler.page :as page-handler]
[frontend.handler.paste :as paste-handler]
[frontend.handler.plugin :as plugin-handler]
[frontend.handler.plugin-config :as plugin-config-handler]
[frontend.handler.route :as route-handler]
[frontend.handler.journal :as journal-handler]
[frontend.handler.search :as search-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.plugin :as plugin-handler]
[frontend.handler.export :as export-handler]
[frontend.handler.whiteboard :as whiteboard-handler]
[frontend.handler.plugin-config :as plugin-config-handler]
[frontend.handler.window :as window-handler]
[frontend.handler.jump :as jump-handler]
[frontend.dicts :as dicts]
[frontend.modules.shortcut.before :as m]
[frontend.state :as state]
[frontend.util :refer [mac?] :as util]
[frontend.commands :as commands]
[frontend.config :as config]
[electron.ipc :as ipc]
[promesa.core :as p]
[clojure.data :as data]
[medley.core :as medley]))
[medley.core :as medley]
[promesa.core :as p]))

(defn- search
[mode]
Expand Down Expand Up @@ -603,7 +603,11 @@

:dev/show-page-ast {:binding []
:inactive (not (state/developer-mode?))
:fn :frontend.handler.common.developer/show-page-ast}})
:fn :frontend.handler.common.developer/show-page-ast}

:dev/validate-db {:binding []
:inactive (not (state/developer-mode?))
:fn :frontend.handler.common.developer/validate-db}})

(let [keyboard-commands
{::commands (set (keys all-built-in-keyboard-shortcuts))
Expand Down Expand Up @@ -809,6 +813,7 @@
:dev/show-page-data
:dev/show-page-ast
:dev/replace-graph-with-db-file
:dev/validate-db
:ui/customize-appearance])
(with-meta {:before m/enable-when-not-editing-mode!}))

Expand Down Expand Up @@ -996,6 +1001,7 @@
:dev/show-page-data
:dev/show-page-ast
:dev/replace-graph-with-db-file
:dev/validate-db
:ui/clear-all-notifications]

:shortcut.category/plugins
Expand Down
33 changes: 33 additions & 0 deletions src/main/frontend/worker/db/validate.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns frontend.worker.db.validate
"Validate db"
(:require [cljs.pprint :as pprint]
[frontend.worker.util :as worker-util]
[logseq.db :as ldb]
[logseq.db.frontend.validate :as db-validate]))

(defn- counts-from-entities
[entities]
{:entities (count entities)
:pages (count (filter :block/name entities))
:blocks (count (filter :block/title entities))
:classes (count (filter ldb/class? entities))
:objects (count (filter #(seq (:block/tags %)) entities))
:properties (count (filter ldb/property? entities))
:property-values (count (mapcat :block/properties entities))})

(defn validate-db
[db]
(let [{:keys [errors datom-count entities]} (db-validate/validate-db! db)]
(if errors
(do
(worker-util/post-message :log [:db-invalid :error
{:msg (str "Validation detected " (count errors) " invalid block(s):")
:counts (assoc (counts-from-entities entities) :datoms datom-count)}])
(pprint/pprint errors)
(worker-util/post-message :notification
[(str "Validation detected " (count errors) " invalid block(s). These blocks may be buggy when you interact with them. See the javascript console for more.")
:warning false]))

(worker-util/post-message :notification
[(str "Your graph is valid! " (assoc (counts-from-entities entities) :datoms datom-count))
:success false]))))
6 changes: 6 additions & 0 deletions src/main/frontend/worker/db_worker.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[frontend.worker.db-listener :as db-listener]
[frontend.worker.db-metadata :as worker-db-metadata]
[frontend.worker.db.migrate :as db-migrate]
[frontend.worker.db.validate :as worker-db-validate]
[frontend.worker.device :as worker-device]
[frontend.worker.export :as worker-export]
[frontend.worker.file :as file]
Expand Down Expand Up @@ -908,6 +909,11 @@
(undo-redo/record-editor-info! repo (ldb/read-transit-str editor-info-str))
nil)

(validate-db
[_this repo]
(when-let [conn (worker-state/get-datascript-conn repo)]
(worker-db-validate/validate-db @conn)))

(dangerousRemoveAllDbs
[this repo]
(p/let [dbs (.listDB this)]
Expand Down
5 changes: 1 addition & 4 deletions src/resources/dicts/en.edn
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@
:editor/collapse-block-children "Collapse all"
:editor/delete-selection "Delete selected blocks"
:editor/cycle-todo "Rotate the TODO state"
:dev/show-page-data "(Dev) Show page data"
:dev/show-block-data "(Dev) Show block data"
:dev/show-block-ast "(Dev) Show block AST"
:dev/show-page-ast "(Dev) Show page AST"
:content/copy-export-as "Copy / Export as.."
:content/copy-block-url "Copy block URL"
:content/copy-block-ref "Copy block ref"
Expand Down Expand Up @@ -796,4 +792,5 @@
:dev/show-page-data "(Dev) Show page data"
:dev/show-page-ast "(Dev) Show page AST"
:dev/replace-graph-with-db-file "(Dev) Replace graph with its db.sqlite file"
:dev/validate-db "(Dev) Validate current graph"
:window/close "Close window"}}

0 comments on commit deb14ed

Please sign in to comment.