diff_graphs.cljs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. [logseq.common.config :as common-config]
  10. #_:clj-kondo/ignore
  11. [logseq.db.sqlite.cli :as sqlite-cli]
  12. [logseq.db.sqlite.export :as sqlite-export]
  13. [nbb.core :as nbb]))
  14. (defn- get-dir-and-db-name
  15. "Gets dir and db name for use with open-db!"
  16. [graph-dir]
  17. (if (string/includes? graph-dir "/")
  18. (let [graph-dir'
  19. (node-path/join (or js/process.env.ORIGINAL_PWD ".") graph-dir)]
  20. ((juxt node-path/dirname node-path/basename) graph-dir'))
  21. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
  22. (def spec
  23. "Options spec"
  24. {:help {:alias :h
  25. :desc "Print help"}
  26. :exclude-namespaces {:alias :e
  27. :coerce #{}
  28. :desc "Namespaces to exclude from properties and classes"}
  29. :include-timestamps? {:alias :t
  30. :desc "Include timestamps in export"}})
  31. (defn -main [args]
  32. (let [{options :opts args' :args} (cli/parse-args args {:spec spec})
  33. [graph-dir graph-dir2] args'
  34. _ (when (or (nil? graph-dir) (nil? graph-dir2) (:help options))
  35. (println (str "Usage: $0 GRAPH-NAME GRAPH-NAME2 [& ARGS] [OPTIONS]\nOptions:\n"
  36. (cli/format-opts {:spec spec})))
  37. (js/process.exit 1))
  38. conn (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir))
  39. conn2 (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir2))
  40. export-options (select-keys options [:include-timestamps? :exclude-namespaces])
  41. export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})
  42. export-map2 (sqlite-export/build-export @conn2 {:export-type :graph :graph-options export-options})
  43. prepare-export-to-diff
  44. (fn [m]
  45. (-> m
  46. (update :classes update-vals (fn [m]
  47. (update m :build/class-properties sort)))
  48. ;; TODO: fix built-in views
  49. (update :pages-and-blocks (fn [pbs]
  50. (vec (remove #(= (:block/title (:page %)) common-config/views-page-name) pbs))))))
  51. diff (->> (data/diff (prepare-export-to-diff export-map) (prepare-export-to-diff export-map2))
  52. butlast)]
  53. (pprint/pprint diff)))
  54. (when (= nbb/*file* (nbb/invoked-file))
  55. (-main *command-line-args*))