diff_graphs.cljs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. (ns diff-graphs
  2. "A script that diffs two DB graphs through their sqlite.build EDN"
  3. (:require ["os" :as os]
  4. ["path" :as node-path]
  5. [babashka.cli :as cli]
  6. [clojure.data :as data]
  7. [clojure.pprint :as pprint]
  8. [clojure.string :as string]
  9. #_:clj-kondo/ignore
  10. [logseq.db.sqlite.cli :as sqlite-cli]
  11. [logseq.db.sqlite.export :as sqlite-export]
  12. [nbb.core :as nbb]))
  13. (defn- get-dir-and-db-name
  14. "Gets dir and db name for use with open-db!"
  15. [graph-dir]
  16. (if (string/includes? graph-dir "/")
  17. (let [graph-dir'
  18. (node-path/join (or js/process.env.ORIGINAL_PWD ".") graph-dir)]
  19. ((juxt node-path/dirname node-path/basename) graph-dir'))
  20. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
  21. (def spec
  22. "Options spec"
  23. {:help {:alias :h
  24. :desc "Print help"}
  25. :timestamps {:alias :t
  26. :desc "Include timestamps in export"}})
  27. (defn -main [args]
  28. (let [{options :opts args' :args} (cli/parse-args args {:spec spec})
  29. [graph-dir graph-dir2] args'
  30. _ (when (or (nil? graph-dir) (nil? graph-dir2) (:help options))
  31. (println (str "Usage: $0 GRAPH-NAME GRAPH-NAME2 [& ARGS] [OPTIONS]\nOptions:\n"
  32. (cli/format-opts {:spec spec})))
  33. (js/process.exit 1))
  34. conn (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir))
  35. conn2 (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir2))
  36. export-options {:include-timestamps? (:timestamps options)}
  37. export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})
  38. export-map2 (sqlite-export/build-export @conn2 {:export-type :graph :graph-options export-options})
  39. diff (butlast (data/diff export-map export-map2))]
  40. (pprint/pprint diff)))
  41. (when (= nbb/*file* (:file (meta #'-main)))
  42. (-main *command-line-args*))