create_graph.cljs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. (ns create-graph
  2. "A script that creates or updates a DB graph given a sqlite.build EDN file.
  3. If the given graph already exists, the EDN file updates the graph."
  4. (:require ["fs" :as fs]
  5. ["path" :as node-path]
  6. [babashka.cli :as cli]
  7. [clojure.edn :as edn]
  8. [logseq.db :as ldb]
  9. [logseq.db.common.sqlite-cli :as sqlite-cli]
  10. [logseq.db.frontend.validate :as db-validate]
  11. [logseq.db.sqlite.export :as sqlite-export]
  12. [logseq.outliner.cli :as outliner-cli]
  13. [clojure.pprint :as pprint]
  14. [nbb.classpath :as cp]
  15. [nbb.core :as nbb]))
  16. (defn- resolve-path
  17. "If relative path, resolve with $ORIGINAL_PWD"
  18. [path]
  19. (if (node-path/isAbsolute path)
  20. path
  21. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  22. (defn- validate-db [db db-name options]
  23. (if-let [errors (:errors
  24. (db-validate/validate-local-db!
  25. db
  26. (merge options {:db-name db-name :verbose true})))]
  27. (do
  28. (println "Found" (count errors)
  29. (if (= 1 (count errors)) "entity" "entities")
  30. "with errors:")
  31. (pprint/pprint errors)
  32. (js/process.exit 1))
  33. (println "Valid!")))
  34. (def spec
  35. "Options spec"
  36. {:help {:alias :h
  37. :desc "Print help"}
  38. :validate {:alias :v
  39. :desc "Validate db after creation"}
  40. :import {:alias :i
  41. :desc "Import edn file using sqlite-export"}})
  42. (defn -main [args]
  43. (let [{options :opts args' :args} (cli/parse-args args {:spec spec})
  44. [graph-dir edn-path] args'
  45. _ (when (or (nil? graph-dir) (nil? edn-path) (:help options))
  46. (println (str "Usage: $0 GRAPH-NAME EDN-PATH [OPTIONS]\nOptions:\n"
  47. (cli/format-opts {:spec spec})))
  48. (js/process.exit 1))
  49. init-conn-args (conj (sqlite-cli/->open-db-args graph-dir))
  50. sqlite-build-edn (merge (if (:import options) {} {:auto-create-ontology? true})
  51. (-> (resolve-path edn-path) fs/readFileSync str edn/read-string))
  52. graph-exists? (fs/existsSync (apply node-path/join init-conn-args))
  53. db-name (if (= 1 (count init-conn-args)) (first init-conn-args) (second init-conn-args))
  54. conn (apply outliner-cli/init-conn
  55. (conj init-conn-args {:classpath (cp/get-classpath) :import-type :cli/create-graph}))
  56. {:keys [init-tx block-props-tx misc-tx] :as _txs}
  57. (if (:import options)
  58. (sqlite-export/build-import sqlite-build-edn @conn {})
  59. (outliner-cli/build-blocks-tx sqlite-build-edn))]
  60. (println "Generating" (count (filter :block/name init-tx)) "pages and"
  61. (count (filter :block/title init-tx)) "blocks ...")
  62. ;; (fs/writeFileSync "txs.edn" (with-out-str (cljs.pprint/pprint _txs)))
  63. ;; (cljs.pprint/pprint _txs)
  64. (ldb/transact! conn init-tx)
  65. (when (seq block-props-tx) (ldb/transact! conn block-props-tx))
  66. (when (seq misc-tx) (ldb/transact! conn misc-tx))
  67. (println (if graph-exists? "Updated graph" "Created graph") (str db-name "!"))
  68. (when (:validate options)
  69. (validate-db @conn db-name {}))))
  70. (when (= nbb/*file* (nbb/invoked-file))
  71. (-main *command-line-args*))