mirror of
https://github.com/logseq/logseq.git
synced 2026-04-24 22:25:01 +00:00
enhance(dev): optionally validate after creating a db
Doing this enough that an option makes this faster to invoke and faster (no re-open) on large graphs
This commit is contained in:
2
bb.edn
2
bb.edn
@@ -81,7 +81,7 @@
|
||||
{:doc "Create a DB graph given a sqlite.build EDN file"
|
||||
:requires ([babashka.fs :as fs])
|
||||
:task (apply shell {:dir "deps/db" :extra-env {"ORIGINAL_PWD" (fs/cwd)}}
|
||||
"yarn -s nbb-logseq -cp src:../outliner/src script/create_graph.cljs" *command-line-args*)}
|
||||
"yarn -s nbb-logseq -cp src:../outliner/src:script script/create_graph.cljs" *command-line-args*)}
|
||||
|
||||
dev:db-import
|
||||
{:doc "Import a file graph to db graph"
|
||||
|
||||
38
deps/db/script/create_graph.cljs
vendored
38
deps/db/script/create_graph.cljs
vendored
@@ -1,12 +1,15 @@
|
||||
(ns create-graph
|
||||
"An example script that creates a DB graph given a sqlite.build EDN file"
|
||||
(:require [logseq.outliner.cli :as outliner-cli]
|
||||
[clojure.string :as string]
|
||||
[clojure.edn :as edn]
|
||||
[datascript.core :as d]
|
||||
["path" :as node-path]
|
||||
"A script that creates a DB graph given a sqlite.build EDN file"
|
||||
(:require ["fs" :as fs]
|
||||
["os" :as os]
|
||||
["fs" :as fs]
|
||||
["path" :as node-path]
|
||||
[babashka.cli :as cli]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.string :as string]
|
||||
[datascript.core :as d]
|
||||
#_:clj-kondo/ignore
|
||||
[logseq.outliner.cli :as outliner-cli]
|
||||
[validate-db]
|
||||
[nbb.classpath :as cp]
|
||||
[nbb.core :as nbb]))
|
||||
|
||||
@@ -17,11 +20,20 @@
|
||||
path
|
||||
(node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
|
||||
|
||||
(def spec
|
||||
"Options spec"
|
||||
{:help {:alias :h
|
||||
:desc "Print help"}
|
||||
:validate {:alias :v
|
||||
:desc "Validate db after creation"}})
|
||||
|
||||
(defn -main [args]
|
||||
(when (not= 2 (count args))
|
||||
(println "Usage: $0 GRAPH-DIR EDN-PATH")
|
||||
(js/process.exit 1))
|
||||
(let [[graph-dir edn-path] args
|
||||
(let [{options :opts args' :args} (cli/parse-args args {:spec spec})
|
||||
[graph-dir edn-path] args'
|
||||
_ (when (or (nil? graph-dir) (nil? edn-path) (:help options))
|
||||
(println (str "Usage: $0 GRAPH-NAME EDN-PATH [OPTIONS]\nOptions:\n"
|
||||
(cli/format-opts {:spec spec})))
|
||||
(js/process.exit 1))
|
||||
[dir db-name] (if (string/includes? graph-dir "/")
|
||||
((juxt node-path/dirname node-path/basename) graph-dir)
|
||||
[(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
|
||||
@@ -34,7 +46,9 @@
|
||||
;; (cljs.pprint/pprint _txs)
|
||||
(d/transact! conn init-tx)
|
||||
(d/transact! conn block-props-tx)
|
||||
(println "Created graph" (str db-name "!"))))
|
||||
(println "Created graph" (str db-name "!"))
|
||||
(when (:validate options)
|
||||
(validate-db/validate-db @conn db-name {:group-errors true :closed-maps true :humanize true}))))
|
||||
|
||||
(when (= nbb/*file* (:file (meta #'-main)))
|
||||
(-main *command-line-args*))
|
||||
|
||||
21
deps/db/script/validate_db.cljs
vendored
21
deps/db/script/validate_db.cljs
vendored
@@ -14,7 +14,7 @@
|
||||
[malli.error :as me]
|
||||
[nbb.core :as nbb]))
|
||||
|
||||
(defn validate-db
|
||||
(defn validate-db*
|
||||
"Validate datascript db as a vec of entity maps"
|
||||
[db ent-maps* {:keys [verbose group-errors humanize closed-maps]}]
|
||||
(let [ent-maps (db-malli-schema/update-properties-in-ents db ent-maps*)
|
||||
@@ -66,6 +66,14 @@
|
||||
:default true
|
||||
:desc "Groups errors by their entity id"}})
|
||||
|
||||
(defn validate-db [db db-name options]
|
||||
(let [datoms (d/datoms db :eavt)
|
||||
ent-maps (db-malli-schema/datoms->entities datoms)]
|
||||
(println "Read graph" (str db-name " with counts: "
|
||||
(pr-str (assoc (db-validate/graph-counts db ent-maps)
|
||||
:datoms (count datoms)))))
|
||||
(validate-db* db ent-maps options)))
|
||||
|
||||
(defn- validate-graph [graph-dir options]
|
||||
(let [[dir db-name] (if (string/includes? graph-dir "/")
|
||||
(let [graph-dir'
|
||||
@@ -75,13 +83,8 @@
|
||||
conn (try (sqlite-cli/open-db! dir db-name)
|
||||
(catch :default e
|
||||
(println "Error: For graph" (str (pr-str graph-dir) ":") (str e))
|
||||
(js/process.exit 1)))
|
||||
datoms (d/datoms @conn :eavt)
|
||||
ent-maps (db-malli-schema/datoms->entities datoms)]
|
||||
(println "Read graph" (str db-name " with counts: "
|
||||
(pr-str (assoc (db-validate/graph-counts @conn ent-maps)
|
||||
:datoms (count datoms)))))
|
||||
(validate-db @conn ent-maps options)))
|
||||
(js/process.exit 1)))]
|
||||
(validate-db @conn db-name options)))
|
||||
|
||||
(defn -main [argv]
|
||||
(let [{:keys [args opts]} (cli/parse-args argv {:spec spec})
|
||||
@@ -92,5 +95,5 @@
|
||||
(doseq [graph-dir args]
|
||||
(validate-graph graph-dir opts))))
|
||||
|
||||
(when (= nbb/*file* (:file (meta #'-main)))
|
||||
(when (= nbb/*file* (nbb/invoked-file))
|
||||
(-main *command-line-args*))
|
||||
|
||||
Reference in New Issue
Block a user