mirror of
https://github.com/logseq/logseq.git
synced 2026-04-27 23:54:55 +00:00
enhance(dev): scripts can read or write db graphs as full path files
This allows for easier use of debugging graphs as they are exported by users as a file. Also DRYed up duplicated helper
This commit is contained in:
24
deps/db/script/create_graph.cljs
vendored
24
deps/db/script/create_graph.cljs
vendored
@@ -2,17 +2,16 @@
|
||||
"A script that creates or updates a DB graph given a sqlite.build EDN file.
|
||||
If the given graph already exists, the EDN file updates the graph."
|
||||
(:require ["fs" :as fs]
|
||||
["os" :as os]
|
||||
["path" :as node-path]
|
||||
[babashka.cli :as cli]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.string :as string]
|
||||
[datascript.core :as d]
|
||||
[logseq.db.sqlite.export :as sqlite-export]
|
||||
[logseq.outliner.cli :as outliner-cli]
|
||||
[nbb.classpath :as cp]
|
||||
[nbb.core :as nbb]
|
||||
[validate-db]))
|
||||
[validate-db]
|
||||
[logseq.db.common.sqlite-cli :as sqlite-cli]))
|
||||
|
||||
(defn- resolve-path
|
||||
"If relative path, resolve with $ORIGINAL_PWD"
|
||||
@@ -21,17 +20,6 @@
|
||||
path
|
||||
(node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
|
||||
|
||||
(defn- get-dir-and-db-name
|
||||
"Gets dir and db name for use with open-db! Works for relative and absolute paths and
|
||||
defaults to ~/logseq/graphs/ when no '/' present in name"
|
||||
[graph-dir]
|
||||
(if (string/includes? graph-dir "/")
|
||||
(let [resolve-path' #(if (node-path/isAbsolute %) %
|
||||
;; $ORIGINAL_PWD used by bb tasks to correct current dir
|
||||
(node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
|
||||
((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
|
||||
[(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
|
||||
|
||||
(def spec
|
||||
"Options spec"
|
||||
{:help {:alias :h
|
||||
@@ -48,11 +36,13 @@
|
||||
(println (str "Usage: $0 GRAPH-NAME EDN-PATH [OPTIONS]\nOptions:\n"
|
||||
(cli/format-opts {:spec spec})))
|
||||
(js/process.exit 1))
|
||||
[dir db-name] (get-dir-and-db-name graph-dir)
|
||||
init-conn-args (conj (sqlite-cli/->open-db-args graph-dir))
|
||||
sqlite-build-edn (merge (if (:import options) {} {:auto-create-ontology? true})
|
||||
(-> (resolve-path edn-path) fs/readFileSync str edn/read-string))
|
||||
graph-exists? (fs/existsSync (node-path/join dir db-name))
|
||||
conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath) :import-type :cli/create-graph})
|
||||
graph-exists? (fs/existsSync (apply node-path/join init-conn-args))
|
||||
db-name (if (= 1 (count init-conn-args)) (first init-conn-args) (second init-conn-args))
|
||||
conn (apply outliner-cli/init-conn
|
||||
(conj init-conn-args {:classpath (cp/get-classpath) :import-type :cli/create-graph}))
|
||||
{:keys [init-tx block-props-tx misc-tx] :as _txs}
|
||||
(if (:import options)
|
||||
(sqlite-export/build-import sqlite-build-edn @conn {})
|
||||
|
||||
20
deps/db/script/diff_graphs.cljs
vendored
20
deps/db/script/diff_graphs.cljs
vendored
@@ -1,27 +1,13 @@
|
||||
(ns diff-graphs
|
||||
"A script that diffs two DB graphs through their sqlite.build EDN"
|
||||
(:require ["os" :as os]
|
||||
["path" :as node-path]
|
||||
[babashka.cli :as cli]
|
||||
(:require [babashka.cli :as cli]
|
||||
[clojure.data :as data]
|
||||
[clojure.pprint :as pprint]
|
||||
[clojure.string :as string]
|
||||
[logseq.common.config :as common-config]
|
||||
[logseq.db.common.sqlite-cli :as sqlite-cli]
|
||||
[logseq.db.sqlite.export :as sqlite-export]
|
||||
[nbb.core :as nbb]))
|
||||
|
||||
(defn- get-dir-and-db-name
|
||||
"Gets dir and db name for use with open-db! Works for relative and absolute paths and
|
||||
defaults to ~/logseq/graphs/ when no '/' present in name"
|
||||
[graph-dir]
|
||||
(if (string/includes? graph-dir "/")
|
||||
(let [resolve-path' #(if (node-path/isAbsolute %) %
|
||||
;; $ORIGINAL_PWD used by bb tasks to correct current dir
|
||||
(node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
|
||||
((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
|
||||
[(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
|
||||
|
||||
(def spec
|
||||
"Options spec"
|
||||
{:help {:alias :h
|
||||
@@ -43,8 +29,8 @@
|
||||
(println (str "Usage: $0 GRAPH-NAME GRAPH-NAME2 [& ARGS] [OPTIONS]\nOptions:\n"
|
||||
(cli/format-opts {:spec spec})))
|
||||
(js/process.exit 1))
|
||||
conn (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir))
|
||||
conn2 (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir2))
|
||||
conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-dir))
|
||||
conn2 (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-dir2))
|
||||
export-options (select-keys options [:include-timestamps? :exclude-namespaces :exclude-built-in-pages?])
|
||||
export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})
|
||||
export-map2 (sqlite-export/build-export @conn2 {:export-type :graph :graph-options export-options})
|
||||
|
||||
13
deps/db/script/dump_datoms.cljs
vendored
13
deps/db/script/dump_datoms.cljs
vendored
@@ -3,28 +3,21 @@
|
||||
|
||||
$ yarn -s nbb-logseq script/dump_datoms.cljs db-name datoms.edn"
|
||||
(:require ["fs" :as fs]
|
||||
["os" :as os]
|
||||
["path" :as path]
|
||||
["path" :as node-path]
|
||||
[clojure.pprint :as pprint]
|
||||
[datascript.core :as d]
|
||||
[logseq.db.common.sqlite-cli :as sqlite-cli]
|
||||
[nbb.core :as nbb]))
|
||||
|
||||
(defn read-graph
|
||||
"The db graph bare version of gp-cli/parse-graph"
|
||||
[graph-name]
|
||||
(let [graphs-dir (path/join (os/homedir) "logseq/graphs")]
|
||||
(sqlite-cli/open-db! graphs-dir graph-name)))
|
||||
|
||||
(defn -main [args]
|
||||
(when (< (count args) 2)
|
||||
(println "Usage: $0 GRAPH FILE")
|
||||
(js/process.exit 1))
|
||||
(let [[graph-name file*] args
|
||||
conn (read-graph graph-name)
|
||||
conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-name))
|
||||
datoms (mapv #(vec %) (d/datoms @conn :eavt))
|
||||
parent-dir (or js/process.env.ORIGINAL_PWD ".")
|
||||
file (path/join parent-dir file*)]
|
||||
file (node-path/join parent-dir file*)]
|
||||
(println "Writing" (count datoms) "datoms to" file)
|
||||
(fs/writeFileSync file (with-out-str (pprint/pprint datoms)))))
|
||||
|
||||
|
||||
17
deps/db/script/export_graph.cljs
vendored
17
deps/db/script/export_graph.cljs
vendored
@@ -1,12 +1,9 @@
|
||||
(ns export-graph
|
||||
"A script that exports a graph to a sqlite.build EDN file"
|
||||
(:require ["fs" :as fs]
|
||||
["os" :as os]
|
||||
["path" :as node-path]
|
||||
[babashka.cli :as cli]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.pprint :as pprint]
|
||||
[clojure.string :as string]
|
||||
[logseq.db.common.sqlite-cli :as sqlite-cli]
|
||||
[logseq.db.sqlite.export :as sqlite-export]
|
||||
[nbb.core :as nbb]))
|
||||
@@ -18,17 +15,6 @@
|
||||
path
|
||||
(node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
|
||||
|
||||
(defn- get-dir-and-db-name
|
||||
"Gets dir and db name for use with open-db! Works for relative and absolute paths and
|
||||
defaults to ~/logseq/graphs/ when no '/' present in name"
|
||||
[graph-dir]
|
||||
(if (string/includes? graph-dir "/")
|
||||
(let [resolve-path' #(if (node-path/isAbsolute %) %
|
||||
;; $ORIGINAL_PWD used by bb tasks to correct current dir
|
||||
(node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
|
||||
((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
|
||||
[(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
|
||||
|
||||
(def spec
|
||||
"Options spec"
|
||||
{:help {:alias :h
|
||||
@@ -54,8 +40,7 @@
|
||||
(println (str "Usage: $0 GRAPH-NAME [& ARGS] [OPTIONS]\nOptions:\n"
|
||||
(cli/format-opts {:spec spec})))
|
||||
(js/process.exit 1))
|
||||
[dir db-name] (get-dir-and-db-name graph-dir)
|
||||
conn (sqlite-cli/open-db! dir db-name)
|
||||
conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-dir))
|
||||
export-options (dissoc options :file)
|
||||
export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})]
|
||||
(if (:file options)
|
||||
|
||||
21
deps/db/script/query.cljs
vendored
21
deps/db/script/query.cljs
vendored
@@ -3,15 +3,12 @@
|
||||
|
||||
$ yarn -s nbb-logseq script/query.cljs db-name '[:find (pull ?b [:block/name :block/title]) :where [?b :block/created-at]]'"
|
||||
(:require ["child_process" :as child-process]
|
||||
["os" :as os]
|
||||
["path" :as node-path]
|
||||
[babashka.cli :as cli]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.pprint :as pprint]
|
||||
[clojure.string :as string]
|
||||
[datascript.core :as d]
|
||||
[logseq.db.frontend.rules :as rules]
|
||||
[logseq.db.common.sqlite-cli :as sqlite-cli]
|
||||
[logseq.db.frontend.rules :as rules]
|
||||
[nbb.core :as nbb]))
|
||||
|
||||
(defn- sh
|
||||
@@ -22,17 +19,6 @@
|
||||
(clj->js (rest cmd))
|
||||
(clj->js (merge {:stdio "inherit"} opts))))
|
||||
|
||||
(defn- get-dir-and-db-name
|
||||
"Gets dir and db name for use with open-db! Works for relative and absolute paths and
|
||||
defaults to ~/logseq/graphs/ when no '/' present in name"
|
||||
[graph-dir]
|
||||
(if (string/includes? graph-dir "/")
|
||||
(let [resolve-path' #(if (node-path/isAbsolute %) %
|
||||
;; $ORIGINAL_PWD used by bb tasks to correct current dir
|
||||
(node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
|
||||
((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
|
||||
[(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
|
||||
|
||||
(def spec
|
||||
"Options spec"
|
||||
{:help {:alias :h
|
||||
@@ -48,9 +34,8 @@
|
||||
:coerce []
|
||||
:desc "Lookup entities instead of query"}})
|
||||
|
||||
(defn query-graph [graph-dir args'' options]
|
||||
(let [[dir db-name] (get-dir-and-db-name graph-dir)
|
||||
conn (sqlite-cli/open-db! dir db-name)
|
||||
(defn query-graph [graph args'' options]
|
||||
(let [conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph))
|
||||
results (if (:entity options)
|
||||
(map #(when-let [ent (d/entity @conn
|
||||
(if (string? %) (edn/read-string %) %))]
|
||||
|
||||
21
deps/db/script/validate_db.cljs
vendored
21
deps/db/script/validate_db.cljs
vendored
@@ -1,11 +1,8 @@
|
||||
(ns validate-db
|
||||
"Script that validates the datascript db of a DB graph
|
||||
NOTE: This script is also used in CI to confirm our db's schema is up to date"
|
||||
(:require ["os" :as os]
|
||||
["path" :as node-path]
|
||||
[babashka.cli :as cli]
|
||||
(:require [babashka.cli :as cli]
|
||||
[cljs.pprint :as pprint]
|
||||
[clojure.string :as string]
|
||||
[datascript.core :as d]
|
||||
[logseq.db.frontend.malli-schema :as db-malli-schema]
|
||||
[logseq.db.frontend.validate :as db-validate]
|
||||
@@ -50,17 +47,6 @@
|
||||
(js/process.exit 1))
|
||||
(println "Valid!"))))
|
||||
|
||||
(defn- get-dir-and-db-name
|
||||
"Gets dir and db name for use with open-db! Works for relative and absolute paths and
|
||||
defaults to ~/logseq/graphs/ when no '/' present in name"
|
||||
[graph-dir]
|
||||
(if (string/includes? graph-dir "/")
|
||||
(let [resolve-path' #(if (node-path/isAbsolute %) %
|
||||
;; $ORIGINAL_PWD used by bb tasks to correct current dir
|
||||
(node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
|
||||
((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
|
||||
[(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
|
||||
|
||||
(def spec
|
||||
"Options spec"
|
||||
{:help {:alias :h
|
||||
@@ -86,8 +72,9 @@
|
||||
(validate-db* db ent-maps options)))
|
||||
|
||||
(defn- validate-graph [graph-dir options]
|
||||
(let [[dir db-name] (get-dir-and-db-name graph-dir)
|
||||
conn (try (sqlite-cli/open-db! dir db-name)
|
||||
(let [open-db-args (sqlite-cli/->open-db-args graph-dir)
|
||||
db-name (if (= 1 (count open-db-args)) (first open-db-args) (second open-db-args))
|
||||
conn (try (apply sqlite-cli/open-db! open-db-args)
|
||||
(catch :default e
|
||||
(println "Error: For graph" (str (pr-str graph-dir) ":") (str e))
|
||||
(js/process.exit 1)))]
|
||||
|
||||
Reference in New Issue
Block a user